首页 > 代码库 > Linux Shell脚本编写——构建基本脚本(一)
Linux Shell脚本编写——构建基本脚本(一)
创建shell脚本
在创建shell脚本文件,必须在文件的第一行指定要使用的shell,其格式为:
#!/bin/bash
创建、执行sh脚本
代码1-1
root@lejian:/data# cat demo1 #/bin/bash date who root@lejian:/data# ls -l total 4 -rw-r--r-- 1 root root 21 Dec 1 13:59 demo1 root@lejian:/data# chmod u+x demo1 root@lejian:/data# ls -l total 4 -rwxr--r-- 1 root root 21 Dec 1 13:59 demo1 root@lejian:/data# ./demo1 Thu Dec 1 14:03:57 CST 2016 root pts/0 2016-12-01 13:34 (110.86.16.210)
代码1-1中,chmod u+x demo1 为修改demo1文件权限使其可执行
如果想在同一行显示一个文本字符串和一个命令的输出,可以用echo语句的-n参数,date必须换行,否则将不会打印日期还是打印date
代码1-2
root@lejian:/data# cat demo2 #!/bin/bash echo -n "The time and date are:" date who root@lejian:/data# ./demo2 The time and date are:Thu Dec 1 20:06:19 CST 2016 root pts/0 2016-12-01 19:59 (122.90.139.156)
使用环境变量,和转义美元$符号,如果要打印美元$需要进行转义,否则有可能打印为空或其他已定义的环境变量
代码1-3
root@lejian:/data# cat demo4 #!/bin/bash echo "User info for userid:$USER" echo "UID:$UID" echo "HOME:$HOME" echo "The cost of item is $15" echo "The cost of item is \$15" root@lejian:/data# ./demo4 User info for userid:root UID:0 HOME:/root The cost of item is 5 The cost of item is $15
用户变量
除了环境变量,shell脚本还允许在脚本中定义和使用自己的变量,定义变量允许临时存储数据并在整个脚本中使用
代码1-4
root@lejian:/data# cat demo5 #!/bin/bash days=19 guest="Tom" echo "$guest checked in $days days ago" days=25 guest="Amy" echo "$guest checked in $days days ago" customer=$guest echo "The customer is $customer" root@lejian:/data# ./demo5 Tom checked in 19 days ago Amy checked in 25 days ago The customer is Amy
反引号
shell脚本中反引号允许将shell命令的输出赋给变量
代码1-5
root@lejian:/data# cat demo6 #!/bin/bash testing=`date` echo "$testing" today=`date +%Y-%m-%d` echo "now is $today" today=`date +%H:%M:%S` echo "now is $today" today=`date "+%Y-%m-%d %H:%M:%S"` echo "now is $today" tomorrow=`date -d tomorrow "+%Y-%m-%d %H:%M:%S"` echo "tomorrow is $tomorrow" yesterday=`date -d yesterday "+%Y-%m-%d %H:%M:%S"` echo "yesterday is $yesterday" root@lejian:/data# ./demo6 Thu Dec 1 20:45:05 CST 2016 now is 2016-12-01 now is 20:45:05 now is 2016-12-01 20:45:05 tomorrow is 2016-12-02 20:45:05 yesterday is 2016-11-30 20:45:05
输出重定向
将命令的输出发送到一个文件中,bash shell采用(>)完成输出重定向,采用(>>)完成追加输出重定向
代码1-6
root@lejian:/data# date > demo7 root@lejian:/data# cat demo7 Thu Dec 1 20:52:09 CST 2016 root@lejian:/data# who > demo7 root@lejian:/data# cat demo7 root pts/0 2016-12-01 19:59 (122.90.139.156) root@lejian:/data# date >> demo7 root@lejian:/data# cat demo7 root pts/0 2016-12-01 19:59 (122.90.139.156) Thu Dec 1 20:52:32 CST 2016
输入重定向
将文件的内容重定向到命令,输入重定向的符号(<),内联输入重定向符号是双小于号(<<),除了这个符号,必须指定一个文件标记来划分输入数据的开始和结尾
wc命令是用来打印文件的文本行数、单词数、字节数等默认输出三个值
代码1-7
root@lejian:/data# wc <<EOF > Java > PHP > Python > EOF 3 3 16
管道
有时需要发送某个命令的输出作为另一个命令的输入
代码1-8
root@lejian:/data# cat demo9 hello Java hello Spring hello Mybatis PHP is the best language in the world Java is the best language in the world root@lejian:/data# cat demo9 | grep Java hello Java Java is the best language in the world
expr命令允许在命令上上处理数学表达式(注:数字和运算符必须有空格分隔),一些有特殊含义的符号(例如星号),可加上反斜杠转义
代码1-9
root@lejian:/data# expr 1 + 5 6 root@lejian:/data# expr 1 - 5 -4 root@lejian:/data# expr 1 * 5 expr: syntax error root@lejian:/data# expr 1 \* 5 5 root@lejian:/data# expr 1 / 5 0
将一个算术运算式的结果赋给一个变量,需要用引号括起来
代码1-10
root@lejian:/data# cat demo1 #!/bin/bash val1=20 val2=10 val3=`expr $val1 / $val2` echo "The result is $val3" root@lejian:/data# ./demo1 The result is 2
使用方括号计算
代码1-11
root@lejian:/data# val1=$[1+5] root@lejian:/data# echo $val1 6 root@lejian:/data# val2=$[$val1+10] root@lejian:/data# echo $val2 16 root@lejian:/data# cat demo2 #!/bin/bash val1=5 val2=8 val3=9 val4=1 val5=10 val6=2 val=$[val1+(val2-val3)+val4*val5+val6] echo "The result is $val" root@lejian:/data# ./demo2 The result is 16
bc计算机允许在命令行输入浮点表达式、解释表达式、计算并返回结果,bc计算器能识别:
- 数字(整数和浮点数)
- 变量(简单变量和数组)
- 注释(以#开始的行或C语言的/**/)
- 表达式
- 编程语句(if-then)
- 函数
代码1-12
root@lejian:/data# bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty‘. 9.1+11.2 20.3 8.1+9.9 18.0 quit
使用bc -q可以去掉提示语
代码1-13
root@lejian:/data# bc -q 3/5 0 scale=3 3/5 .600 val1=10 val2=6 val1*val2 60 val3=val1+val2 print val3 16 quit
脚本中使用bc
代码1-14
root@lejian:/data# cat demo3 #!/bin/bash val1=`echo "scale=3;3/6" | bc` echo "The result is $val1" root@lejian:/data# ./demo3 The result is .500
查看退出状态码
Linux提供$?变量来保存上个执行命令的退出状态码
代码1-15
root@lejian:/data# date Fri Dec 2 04:57:24 CST 2016 root@lejian:/data# echo $? 0 root@lejian:/data# aaa No command ‘aaa‘ found, did you mean: Command ‘aha‘ from package ‘aha‘ (universe) Command ‘aa‘ from package ‘astronomical-almanac‘ (universe) Command ‘ara‘ from package ‘ara‘ (universe) Command ‘jaaa‘ from package ‘jaaa‘ (universe) aaa: command not found root@lejian:/data# echo $? 127
状态码 | 描述 |
0 | 命令成功结束 |
1 | 通用未知错误 |
2 | 误用shell命令 |
126 | 命令不可执行 |
127 | 没找到命令 |
128 | 无效退出参数 |
128+x | Linux信号x的严重错误 |
130 | 命令通过Ctrl+C终止 |
255 | 退出状态码越界 |
exit退出命令可以指定退出状态码,但不能超过255,否则会取模
代码1-16
root@lejian:/data# cat demo4 #!/bin/bash date echo $? who exit 5 root@lejian:/data# ./demo4 Fri Dec 2 05:03:08 CST 2016 0 root pts/0 2016-12-02 04:39 (122.91.222.126) root@lejian:/data# echo $? 5
Linux Shell脚本编写——构建基本脚本(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。