我正在使用 rsync 命令同步两个文件夹,在 rysnc 成功时,我想成功复制文件,并在复制附加源文件夹名称参数(如 Success_FolderName)时。我使用 $(basename !:3) 获取第三个参数,即文件夹名称。
bash /Sync.sh 10_03_2016
#! /bin/bash
set -o history
set -o histexpand
/usr/bin/rsync -avh -r /Source/$1 /Destination/
rsyncStatus=$?
folderParam=$(basename !:3)
if($rsyncStatus==0)
then
cp /Status/Sucesss /Result/Success_$folderParam
else
cp /Status/Failure /Result/Failure_$folderParam
if
输出
错误
/Sync.sh: line 7: :3: bad word specifier
并且文件被复制为“Success_”
答案1
我不明白你为什么要把事情搞得这么复杂。!:N
当您已经将需要的所有内容作为参数传递时,为什么还要使用(相当挑剔的)历史扩展功能呢?例如:
#! /bin/bash
source="/Source/$1"
destination="Destination/"
folderParam="$(basename "$source")"
/usr/bin/rsync -avh -r "$source" "$destination"
rsyncStatus=$?
if($rsyncStatus==0)
then
cp /Status/Sucesss /Result/Success_"$folderParam"
else
cp /Status/Failure /Result/Failure_"$folderParam"
if
或者,更简单:
#! /bin/bash
source="/Source/$1"
destination="Destination/"
folderParam="$(basename "$source")"
if /usr/bin/rsync -avh -r "$source" "$destination"; then
cp /Status/Sucesss /Result/Success_"$folderParam"
else
cp /Status/Failure /Result/Failure_"$folderParam"
if
甚至:
#! /bin/bash
source="/Source/$1"
destination="Destination/"
folderParam="$(basename "$source")"
touch "/Result/Failure_$folderParam"
/usr/bin/rsync -avh -r "$source" "$destination" &&
mv /Result/Failure_"$folderParam" /Result/Success_"$folderParam"
答案2
特登的回答更好,但如果您确实想使用历史运算符,您可以指定要引用哪个命令:
!rsync:3
将被替换为最后一次rsync
调用的第三个参数。所以就你而言,
folderParam=$(basename !rsync:3)