Linux shell 脚本文件命令
shell 脚本文件开头
注释格式
1 2 3 4 5 6 7 8 9 10
| #单行注释 用#
#多行注释 :<<! # 被注释的内容 !
##### echo 输出文字内容 ```shell echo "hello world !"
|
#作为可执行程序
1 2
| chmod +x ./test.sh #使脚本具有执行权限 ./test.sh #执行脚本
|
变量和字符串拼接
1
| docker commit ${name} ${name}-backup
|
获取上一条命令的执行结果
1 2 3 4 5 6 7
| var=`ps -ef | grep java` 或 var=$(ps -ef | grep java) echo "var = $var"
#输出docker image id 并赋值给var变量 var=`docker images -q bop-fms-gateway-backup`; echo "id=$var"
|
shell 数组
1 2 3 4 5 6 7 8
| #使用@ 或 * 可以获取数组中的所有元素 my_array=(A B "C" D) echo "第1个元素为: ${my_array[0]}" echo "第2个元素为: ${my_array[1]}" echo "第3个元素为: ${my_array[2]}" echo "第4个元素为: ${my_array[3]}" echo "数组的元素为: ${my_array[*]}" echo "数组元素个数为: ${#my_array[*]}"
|
shell 传递参数
https://www.runoob.com/linux/linux-shell-passing-arguments.html
shell 日期变量
1 2 3 4 5 6 7 8 9 10
| #将时间 时 分 秒 赋给变量curTime,注意 date 和 双引号之间有空格 curTime=$(date "+%H%M%S") #将时间 年 月 日 赋给变量curDate curDate=$(date "+%Y%m%d") var=`date "+%Y-%m-%d %H:%M:%S"`
#创建以当前时间为文件名的文件夹 mkdir `date +%Y%m%d` #备份以时间做为文件名的文件 tar cvf ./htdocs`date +%Y%m%d`.tar ./*
|
if else
1
| if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
|
shell test 命令
https://www.runoob.com/linux/linux-shell-test.html
shell 输入/输出重定向
https://www.runoob.com/linux/linux-shell-io-redirections.html
shell 文件包含
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #创建两个 shell 脚本文件。 #test1.sh 代码如下:
#!/bin/bash url="http://www.runoob.com"
#test2.sh 代码如下:
#!/bin/bash #使用 . 号来引用test1.sh 文件 . ./test1.sh # 或者使用以下包含文件代码 # source ./test1.sh echo "菜鸟教程官网地址:$url" #接下来,我们为 test2.sh 添加可执行权限并执行: chmod +x test2.sh ./test2.sh #输出: 菜鸟教程官网地址:http://www.runoob.com
|
若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