Bash 脚本,使用子目录的命令
Bash script, commands using subdirectory
我正在尝试比较两个目录中的文件,但我无法让我的 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=""
dir2=""
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
在标有 <------ 的行和下面的行中,我收到错误 --format: command not found。我假设这是因为我在基本目录中而不是在子目录中。由于传递的参数是目录的名称,因此我尝试执行类似“$1/$i”的操作来使线路正常工作,但没有成功。
您不能只将命令分配给变量,您必须使用 $()
或 `` 在子 shell 中执行此操作。喜欢这里:
选项 1:
stamp1=$(stat --format %Y "$i")
选项 2:
stamp1=`stat --format %Y "$i"`
我个人更喜欢选项 1(子 shell)
附录:如 sp asic (thx) 的评论所述,使用 $()
因为反引号是遗留语法,请参阅:http://mywiki.wooledge.org/BashFAQ/082
我正在尝试比较两个目录中的文件,但我无法让我的 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=""
dir2=""
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
在标有 <------ 的行和下面的行中,我收到错误 --format: command not found。我假设这是因为我在基本目录中而不是在子目录中。由于传递的参数是目录的名称,因此我尝试执行类似“$1/$i”的操作来使线路正常工作,但没有成功。
您不能只将命令分配给变量,您必须使用 $()
或 `` 在子 shell 中执行此操作。喜欢这里:
选项 1:
stamp1=$(stat --format %Y "$i")
选项 2:
stamp1=`stat --format %Y "$i"`
我个人更喜欢选项 1(子 shell)
附录:如 sp asic (thx) 的评论所述,使用 $()
因为反引号是遗留语法,请参阅:http://mywiki.wooledge.org/BashFAQ/082