gpt4 book ai didi

linux - 如何写 "bash script.sh argument "

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:18 24 4
gpt4 key购买 nike

嗨,有人可以帮我解决这个问题吗?

如何编写一个脚本,该脚本接受文件名作为参数并以这种方式准确显示其修改日期和时间:

[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

请帮忙,我需要快速回答

谢谢

最佳答案

这实际上做起来非常简单。您应该阅读stat正如@John Bollinger所说的命令。我还使用了date命令格式化日期。您可以阅读有关为脚本获取参数的内容 here

将所有这些结合起来会得到 -

#!/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

关于linux - 如何写 "bash script.sh argument ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50789500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com