Bash脚本部署与编程全解析
1. 命令行参数的使用
在shell脚本中,我们可以使用命令行参数。例如 test4.sh
脚本:
#!/bin/bash
# Testing command line arguments
echo $1 checked in $2 days ago
使用以下命令为脚本添加执行权限并运行:
$ chmod u+x test4.sh
$ ./test4.sh Barbara 4
Barbara checked in 4 days ago
$ ./test4.sh Jessica 5
Jessica checked in 5 days ago
在这个脚本中, $1
变量保存人的名字, $2
变量保存他们签到的天数。需要注意的是,如果位置变量不存在,shell不会产生错误消息,但可能得不到预期的结果。例如:
$ ./test4.sh rich
rich checked in days ago
因此,需要在程序代码中检查位置变量是否存在。
2. 退出状态
当shell脚本结束时,它会向启动它的父shell返回一个退出状态。这个退出状态告诉我们脚本是否成功完成。Linux提供了特殊的 $?
变量,