[英]Bash script, commands using subdirectory
I am trying to compare the files in two directories but I am having trouble getting my stat command to work properly, I can get it to work from the command line using the same syntax as I have here. 我正在尝试比较两个目录中的文件,但是我无法使stat命令正常工作,我可以使用与此处相同的语法从命令行中使其工作。
# Usage: compdir <base_dir> <modified_dir>
# Handle MODIFIED and REMOVED files
for i in "${arr1[@]}"
do
REMOVED=1
for j in "${arr2[@]}"
do
if [ $i = $j ]; then
# take time stamps
dir1="$1"
dir2="$2"
stamp1=stat --format %Y "$i" <--------- THIS LINE
stamp2=stat --format %Y "$j"
if [[ $stamp1 > $stamp2 ]] ; then
echo "$j MODIFIED"
fi
REMOVED=0
break
fi
done
if [ $REMOVED -eq 1 ]; then
echo $i REMOVED
fi
done
# handle NEW files
for j in "${arr2[@]}"
do
NEW=1
for i in "${arr1[@]}"
do
if [ $j = $i ]; then
NEW=0
break
fi
done
if [ $NEW -eq 1 ]; then
echo "$j NEW"
fi
done
On the line marked with a <------- and the line below I get the error --format: command not found. 在标有<-------的行和下面的行中,出现错误--format:命令未找到。 I am assuming this is because I am in the base directory and not in the subdirectories. 我假设这是因为我在基本目录中,而不在子目录中。 Since the arguments passed are the names of the directories I've tried doing something like "$1/$i" to get the line to work but have had no luck. 由于传递的参数是目录的名称,因此我尝试执行“ $ 1 / $ i”之类的操作来使该行正常工作,但是没有运气。
You cannot just assign a command to a variable, you have to do it in a subshell using $()
or ``. 您不能仅将命令分配给变量,而必须在子shell中使用$()
或``来执行它。 Like here: 像这儿:
Option 1: 选项1:
stamp1=$(stat --format %Y "$i")
Option 2: 选项2:
stamp1=`stat --format %Y "$i"`
I personally prefer option 1 (subshell) 我个人更喜欢选项1(subshell)
Addendum: As stated in the comment by sp asic (thx), use $()
as the backticks are the legacy syntax, see: http://mywiki.wooledge.org/BashFAQ/082 附录:如sp asic(thx)的注释中所述,使用$()
作为反引号是传统语法,请参阅: http : //mywiki.wooledge.org/BashFAQ/082
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.