gpt4 book ai didi

shell - 验证复制成功

转载 作者:行者123 更新时间:2023-12-04 02:32:02 25 4
gpt4 key购买 nike

我想编写一个脚本来验证复制是否成功。
这是我所拥有的:

#!/bin/sh
cp home/testing/present.txt home/testing/future.txt
echo "Copy Code: $? - Successful"
if [ $? != 0 ]; then
echo "Copy Code: $? - Unsuccessful"
fi

“if”语句没有被初始化。如何解决这个问题?
感谢您的时间。

最佳答案

$?指的是最后一条命令:

#!/bin/sh
cp home/testing/present.txt home/testing/future.txt
echo "Copy Code: $? - Successful" # last command: cp
if [ $? != 0 ]; then # last command: echo
echo "Copy Code: $? - Unsuccessful" # last command: [
fi

如果要重复处理特定命令的状态,只需将结果保存在另一个变量中:
#!/bin/sh
cp home/testing/present.txt home/testing/future.txt
status=$?
echo "Copy Code: $status - Successful"
if [ $status != 0 ]; then
echo "Copy Code: $status - Unsuccessful"
fi

但是,更好的方法是简单地测试 cp首先命令:
if cp home/testing/present.txt home/testing/future.txt
then
echo "Success"
else
echo "Failure, exit status $?"
fi

关于shell - 验证复制成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29038653/

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