本节所讲内容
shell 基本语法
变量
表达式
判断语句
if表达式
先看一个简单的shell程序
[root@server63test]# chmod +x example01.sh
[root@server63 test]# cat example01.sh
#!/bin/bash
#Thisis to show what a example looks like.
echo"Our first example"
echo# This inserts an empty line in output
echo"We are currently in the following directory."
pwd
echo
echo"This directory contains the following files."
ls
注释
#!/bin/bash #! 跟 shell命令的完整路径 作用:显示后期命令以哪种shell来执行这些命令
如不指定以当前shell 执行
#Thisis to show what a example looks like. shell中以#开头表示注释 执行时被忽略
shell程序 一般以.sh 结尾
总结
创建shell程序的步骤
第一步: 创建一个包含命令和控制结构的shell文件
第二步: 修改这个文件的权限使它可以执行
使用 chmod u+x
第三步:执行1 ./example01.sh
方法2 使用绝对路径/root/test/example01.sh
方法3 bash example01.sh
shell变量
变量是shell传递数据的一种方法。变量是用来代表每个值的符号名
例:x=3 x代表3
shell有两种变量: 临时变量和永久变量
临时变量:是shell程序内部定义的,其使用范围仅限于定义它的程序,对其他程序不可见。包括用户自定义变量,位置变量和预定变量。
永久性变量 是环境变量,其值不随shell脚本的执行结束而消失
例:
如 $PATH
[root@server63test]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
#用作运行某个命令的时候,本地查找不到某个命令,会到这个声明的目录中去查找
为什么ls可以执行 因为 ls是别名 /bin/ls, /bin/ls 已写在$PATH中
[root@server63test]# which ls
aliasls='ls --color=auto'
/bin/ls
用户自定义的变量:由字母或下划线开头,由字母、数字或下划线序列组成,并且大小写字母意义不同。变量名长度没有限制。
使用变量值时,要在变量名前加上前缀“$”
例
例如 1var 是非法变量 以数字开头
变量赋值: 赋值号 “=” 两边不能有空格
[root@server63test]#
[root@server63 test]# A=aaa
[root@server63 test]# A = aaa
bash:A: command not found
例:将一个命令的执行结果赋给变量
[root@server63 test]# A=`date` 反引号中接可执行的命令 将shell命令中的输出赋值给变量
[root@server63 test]# echo $A
ThuMay 12 17:15:15 CST 2016
[root@server63test]# B=$(ls -l)
[root@server63test]# echo $B
total4 -rwxr-xr-x. 1 root root 241 May 12 16:28 example01.sh
可以利用变量和其他字符组成一个新的字符串
[root@server63test]# MYDIR=/home/zhh/
[root@server63test]# echo $MYDIR/zhangsan
/home/zhh//zhangsan
[root@server63test]# DAY=mon
[root@server63test]# echo Tody is $DAY day
Todyis mon day
[root@server63test]# echo Today is ${DAY}day
Todayis monday
列出所有的变量:
set 命令
[root@server63test]# set | grep DAY
DAY=mon
给变量赋值多个单词
bash:Ron: command not found
[root@server63test]# NAME="mike Ron"
[root@server63test]# echo $NAME
mikeRon
[root@server63test]# name='jj dd'
[root@server63test]# echo $NAME
mikeRon
单引号和双引号的区别:
单引号之间的内容原封不动的输出给变量
双引号取消了空格的作用,特殊符号的含义保留
[root@server63~]# NAME="aaa bbb"
[root@server63~]# echo $NAME
aaabbb
[root@server63~]# NAME="mike jone $NAME"
[root@server63~]# echo $NAME
mikejone aaa bbb
[root@server63~]# NAME='mike jone $NAME'
[root@server63~]# echo $NAME
mikejone $NAME
删除变量: unset
[root@server63~]# NAME="john aa"
[root@server63~]# echo $NAME
johnaa
[root@server63~]# unset NAME
[root@server63~]# echo $NAME
位置变量和特殊变量
位置变量:shell解释执行用户的命令时,讲命令行的第一个字作为命令名,而其他字作为参数。由出现在命令行上的位置确定的参数称为位置参数
ls-l ls 命令 -l 位置参数
位置变量:使用$N 来表示
[root@server63~]# ./example.sh file1 file2 file3
位置变量用$N 来表示
$0 这个程序的文件名 example.sh
$N这个程序的第n个参数 n=1...N
[root@server63~]# cat example.sh.sh
#!/bin/bash
echo"$1"
[root@server63 ~]# ./1.sh /etc/passwd /etc/hosts
/etc/passwd
/etc/hosts
特殊变量
有些变量是一开始执行Script脚本时就会设定,且不能被修改,但我们不叫它只读的系统变量,而叫它特殊变量。这些变量当一执行程序时就有了,一下是一些特殊变量:
$* 这个程序的所有参数
$# 这个程序的参数个数
$$ 这个程序PID
$! 执行上一个后台程序的PID
$? 执行上一个质量的返回值
[root@xuegod63~]# cat a.sh
#!/bin/bash
echo"$* Represents all parameters of this program"
echo"$# Number of parameters representing the program"
touch/tmp/a.txt
echo"$$ The program process ID"
touch/tmp/b.txt &
echo"$! Execution on a backgroundinstruction PID"
[root@xuegod63~]# cat a.sh
#!/bin/bash
echo"$* 表示这个程序的所有参数"
echo"$# 表示这个程序的参数个数"
touch/tmp/a.txt
echo"$$ 表示程序的进程ID"
touch/tmp/b.txt &
echo"$! 执行上一个后台指令的PID"
[root@xuegod63~]# sh a.sh
表示这个程序的所有参数
0表示这个程序的参数个数
24354 表示程序的进程ID
24356 执行上一个后台指令的PID
[root@xuegod63~]# chmod +x a.sh
[root@xuegod63~]# ./a.sh aaa bbb ccc
aaabbb ccc 表示这个程序的所有参数
3表示这个程序的参数个数
24429 表示程序的进程ID
24431 执行上一个后台指令的PID
例:变量在shell中的使用
[root@xuegod63~]# cat b.sh
#!/bin/bash
var1="abcdefg"
echo$var1
var2=1234
echo"The value of var2 is $var2"
echo$HOME
echo$PATH
echo$PWD
read命令
作用:从磁盘读入数据,赋值给变量
[root@xuegod63~]# read a b c
12 3
[root@xuegod63~]# echo $a $b $c
12 3
[root@xuegod63~]# echo $a
1
例:在shell中用read命令
[root@xuegod63~]# cat read.sh
#!/bin/bash
echo"input first second third:"
read first second third
echo"the first parameter is $frist"
echo"the second parameter is $second"
echo"the third parameter is $third"
[root@xuegod63~]# ./read.sh
ab c
thefirst parameter is
thesecond parameter is b
thethird parameter is c
expr 命令
作用: shell变量的算术运算:
expr命令:对整数型变量进行算术运算
语法: expr 表达式 # 注意 运算符之间要有空格
[root@xuegod63~]# expr 3 + 5
8
[root@xuegod63~]# var1=8
[root@xuegod63~]# var2=2
[root@xuegod63~]# expr $var1 - 5
3
[root@xuegod63~]# expr $var1 / $var2
4
[root@xuegod63~]# expr $var1 * $var2
expr:syntax error
[root@xuegod63~]# expr $var1 \* $var2 \ 转译
16
expr程序例子
[root@xuegod63~]# cat expr.sh
#!/bin/bash
a=10
b=20
c=30
value1=`expr$a + $b + $c`
echo"The value of value1 is $value1"
value2=`expr$c / $b`
echo"The value of value2 is $value2"
value3=`expr$c \* $b` #整除的意思
echo"The value of value3 is $value3"
value4=`expr$a + $c / $b`
echo"The value of value4 is $value4"
[root@xuegod63~]# chmod +x expr.sh
[root@xuegod63~]# ./expr.sh
Thevalue of value1 is 60
Thevalue of value2 is 1
Thevalue of value3 is 600
Thevalue of value4 is 11
复杂的运算:
[root@xuegod63~]# expr `expr 5 + 11` / $var4
2
变量测试语句:
test
格式
test 测试条件
测试范围:整数,字符串,文件
字符串和变量
test$str1 == $str2 是否相等
test$str1!= $str2 是否不相等
test $str1 测试字符串是否不空 如果$str1不为空 则返回结果为真
test-n $str1 测试字符串是否不为零 如果字符串长度不为零 则返回结果为真
或
test -z $str1 测试字符串长度为零 如果字符串长度为零 则返回结果为真
测试整数:
test int1 -eq int2 =
test int1 -ge int2 >=
test int1 -gt int2 >
test int1 -le int2 <=
test int1 -lt int2 <
说明:也可以省略 test 写成 [int1 -lt int2]
文件测试:
test-d file #测试是否为目录
test -f file #测试是否为文件
test -r/w/x #测试权限
test -s file #测试大小为空。是否为空文件
说明:
test-x file 简写成 [-x file]
跟流程控制结合使用
if语句
语法:
if 条件
then
语句
fi
扩展 ; 分号 表示两个命令写在一起。互不影响。 &&是前面执行成功才执行后面的
[root@xuegod64 ~]# ls /oppt && ls
ls: cannot access /oppt: No such file ordirectory
[root@xuegod64 ~]# ls /oppt ; ls
ls: cannot access /oppt: No such file ordirectory
anaconda-ks.cfg install.log install.log.syslog key
[root@xuegod64 ~]# cat if.sh
#!/bin/bash
echo "if test"
if [ -x /bin/ls ] ; then
/bin/ls
fi
echo "+++++++++++++++++++++++++++++"
[root@xuegod64 ~]# ./if.sh
if test
if.sh
+++++++++++++++++++++++++++++
if /else 用法:
语法:
if 条件1 ; then
命令1
else
命令2
fi
[root@xuegod64 ~]# vim else.sh
#!/bin/bash
if [ -x /etc/passwd ] ; then
/bin/ls
else
pwd
fi
[root@xuegod64 ~]# ./else.sh
/root
[root@xuegod64 ~]# chmod +x /etc/passwd
[root@xuegod64 ~]# ./else.sh
else.sh if.sh
多个条件的联合
-a 或 && : 逻辑与,仅当两个条件都成立时 结果为真
-o 或 ll : 逻辑或。两个条件有一个成立,结果为真
更复杂的if语句
语法:
if 条件1 ; then
命令1
elif 条件2 ; then
命令2
elif 条件3 ; then
命令3
else
命令4
fi
综合实例 : 测试文件类型
[root@xuegod64 ~]# cat elif.sh
#!/bin/bash
echo "input a file name:"
read file_name
if [ -d $file_name ] ; then
echo "$file_name is a dir"
elif [ -f $file_name ] ; then
echo "$file_name is a file"
elif [ -c $file_name -o -b $file_name ] ; then
echo "$file_name is a device file"
else
echo "$file_name is an unknow file"
fi
brw-rw----. 1 root disk 8, 16 May 20 09:16/dev/sdb
[root@xuegod64 ~]# ll /dev/ | grep c
crw-rw----. 1 root video 10, 175 May 20 09:16 agpgart
crw-rw----. 1 root root 10, 57 May 20 09:16 autofs