shell脚本

2020-07-14  本文已影响0人  g_ices

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所说的 shell 通常都是指 shell 脚本,,shell 和 shell script 是两个不同的概念。Shell 脚本文件的名称可以任意,但为了避免被误以为是普通文件,建议将.sh 后缀加上,以表示是一个脚本文件。

初识shell脚本

#!/bin/bash
# "#" 注释行,不含第一行
echo hello word!

运行shell脚本

chmod +x ./shell1.shh  #使脚本具有执行权限
./shell.shh  #执行脚本

shell脚本参数

[root@virtualLinux file]# cat sshel1.sh
#!/bin/bash
echo "Shell脚本名为$0"
echo "总共有$#个参数,分别是$*"
echo "第一个参数为$1,第三个参数为$3"
echo "执行ls -l 命令"
ls -l
echo "执行ls -l 命令,执行状态码为$?"

运行

[root@virtualLinux file]# sh sshel1.sh one two three
Shell脚本名为sshel1.sh
总共有3个参数,分别是one two three
第一个参数为one,第三个参数为three
执行ls -l 命令
总用量 12
drwxr-xr-x. 2 ftpuser ftp   63 10月 16 22:44 test
-rw-r--r--. 1 root    root 211 10月 28 19:24 test1.sh
drwxr-xr-x. 3 root    root  18 10月  7 19:32 test2
-rw-r--r--. 1 ftpuser ftp   46 10月 27 19:18 test.sh
-rw-r--r--. 1 root    root 230 10月  7 19:29 test.tar.gz
执行ls -l 命令,执行状态码为0
[root@virtualLinux file]#

判断用户的参数

Shell 脚本中的条件测试语法可以判断表达式是否成立,若条件成立则返回数字 0,否则便返回其他随机数值。
测试语句格式:[ 条件表达式 ]
按照测试对象来划分,条件测试语句可以分为 4 种:

文件测试所用的参数

判断一个文件是目录还是文件

[root@virtualLinux file]# [ -d /home/vsftpd/java/file ]
[root@virtualLinux file]# echo $?
0
[root@virtualLinux file]#

与逻辑,判断当前对象是目录就输出folder

[root@virtualLinux file]# [ -d test ]
[root@virtualLinux file]# echo $?
0
[root@virtualLinux file]# [ -d test ] && echo "folder"
folder
[root@virtualLinux file]#

或逻辑,判断当前用户是否为root用户

[root@virtualLinux file]# echo $USER
root
[root@virtualLinux file]# [ $USER = root ] || echo "login is root"
[root@virtualLinux file]# su oracle
[oracle@virtualLinux file]$ [ $USER = root ] || echo "login not root"
login not root
[oracle@virtualLinux file]$

非逻辑,判断当前用户是否为root用户

[root@virtualLinux ~]# [ $USER = root ] && echo "root"
root
[root@virtualLinux ~]#

在执行[ 条件表达式 ] 后,如果条表达式成了,&& 与条件后面的命令会执行,如果不成立,与条件后面的命令不会执行。如果条件表达式后面跟的是||或条件,如果条件表达不成立会执行,如果成立不会执行

条件表达式的执行

[root@virtualLinux ~]# [ ! $USER = root ] && echo "root" || echo "other"
other
[root@virtualLinux ~]#

先条件判断用户为root,然后!取非,反条件,执行个与运算不成立,执行的第二或运算,输出的other。

整数比较的运算符

做一个简单的数字大小判断

root@virtualLinux ~]# [ 10 -eq 10 ]
[root@virtualLinux ~]# echo $?
0
[root@virtualLinux ~]# [ 10 -lt 2 ]
[root@virtualLinux ~]# echo $?
1
[root@virtualLinux ~]# [ 10 -gt 2 ]
[root@virtualLinux ~]# echo $?
0
[root@virtualLinux ~]#

常见的比较

一个没有定义的变量判断为空后赋值再次空判断

[root@virtualLinux ~]# echo $String

[root@virtualLinux ~]# [ -z $String ]
[root@virtualLinux ~]# echo $?
0
[root@virtualLinux ~]# String="123"
[root@virtualLinux ~]# echo $String
123
[root@virtualLinux ~]# [ -z $String ]
[root@virtualLinux ~]# echo $?
1
[root@virtualLinux ~]#

流程控制语句

if、for、while、case 在Shell脚本中必不可少的,应该说在现在所有的编程语言都离不开这4中流程控制语句。

if 条件语句

