shell – 验证副本是否成功

前端之家收集整理的这篇文章主要介绍了shell – 验证副本是否成功前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想编写一个脚本来验证副本是否成功.
这就是我所拥有的:
  1. #!/bin/sh
  2. cp home/testing/present.txt home/testing/future.txt
  3. echo "Copy Code: $? - Successful"
  4. if [ $? != 0 ]; then
  5. echo "Copy Code: $? - Unsuccessful"
  6. fi

“if”语句未初始化.怎么解决这个?
感谢您的时间.

$?指的是最后一个命令:
  1. #!/bin/sh
  2. cp home/testing/present.txt home/testing/future.txt
  3. echo "Copy Code: $? - Successful" # last command: cp
  4. if [ $? != 0 ]; then # last command: echo
  5. echo "Copy Code: $? - Unsuccessful" # last command: [
  6. fi

如果要重复使用特定命令的状态,只需将结果保存在另一个变量中:

  1. #!/bin/sh
  2. cp home/testing/present.txt home/testing/future.txt
  3. status=$?
  4. echo "Copy Code: $status - Successful"
  5. if [ $status != 0 ]; then
  6. echo "Copy Code: $status - Unsuccessful"
  7. fi

但是,更好的方法是首先简单地测试cp命令:

  1. if cp home/testing/present.txt home/testing/future.txt
  2. then
  3. echo "Success"
  4. else
  5. echo "Failure,exit status $?"
  6. fi

猜你在找的Bash相关文章