Linux 如何写一个“";bash script.sh参数";

Linux 如何写一个“";bash script.sh参数";,linux,bash,Linux,Bash,嗨,有人能帮我吗 如何编写以文件名作为参数并以这种方式精确显示其修改日期和时间的脚本: [user@localhost...]$ bash script.sh temp.txt the file temp.txt was modified on May 1 20:20 [user@local....]$ bash script.sh testRegex Pub the file testRegex was modified on May 1 20:22 the directory /home/

嗨,有人能帮我吗

如何编写以文件名作为参数并以这种方式精确显示其修改日期和时间的脚本:

[user@localhost...]$ bash script.sh temp.txt
the file temp.txt was modified on May 1 20:20
[user@local....]$ bash script.sh testRegex Pub
the file testRegex was modified on May 1 20:22
the directory /home/user/Public was modified on Dec 26 08:00
the directory /home/user/Pubs. was modified on May 2 20:00
然后修改该脚本,使其列出名称包含给定模式的目录的修改日期,如下所示:

[user@localhost...]$ bash script.sh temp.txt
the file temp.txt was modified on May 1 20:20
[user@local....]$ bash script.sh testRegex Pub
the file testRegex was modified on May 1 20:22
the directory /home/user/Public was modified on Dec 26 08:00
the directory /home/user/Pubs. was modified on May 2 20:00
请帮忙,我需要尽快回答这个问题


谢谢

传递选项和值是一种很好的方法:

例如:

file_name=""
help_message="To use this script type script.sh --file /path/to/file.txt"
# -- Get input options (if any)
while [[ $# > 0 ]] ;do
    key="$1"
    case ${key,,} in
        -f|--file)
            file_name="${2,,}"
            shift
        ;;
        -h|--help)
            echo -e "$help_message"
            exit;
            shift
        ;;
    esac
    shift
done
按如下方式调用脚本:

bash script.sh -f "temp.txt"

关于脚本的“逻辑”,您必须弄清楚;-)

实际上这很简单。你应该仔细阅读@johnbollinger所说的命令。我还使用该命令格式化日期。您可以阅读脚本的参数

将所有这些结合在一起将产生-

#!/bin/bash

filename=$1;
dirname=$2;

file_mod_date=`date -d @$( stat -c %Y $1 ) +%m" "%B" "%H:%M`;

echo "The file ${filename} was modified on ${file_mod_date}";
if [ "$2" == "" ]; then
    exit 1;
else
for i in /home/user/*${dirname}*/; do
    dir_mod_date=`date -d @$( stat -c %Y $i ) +%m" "%B" "%H:%M`;
    echo "The directory ${i} was modified on ${dir_mod_date}";
done
fi

我们不会为你做家庭作业,你到底在做什么还不清楚。但也许你可以看看这个命令。你下面对其中一个答案的评论很幽默:“谢谢,但我真的不能做太多,到期日是在2天内,我没有参加这门课程,但我试图了解尽可能多,与它一起工作,但我被困在这。任何简单的帮助都会很有意义。”。我们真的需要一个给我的Codez堆栈交换给像你这样的人。哈哈,你当时不应该做这项工作