学习Linux中的shell script

1.什么是shell script

shell script 就是一种程序化脚本。shell是让用户和系统沟通的一种工具,我们所使用的bash就是shell的一种。而shell script 就是用shell功能来写一个程序。

1.1.编写第一个script程序

第一个程序的功能就是简单输出hello world

zhangsan@Aliyun:~$ mkdir script; cd script
zhangsan@Aliyun:~$ vim zhangsan01.sh
  1 #!/bin/bash
  2 # This is the program show "Hello world"
  3 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
  4 export PATH
  5 echo -e "Hello world! \a \n"
  6 exit 0  #离开script回传给系统一个0 这里也可以回传其他值
zhangsan@Aliyun:~$ chmod +x zhangsan01.sh 
# 加上执行权限
zhangsan@Aliyun:~$ ./zhangsan01.sh 
Hello world! 

1.2.script编程的良好习惯

在写程序的时候,需要保持好的习惯(好记性不如烂笔头)。在每个script程序执行时,我们需要处理好:
1.script的功能
2.script的版本信息
3.script的作者与联络方式

2.简单范例

2.1.交互式脚本

  1 #!/bin/bash
  2 # Program:
  3 #       User input his first name and last name,Program show his full name
  4 # History:
  5 # 2020/02/20 kaitokuroba
  6 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
  7 export PATH
  8 
  9 read -p "Please input your first name:" firstname
 10 read -p "Please input your last name:" lastname  
 11 echo -e  "\n Your full name is $firstname $lastname"
 12 exit 0
zhangsan@Aliyun:~$ ./zhangsan02.sh    
Please input your first name:san
Please input your last name:zhang
Your full name is san zhang

2.2.随日期变化

filename没有被定义的话,文件的名字就是filename,filename被定义过后,就是定义的名字。

  1 #!/bin/bash
  2 # Program
  3 #       Program creat three files, which named by user's input
  4 #       and date command.
  5 #History
  6 #       2020/0220/
  7 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
  8 export PATH
  9 set -x
 10 echo -e "I will use touch command to create 3 files." 
 11 read -p "Please input your filename:" fileuser
 12 
 13 filename=${fileuser:-"filename"} #变量分析,若是filesuer存在,则用filesuer,若不存在,则用filename.
 14 
 15 date1=$(date --date='2 days ago' +%Y%m%d)
 16 date2=$(date --date='1 days ago' +%Y%m%d)
 17 date3=$(date +%Y%m%d)
 18 file1=${filename}${date1}
 19 file2=${filename}${date2}
 20 file3=${filename}${date3}
 21 
 22 
 23 touch "$file1"
 24 touch "$file2"
 25 touch "$file3"

2.3.不同的script的执行方式的区别

直接执行方式来执行script
无论是用绝对路径还是相对路径来执行script,其都会调用一个新的bash,即其是在子进程中执行的。
利用source
利用source命令来执行的话,就是在父进程执行,
330030192217601

3.判断指令

3.1.test命令的用法

test可以用来作为文件类型检测,文件权限检测以及文件新旧程度比较的工具。

zhangsan@Aliyun:~$ test -e /dmtsai && echo "exist" || echo "Not exist"
Not exist

这里写了一个判断文件类型的脚本

  1 #!/bin/bash
  2 # 功能
  3 # 用户输入一个文件名
  4 # 我们判断:
  5 # 1.文件是否存在,不存在打出“filename does not exist”
  6 # 2.若文件存在则,判断是文件还是目录,输出filename is regular file 
  7 #                               或者,输出filename is directory
  8 # 3.判断执行者的权限并输出
  9 
 10 set -x
 11 read -p "Please input your filename: " filename
 12 test -e /$filename || echo "filename does not exist" 
 13 test -e /$filename || exit
 14 test -f /$filename && echo "filename is regular file" 
 15 test -d /$filename && echo "filename is directoty"
 16 
 17 
 18 test -r /$filename && echo "有读权限"
 19 test -x /$filename && echo "有执行权限"
 20 test -w /$filename && echo "有写权限"
 21 

3.2.脚本文件的默认变量

脚本文件后面也可以添加参数。在执行脚本文件时,可以向里面加入参数,就像我们在使用函数一样。具体的变量指代如下所示:

/path/to/scriptname opt1 opt2 opt3
         $0          $1   $2   $3