if 条件语句分为单分支结构,双分支结构,多分支结构三种。

单分支结构
if 条件
    then 命令
fi

示例:

[root@virtualLinux shell]# ls
test1.sh  test.sh
[root@virtualLinux shell]# vim test2.sh
[root@virtualLinux shell]# cat test2.sh
#!/bin/bash
File="test.sh"
if [ -e $File ]
 then echo "find file"
fi
[root@virtualLinux shell]# bash test2.sh
find file
[root@virtualLinux shell]#
双分支结构

双分支结构语法关键字if then else if 它只进行一次if条件,如果满足执行then 不满足执行else。
ping 命令中 -c定义重试次数,-i定义重发间隔时间,-W定义参数超时等待时间

示例: ping 百度,如果能连通输出在线,不能连通输出不在线

[root@virtualLinux shell]# more test3.sh
#!/bin/bash
ping -c 3 -i 0.5 -W 3 www.baidu.com
if [ $? -eq 0 ]
  then echo "on-line"
else echo "off-line"
fi
[root@virtualLinux shell]# bash test3.sh
PING www.a.shifen.com (180.97.33.108) 56(84) bytes of data.
64 bytes from www.baidu.com (180.97.33.108): icmp_seq=1 ttl=54 time=40.7 ms
64 bytes from www.baidu.com (180.97.33.108): icmp_seq=2 ttl=54 time=39.5 ms
64 bytes from www.baidu.com (180.97.33.108): icmp_seq=3 ttl=54 time=37.5 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1004ms
rtt min/avg/max/mdev = 37.519/39.277/40.771/1.340 ms
on-line
[root@virtualLinux shell]#
多分支结构

多分支结构就是if,then,elsif,else,fi关键字组成,相当于口语的如果。。。。那么,如果。。。那么。。。
在 Linux 系统中,read 是用来读取用户输入信息的命令, 能够把接收到的用户输入信息赋值给后面的指定变量,-p 参数用于向用户显示一定的提示信息。
示例: 根据输入的成绩输出对应的字符串

[root@virtualLinux shell]# vim test4.sh
[root@virtualLinux shell]# more test4.sh
#!/bin/bash
read -p "请输入成绩:" GRADE
if [ $GRADE -ge 80 ]
 then echo "优"
elif [ $GRADE -lt 80 ] && [ $GRADE -ge 70 ]
 then echo "良"
elif [ $GRADE -lt 70 ] && [ $GRADE -ge 60 ]
 then echo  "合格"
else
 echo "不合格"
fi
[root@virtualLinux shell]# bash test4.sh
请输入成绩:85
优
[root@virtualLinux shell]# bash test4.sh
请输入成绩:55
不合格
[root@virtualLinux shell]# bash test4.sh
请输入成绩:76
良
[root@virtualLinux shell]# Linux shell]#

for 条件循环语句

for 循环在处理范围数据时,是最适合是,它可以读取文本数,也可以做自己的循环
语法格式

for 变量名 in 取值列表
do 
    命令序列
done
------------------------------------
for 变量名 in 列表文件
do 
    命令序列
done

示例:循环输出数字或者目录

[root@virtualLinux shell]# ls
test1.sh  test2.sh  test3.sh  test4.sh  test5.sh  test.sh
[root@virtualLinux shell]# cat test5.sh
#!/bin/bash
for num in {1..10}
do
 echo $num;
done

for ((i=1;i<10;i++));
do
  echo $i;
done

echo ‘在for执行命令循环,需用反引号``’
for str in `ls`;
do
  echo $str;
done
[root@virtualLinux shell]# bash test5.sh
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
‘在for执行命令循环,需用反引号’
test1.sh
test2.sh
test3.sh
test4.sh
test5.sh
test.sh
[root@virtualLinux shell]#

while 条件循环语句

while是不断的重复循环执行,直到循环条件为false,或者执行exit 0命令才会退出循环
语法格式

while 条件
 do 命名序列
done

示例: 做一个循环

[root@virtualLinux shell]# cat test6.sh
#!/bib/bash
flag=true
num=1
while $flag
do
 echo $num;
 num=`expr $num + 1`;
 if [  $num -eq 3  ]
   then flag=false;
 fi
done

num2=5
while true
do
 echo $num2
 num2=`expr $num2 + 1`;
if [ $num2 -eq 7 ]
 then exit 0
fi
done
[root@virtualLinux shell]# bash test6.sh
1
2
5
6
[root@virtualLinux shell]#
上一篇 下一篇

猜你喜欢

热点阅读