Linux Shell脚本编写——构建基本脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux Shell脚本编写——构建基本脚本相关的知识,希望对你有一定的参考价值。

创建shell脚本

在创建shell脚本文件,必须在文件的第一行指定要使用的shell,其格式为:

#!/bin/bash

创建、执行sh脚本

代码1-1

[email protected]:/data# cat demo1 
#/bin/bash
date
who

[email protected]:/data# ls -l
total 4
-rw-r--r-- 1 root root 21 Dec  1 13:59 demo1
[email protected]:/data# chmod u+x demo1 
[email protected]:/data# ls -l
total 4
-rwxr--r-- 1 root root 21 Dec  1 13:59 demo1
[email protected]:/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
[email protected]:/data# cat demo2 
#!/bin/bash
echo -n "The time and date are:"
date
who
[email protected]:/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
[email protected]:/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"
[email protected]:/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
[email protected]:/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"
[email protected]:/data# ./demo5 
Tom checked in 19 days ago
Amy checked in 25 days ago
The customer is Amy

  

反引号
shell脚本中反引号允许将shell命令的输出赋给变量
代码1-5
[email protected]:/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"
[email protected]:/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
[email protected]:/data# date > demo7
[email protected]:/data# cat demo7 
Thu Dec  1 20:52:09 CST 2016
[email protected]:/data# who > demo7 
[email protected]:/data# cat demo7 
root     pts/0        2016-12-01 19:59 (122.90.139.156)
[email protected]:/data# date >> demo7 
[email protected]:/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
[email protected]:/data# wc <<EOF
> Java
> php
> Python
> EOF
 3  3 16

  

管道
有时需要发送某个命令的输出作为另一个命令的输入
代码1-8
[email protected]:/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
[email protected]:/data# cat demo9 | grep Java
hello Java
Java is the best language in the world

  

expr命令允许在命令上上处理数学表达式(注:数字和运算符必须有空格分隔),一些有特殊含义的符号(例如星号),可加上反斜杠转义
代码1-9
[email protected]:/data# expr 1 + 5
6
[email protected]:/data# expr 1 - 5
-4
[email protected]:/data# expr 1 * 5
expr: syntax error
[email protected]:/data# expr 1 \* 5
5
[email protected]:/data# expr 1 / 5
0

  

将一个算术运算式的结果赋给一个变量,需要用引号括起来
代码1-10
[email protected]:/data# cat demo1 
#!/bin/bash
val1=20
val2=10
val3=`expr $val1 / $val2`
echo "The result is $val3"
[email protected]:/data# ./demo1 
The result is 2

  

使用方括号计算

代码1-11

[email protected]:/data# val1=$[1+5]
[email protected]:/data# echo $val1
6
[email protected]:/data# val2=$[$val1+10]
[email protected]:/data# echo $val2
16
[email protected]:/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"
[email protected]:/data# ./demo2 
The result is 16

  

bc计算机允许在命令行输入浮点表达式、解释表达式、计算并返回结果,bc计算器能识别:

  • 数字(整数和浮点数)
  • 变量(简单变量和数组)
  • 注释(以#开始的行或C语言的/**/)
  • 表达式
  • 编程语句(if-then)
  • 函数

代码1-12

[email protected]:/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
[email protected]:/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
[email protected]:/data# cat demo3 
#!/bin/bash
val1=`echo "scale=3;3/6" | bc`
echo "The result is $val1"
[email protected]:/data# ./demo3 
The result is .500

  

查看退出状态码
Linux提供$?变量来保存上个执行命令的退出状态码

代码1-15

[email protected]:/data# date
Fri Dec  2 04:57:24 CST 2016
[email protected]:/data# echo $?
0
[email protected]:/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
[email protected]:/data# echo $?
127

  

linux退出状态码
状态码 描述
0 命令成功结束
1 通用未知错误
2 误用shell命令
126 命令不可执行
127 没找到命令
128 无效退出参数
128+x Linux信号x的严重错误
130 命令通过Ctrl+C终止
255 退出状态码越界

 

exit退出命令可以指定退出状态码,但不能超过255,否则会取模

代码1-16

[email protected]:/data# cat demo4 
#!/bin/bash
date
echo $?
who
exit 5
[email protected]:/data# ./demo4 
Fri Dec  2 05:03:08 CST 2016
0
root     pts/0        2016-12-02 04:39 (122.91.222.126)
[email protected]:/data# echo $?
5

  

 


以上是关于Linux Shell脚本编写——构建基本脚本的主要内容,如果未能解决你的问题,请参考以下文章

shell--构建基本脚本

linux下shell脚本命令

Linux Shell脚本编写——使用结构化命令

如何编写一个shell脚本

Linux命令 linux下高效编写shell脚本的10个建议

Linux下高效编写shell脚本的10个建议