Linux下的shell脚本编程-变量-算术表达式-判断语句-if分支语句 一:实验环境
1):虚拟机 2):linux系统 二:实验目标
1): shell 基本语法 2):变量 3):表达式 4):判断语句 5): if表达式 三:实验脚本
第一块
一个简单的shell脚本程序
[iyunv@localhost ~]# mkdir test
[iyunv@localhost test]# vim example1.sh
#!/bin/bash
#This is to show what a example looks like.
s is 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 followingdirectory."
echo "This directory contains the followingfiles"
注释:
#!/bin/bash #“!”后面跟shell命令的绝对路径。 作用:显示后期命令以哪种shell来执行这些命令。如不指shell,以当前shell作为执行的shell。
[iyunv@xuegod63test]# ll /bin/sh
lrwxrwxrwx. 1root root 4 Dec 18 2012 /bin/sh -> bash
#This is to showwhat a example looks like. #以shell中以#开始头表示整行都被当作一个注释。执行时被忽略。
shell程序一般以.sh结尾
[iyunv@localhost test]# ll example1.sh
-rw-r--r--. 1 root root 280 7月 28 19:12example1.sh
[iyunv@localhost test]# chmod u+xexample1.sh
[iyunv@localhost test]# ll example1.sh
-rwxr--r--. 1 root root 280 7月 28 19:12 example1.sh #显示绿色的是一个shell脚本
[iyunv@localhost test]# ./example1.sh #执行刚编写好的shell
./example1.sh: line 3: s: command not found
Our first example
We are currently in the following directory.
/root/test
This directory contains the following files
总结:
创建shell程序的步骤:
第一步:创建一个包含命令和控制结构的shell文件。
第二步:修改这个文件的权限使它可以执行。
使用chmod u+x
第三步:执行
方法1:[iyunv@localhost test]# ./example1.sh
方法2:[iyunv@localhost test]# /root/test/example1.sh
方法3:[iyunv@localhost test]# bash example1.sh
注:前两种方法都是需要脚本具有执行权限的,第三种方法是不需要脚本具有执行权限的。
第二块
shell变量
作用:变量是shell 传递数据的一种方法。变量是用来代表每个值的符号名。
Shell 有两类变量:临时变量和永久变量。
临时变量:是shell 程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。
临时变量用户定义:由字母或下划线打头。由字母、数字或下划线组成,并且大小写字母意义不同。变量名长度没有限制。使用变量值时,要在变量名前加上前缀“$”。
例:1VAR 是非法变量。
永久变量:是环境变量,其值不随shell 脚本的执行结束而消失。
永久变量如:$PATH
[iyunv@localhost~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
#用作运行某个命令的时候,本地查找不到某个命令或文件,会到这个声明的目录中去查找。
变量赋值:赋值号“=”两边应没有空格。
1:
[iyunv@localhost~]# A=122
[iyunv@localhost~]# A = 123
bash: A: command not found
命令的执行结果赋给变量
1:
[iyunv@localhost ~]# A=`date` #将date时间赋值给A
[iyunv@localhost ~]# echo $A
2015年 07月 29日星期三 07:57:00 CST
#用date命令查看时间,将date值赋值给A之后,A就具有了date的功能
2:
[iyunv@localhost test]# ls -a
. .. example1.sh
[iyunv@localhost test]# C=$(ls-a) #将命令ls –a 赋值给C
[iyunv@localhost test]# echo$(ls -a)
. .. example1.sh:
3:
[iyunv@localhost test]# A=$C #也可以将C值赋值给A值
[iyunv@localhost test]# echo $A
. .. example1.sh
可以利用变量和其它字符组成一个新的字符串。
1:
[iyunv@localhost ~]# useradd zy
[iyunv@localhost test]#MYDIR=/home/zy
[iyunv@localhost test]# echo$MYDIR/yu
/home/zy/yu
2:
[iyunv@localhost test]# DAY=mon #将mon赋值给DAY
[iyunv@localhost test]# echoTaday is ${DAY}day #输出Taday is Monday
Taday is Monday
#我们是将mon赋值给DAY的,那么输出也该是mon,为什么输出的是Mon呢?
在英语中的语法是星期的每个单词的第一个字母都是大写,
列出所有的变量:
set 命令
1:
[iyunv@localhost test]# set |grep DAY
DAY=mon
给变量赋值多个单词:
1:
[iyunv@localhost test]# NAME="jake Ron"
[iyunv@localhost test]# echo$NAME
jake Ron
[iyunv@localhost test]# NAME='yu zy'
[iyunv@localhost test]# echo$NAME
yu zy
#在这里,我们可以看到单引号,和双引号的效果是一样的。
那么它们就没有区别吗?
单引号之间的内容原封不动地指定给了变量。
双引号取消了空格的作用,特殊符号的含义保留。
单引号与双引号的区别
[iyunv@localhost test]# NAME="jake Ron $NAME"
[iyunv@localhost test]# echo$NAME
jake Ron yu zy
#这个时候,是讲特殊符号的含义保留 “¥NAME“就是讲上一个的赋值变量直接运用。
[iyunv@localhost test]# NAME='jake Ron $NAME'
[iyunv@localhost test]# echo$NAME
jake Ron $NAME
#我们看到了,单引号下的特殊符号的含义是不保留的,在这里不显示任何意义。
删除变量:
[iyunv@localhost test]# unsetNAME
[iyunv@localhost test]# echo$NAME
-----这里什么都没有显示------
[iyunv@localhost test]#
位置变量和特殊变量
位置变量:Shell解释执行用户的命令时,将命令行的第一个字作为命令名,而其它字作为参数。由出现在命令行上的位置确定的参数称为位置参数。
位置变量:使用$N 来表示,N表示number数字
例:
[iyunv@xuegod63 test]#./example.sh file1 file2 file3
$0 这个程序的文件名 example.sh
$n 这个程序的第n个参数值,n=1..N
特殊变量:
有些变量是一开始执行Script脚本时就会设定,且不能被修改,但我们不叫它只读的系统变量,而叫它特殊变量。这些变量当一执行程序时就有了,以下是一些等殊变量:
$* 这个程序的所有参数
$# 这个程序的参数个数
$$ 这个程序的PID
$! 执行上一个后台程序的PID
$? 执行上一个指令的返回值
小综合实例:
[iyunv@localhost test]# vima.sh
#!/bin/bash
echo "$* 表示这个程序的所有参数 "
echo "$# 表示这个程序的参数个数"
touch /tmp/a.txt
echo "$$ 表程序的进程ID "
touch /tmp/b.txt &
echo "$! 执行上一个后台指令的PID"
echo "$$ 表程序的进程ID "
[iyunv@localhost test]# chmodu+x a.sh
[iyunv@localhost test]# ll a.sh
-rwxr--r--. 1 root root247 7月 29 09:03 a.sh
[iyunv@localhost test]# ./a.shabc 123
abc 123 表示这个程序的所有参数
2 表示这个程序的参数个数
4994 表程序的进程ID
4996 执行上一个后台指令的PID
4994 表程序的进程ID
例: 变量在shell中的使用
[iyunv@localhost test]# vima1.sh
#!/bin/bash
echo $var1
var2=1234
echo "The value of var2 is $var2"
echo $HOME
echo $PATH
echo $PWD
[iyunv@localhost test]# chmod+x a1.sh
[iyunv@localhost test]# ./a1.sh
The value of var2 is 1234 # echo"The value of var2 is $var2"
/root # echo $HOME
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
# echo $PATH
/root/test # echo $PWD
Read命令:
作用:从键盘读入数据,赋给变量
例1:
[iyunv@localhost test]# read ab c
1 2 3
#读入数据1 2 3 将数据赋值给变量a b c 。
[iyunv@localhost test]# echo $a$b $c
1 2 3
例2
[iyunv@localhost test]# vimread.sh #编辑一个read脚本
#!/bin/bash
echo "input first second third :" #在此脚本中输入三个变量
read first second third #读取三个变量
echo "the first parameter is$first" #输出第一个变量的数据
echo "the second parameter is $second"#输出第二个变量的数据
echo "the third parameter is$third" #输出第三个变量的数据
[iyunv@localhost test]# chmod+x read.sh #给此read脚本添加执行权限
[iyunv@localhost test]#./read.sh #执行read脚本
input first second third :
1 2 3 #对三个变量进行赋值
the first parameter is 1 #输出第一个变量所赋的值,即数据
the second parameter is 2 #输出第二个变量所赋的值,即数据
the third parameter is 3#输出第三个变量所赋的值,即数据
expr 命令
作用:Shell变量的算术运算:
expr命令:对整数型变量进行算术运算
语法: expr 表达式 #注意 运算符之间要有空格
1:例子
[iyunv@localhost test]# expr2000 + 5000
7000
#这是最简单的使用expr功能,:
2:使用 变量实现expr功能
[iyunv@localhost test]#var1=100 #var=变量
[iyunv@localhost test]#var2=1000
[iyunv@localhost test]# expr$var1 / $var2
0
[iyunv@localhost test]# expr$var2 / $var1
10
[iyunv@localhost test]# expr$var2 - 500
500
[iyunv@localhost test]# expr$var2 * 500
expr: 语法错误
[iyunv@localhost test]# expr$var2 \* 500
500000
#在linux中”* ”表示特殊符号,具有特殊的含义,因此直接用“ * ”是不能够表示乘的意思的。但是在之前加一个“ \ ”之后就可以表示乘的意思了,此反斜杠表示是转义的意思,将“ * ”的特殊含义转换成乘的意思。
3:使用shell实现expr的功能
[iyunv@localhost test]# vimexpr.sh #创建并打开exprshell脚本
#! /bin/sh
a=10
b=20
c=30
value1=`expr $a + $b + $c` #属性1是a b c 三个变量赋值的相加
echo"The value of value1 is $value1"
value2=`expr $c / $b`
echo"The value of value2 is $value2" #在 linux下的除法是只取整数的
value3=`expr $c \* $b`
echo"The value of value3 is $value3"
value4=`expr $a + $c / $b`
echo"The value of value4 is $value4"
[iyunv@localhost test]# ls
a1.sh a.sh example1.sh expr.sh read.sh
[iyunv@localhost test]# chmod+x expr.sh
[iyunv@localhost test]#./expr.sh
The value of value1 is 60
The value of value2 is 1
The value of value3 is 600
The value of value4 is 11
#在linux中也是遵循数学的先乘除后加减的运算规则的
4:复杂的运算
[iyunv@localhost test]# var4=8
[iyunv@localhost test]# expr`expr 5 + 11` / $var4
2
[iyunv@localhost test]# var2=4
[iyunv@localhost test]# var3=8
[iyunv@localhost test]#var4=`expr $var3 \* $var2 / $var1 \* $var2` #这里是将运算结果赋值给变量4
[iyunv@localhost test]# echo$var4 #那么这里只需要输出变量4就OK了
64
变量测试语句:
test
格式:
test 测试条件
测试范围: 整数,字符串,文件
字符串和变量:
test $str1 == $str2 是否相等
test $str1!= $str2 是否不相等
test $str1 如果$str1不为空,则返回结果为真
test -n $str1如果字符串长度为不零,则返回结果为真
或
test -z $str1 如果字符串长度为零,则返回结果为真
大于等于小于:测试整数:
test int1 -eq int2 = Is equal to the
test int1 -gt int2 > Is greaterthan
test int1 -ge int2 >= Greater than or equal to
test int1 -lt int2 < Less than
test int1 -le int2 <= Less than or equalto
test int1 -ne int2 != Is not equal to
说明:也可以省略test 写成: [ int1 -lt int2 ]
文件测试:
test -d file #测试是否为目录
test -f file #测试是否为文件
test -x file #测试是否有可执行权限
test -r file #测试是否为有可读权限
test -w file #测试文件是否有可写权限
test -e file #测试文件是否存在
test -s file #测试大小是为空。是否为空文件
说明:
test -x file 简写成: [ -x file ]
if 语句
语法:if 条件为真就执行命令,为假就直接退出(then)
步骤:
1:if 条件--判断
2:then
3:执行的语句
4:fi—结束
例:
[iyunv@localhost ~]# vim if.sh
#!/bin/bash #表示的是所调用的shell环境,默认是/bin/bash
echo "if test" #这个就是往屏幕上输出if test ,相当于执行脚本之后所看到的提示,能让人明白这个脚本是干什么的
if [ -x /bin/ls ] then /bin/ls
fi #if语句结束
[iyunv@localhost ~]# chmod +xif.sh
[iyunv@localhost ~]# ./if.sh
if test #看到这个,就明白这个脚本是干什么的了
anaconda-ks.cfg install.log test
if.sh install.log.syslog
注释:
#if语句,条件判断,容错处理,【-x/bin/ls】 判断/bin/ls的ls命令是否具有可执行权限。
#then
/bin/ls 指如果/ls命令具有执行权限的话就直接跳到下一行执行/bin/ls命令,如果没有的话就直接退出。
注:if [ -x /bin/ls ] ; then #也可以写成一行,但是中间要加上“;”
扩展
“;” 分号,表示两个两个命令写在一行。互不影响。
“&&”and号,便是前后两个命令写在一行,互有影响
例:
[iyunv@localhost ~]# ls /optt/ && ls #故意将查看opt命令打错
ls: 无法访问/optt/: 没有那个文件或目录
#&&下命令之后,查看时,有一个命令出错,则所有命令都出错
[iyunv@localhost ~]# ls /optt/ ; ls
ls: 无法访问/optt/: 没有那个文件或目录
anaconda-ks.cfg install.log.syslog
install.log test
#“;”命令之后,即使第一个命令出错,但第二个命令任然成功。
if /else 用法:
语法:
步骤
if 条件1 ;then
命令1
else
命令2:
fi
例:
[iyunv@localhost ~]# ll/etc/passwd
-rw-r--r--. 1 root root1889 7月 29 08:17 /etc/passwd
#这里可以看到/passwd命令是不具有执行权限的
[iyunv@localhost ~]# vimelse.sh
#! /bin/bash
echo"====if/else===="
if [ -x /etc/passwd ]
then
/bin/ls
else
pwd
fi
[iyunv@localhost ~]# chmod +xelse.sh
[iyunv@localhost ~]# ./else.sh
====if/else====
/root
#输出了/root命令,表示了/passwd没有执行权限。
注释:
1:#if[ -x /etc/passwd ] 指if语句中,判断/passwd命令是否具有执行权限
2:#then /bin/ls 指如果/passwd具有执行权限,就执行/ls命令。
3:#else pwd 指/passwd没有执行权限,不再直接退出,而是执行else下的pwd命令
当/passwd具有执行权限时
[iyunv@localhost ~]# chmod +x/etc/passwd
[iyunv@localhost ~]# ll/etc/passwd
-rwxr-xr-x. 1 root root1889 7月 29 08:17 /etc/passwd
[iyunv@localhost ~]# ./else.sh
====if/else====
anaconda-ks.cfg if.sh install.log.syslog 公共的 视频 文档 音乐
else.sh install.log test 模板 图片 下载 桌面
#这里输出的就是/ls命令后的
综合实例: 测试文件类型
多个条件的联合
-a 或 && : 逻辑与,当两个条件都为真时,结果才为真,有一个假即为假
-o 或 || : 逻辑或 ,当两个条件都为假时,结果才为假,有一个真即为真
更复杂的if语句:
步骤:
if 条件1 ; then
命令1
elif 条件2 ; then
命令2:
elif 条件3 ; then
命令3
else
命令4
fi
例子
[iyunv@localhost test]# vimelif.sh
#! /bin/bash
echo "input a filename:"
read file_name #
if [ -d $file_name ]
then #判断$file_name是否为目录,是就执行以下“$file_nameis a dir”命令
echo " $file_name is a dir"
elif [ -f $file_name ]
then #指$file_name不是目录,就判断它是否为文件,是就执行以下命令" $file_name is file"
echo " $file_name is file"
elif [ -c $file_name -o -b$file_name ]
then#指$file_name类型不是文件,就判断它的类型是否为c类型的字符设备文件或者是否为
blog文件,是两种文件中的一种就执行以下命令" $file_name is file"
echo " $file_name is a device file"
else #指此文件都不是以上的所有类型文件,就执行以下命令" $file_name is an unknowfile"
echo " $file_name is an unknowfile"
fi #语句结束
[iyunv@localhost test]# chmod+x elif.sh #给脚本增加执行权限
[iyunv@localhost test]#./elif.sh
input a file name:
/etc #此处判断处,输入/etc,判断其文件类型
/etc is a dir #此处为判断结果,etc文件是一个目录
[iyunv@localhost test]#./elif.sh
input a file name:
/etc/passwd
/etc/passwd is file # passwd类型是一个文件
[iyunv@localhost test]#./elif.sh
input a file name:
/dev/sda
/dev/sda is a device file #输出/dev/sda是一个块设备文件
四:实验结束
|