现在举一个例子:文件名称为sh05.sh

  1 #!/bin/bash
  2 # Program
  3 #       Prpgram shows the script name, parameters
  4 #
  5 
  6 echo "the script is     ==> $0 "
  7 echo "Total parameter number is  ==> $#"
  8 [ "$#" -lt 2 ] &&  echo "The number of parameter is less than 2. Stop here." && exit 0
  9 
 10 echo "Your whole parameter is ==> '$@' "
 11 echo "The first parameter    ==> $1"
 12 echo "The second paremeter  ==> $2"                                    

执行这个文件的结果:

zhangsan@Aliyun:~$ ./sh05.sh 1 2 3 4 5
the script is 	==> ./sh05.sh 
Total parameter number is  ==> 5
Your whole parameter is ==> '1 2 3 4 5' 
The first parameter    ==> 1
The second paremeter  ==> 2

当然,我们的这个变量也是可以偏移的,即输入的变量可以被切割。本来五个变量 1,2,3,4,5。输入shift 2,那么前两个变量就会被删除。

4.条件判别

4.1.利用 if…then

单侧,简单条件判别式

if [ 条件判断式 ]; then
	当条件判断式成立时,可以进行的命令工作内容
fi 

由if,then举例说明:

  1 #!/bin/bash
  2 #Program
  3 
  4 
  5 read -p "Please input (y/n): " yn
  6 
  7 if [ "$yn" == "y" ] || [ "$yn" == "Y" ]; then
  8         echo "OK, continue"
  9         exit 0
 10 fi
 11 
 12 if [ "$yn" == "n" ] || [ "$yn" == "N" ]; then
 13         echo "Oh, interrupt"
 14         exit 0
 15 fi
 16 echo "I don't know what your choice is:" && exit 0

不希望用户由键盘输入时,可以用上一节所使用的参数$1

  1 #!/bin/bash
  2 # Program
  3 #       check $1 is equal to "hello"
  4 #
  5 #
  6 
  7 if [ "$1" == "hello" ]; then
  8         echo "Hello, how are you!"
  9 elif [ "$1" == "" ]; then
 10         echo "You must input parameter, ex> {$0 someword}"
 11 else
 12         echo "the only parameter is 'hello' , ex> {$0 hello}"
 13 fi
 14        

4.2.利用case…esac判断

case $变量名称 in  <==关键字为case,还有变量前有s
	"第一个变量内容")
	程序段
	;;
	"第二个变量内容")
	程序段
	;;
	*)      最后一个变量用*代表
	exit 1
	;;
esac

4.3.function函数

这里的函数和其他语言的差不多意思,可以简化代码,避免板砖。在shell script中,function的语法是这样的:

function fname()  {
		 程序段
}

下面是一个利用function的例子:

  1 #!/bin/bash
  2 # Program
  3 #       Use function to repeat information
  4 
  5 function printit () {
  6         echo "your choice is $1"
  7 
  8 }
  9 
 10 echo "This progranm will print your selection"
 11 
 12 case $1 in
 13         "one")
 14                 printit 1
 15                 ;;
 16                 
 17         "two")
 18                 printit 2
 19                 ;;
 20         "three")
 21                 printit 3
 22                 ;;
 23         *)      
 24                 echo "Useage $0 {one|two|three}"
 25                 ;;
 26 esac

具体使用如下:

zhangsan@Aliyun:~$ ./sh09.sh one
This progranm will print your selection
your choice is 1
zhangsan@Aliyun:~$ ./sh09.sh four
This progranm will print your selection
Useage ./sh09.sh {one|two|three}

5.循环loop

5.1. while do done, until do done

不定循环的两种方式,一种就是while,开始时满足循环条件,之后跳出循环;另一种是until,开始时不满足循环条件,之后跳入。
while do done condition条件不成立时,终止循环

while [ condition ] <==中括号内为判别式
do				<==do 是循环的开始
	程序段落
done          <==循环结果

until do done 当condition条件成立时,就终止循环

until [ condition ] <==中括号内为判别式
do				<==do 是循环的开始
	程序段落
done          <==循环结果

5.2. for…do…done

for循环就是已经知道要循环几次。

for var in con1 con2 con3...
do
	program!
done

查找账号:

  1 #!/bin/bash
  2 # Program
  3 
  4 users=$(cut -d ':' -f1 /etc/passwd)
  5 for username in $users
  6 do
  7         id $username
  8         finger $username
  9 done        

5.3 for…do…done数值处理

格式如下

for ((初始值;限制值;执行步长))
do
	程序段
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值