拓展阅读

linux Shell 命令行-00-intro 入门介绍

linux Shell 命令行-02-var 变量

linux Shell 命令行-03-array 数组

linux Shell 命令行-04-operator 操作符

linux Shell 命令行-05-test 验证是否符合条件

linux Shell 命令行-06-flow control 流程控制

linux Shell 命令行-07-func 函数

linux Shell 命令行-08-file include 文件包含

linux Shell 命令行-09-redirect 重定向

场景

希望每次拷贝文件的不会覆盖。保留文件信息。

并且输出执行的结果。

  [sh]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 获取当前时间戳 timeStr=$(date +%Y%m%d%H%M%S) echo "time: $timeStr" # 备份文件 cp -f source.jar "bak_${timeStr}.jar" # 上一次执行结果 copyCode=$? if [ "$copyCode" -eq 0 ]; then echo 'copy success' else echo 'copy failed, copyCode=$copyCode' exit 1 fi

参考资料

https://www.runoob.com/linux/linux-shell.html