一篇博客学会shell脚本
通过本章学习我们可以学会以下内容:一 什么是shell以及shell的好处
二 shell脚本的格式以及变量的使用方法
三 test测试语句和if流程控制
四 case-for-while语句的使用方法
五 中双小括号的使用方法
六 循坏嵌套使用方法
一 什么是shell以及shell的工作方式:
shell简单来说可以当做人与计算机之间的翻译官,他作为用户与linux系统内部的通信媒介,除了能够支持各种变量与参数外,还可以提供循坏、分之等高级编程语言才有的控制结构特性。
shell的工作方式有二种:
交互式:用户每输入一条命令就立即执行。
批处理:用户先编好一个完整的shell脚本,shell会一次执行脚本的多个命令。
二 shell脚本的格式以及变量的使用方法
创建shell脚本的步骤:
第一步:创建一个包含命令和控制结构的shell文件,一般是后缀.sh
第二部:修改文件的权限使它可以执行,一般使用chmod U+x 再加文件名
第三步:执行脚本
方法一: ./加脚本名称(常用这一种)
方法二:使用绝对路径
方法三:bash加文件名
举例说明:创建一个脚本,查询一下他的路径和包含的文件内容。
[*] # vim example01.sh 创建脚本名称
[*] # chmod +x example01.sh加上执行权限
[*] # ./example01.sh 运行脚本
[*] our first example
[*]
[*] we are currently in the following directory.
[*] /root/jiaoben
[*]
[*] this directory contains the following files
[*] example01.sh
[*] # cat example01.sh查看脚本内容
[*] #!/bin/bash
[*] #this 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 following directory."
[*] pwd
[*] echo
[*] echo "this directory contains the following files"
[*] ls
shell变量的使用方法:
用户定义变量:由字母或者下划线开头,由字母或者下划线组成,大小写字母意义不同,变量长度没有限制。
使用变量值时:要在变量名前加上前缀"$"
举例说明:2VAR就是非法变量
变量赋值:赋值号 = 二边没有空格
举例说明:
# A=aaa变量赋值这种正确的
# A = AAA这种错误
-bash: A: command not found
# echo $A 使用变量
aaa
# A=`date` 将一个命令的执行结果赋予变量注意加``
# echo $A
Thu Mar 22 10:33:58 CST 2018
# B=$(ls -l) B的变量赋值就是一个ls -l的命令
# echo $B
total 40 -rw-------. 1 root root 1409 Mar 14 22:00 anaconda-ks.cfg -rwxr-xr-x. 1 root root 75 Mar 16 15:26 backup.sh -rwxr-xr-x. 1 root root 115 Mar 19 16:00 baojing.sh -rwxr-xr-x. 1 root root 195 Mar 16 17:00 caipiao.sh drwxr-xr-x. 2 root root 24 Mar 22 10:11 jiaoben drwxr-xr-x. 4 root root 49 Mar 19 20:38 lianxi -rwxr-xr-x. 1 root root 184 Mar 20 15:46 mizhi.sh -rwxr-xr-x. 1 root root 113 Mar 20 15:05 pingfang.sh -rwxr-xr-x. 1 root root 235 Mar 20 21:26 sanjiao.sh -rwxr-xr-x. 1 root root 44 Mar 19 14:17 shifen.sh -rwxr-xr-x. 1 root root 92 Mar 19 15:53 wokao.sh -rwxr-xr-x. 1 root root 112 Mar 20 15:19 xingqitian.sh drwxr-xr-x. 2 root root 6 Mar 18 13:48 zuoye
# A=$B A赋值变量B的内容
# echo $A
total 40 -rw-------. 1 root root 1409 Mar 14 22:00 anaconda-ks.cfg -rwxr-xr-x. 1 root root 75 Mar 16 15:26 backup.sh -rwxr-xr-x. 1 root root 115 Mar 19 16:00 baojing.sh -rwxr-xr-x. 1 root root 195 Mar 16 17:00 caipiao.sh drwxr-xr-x. 2 root root 24 Mar 22 10:11 jiaoben drwxr-xr-x. 4 root root 49 Mar 19 20:38 lianxi -rwxr-xr-x. 1 root root 184 Mar 20 15:46 mizhi.sh -rwxr-xr-x. 1 root root 113 Mar 20 15:05 pingfang.sh -rwxr-xr-x. 1 root root 235 Mar 20 21:26 sanjiao.sh -rwxr-xr-x. 1 root root 44 Mar 19 14:17 shifen.sh -rwxr-xr-x. 1 root root 92 Mar 19 15:53 wokao.sh -rwxr-xr-x. 1 root root 112 Mar 20 15:19 xingqitian.sh drwxr-xr-x. 2 root root 6 Mar 18 13:48 zuoye
# A=date 看这里不加``会有什么后果
# echo $A
date 只输出date
这里要说一下单引号和双引号的区别:
单引号之间的内容原封不动的指定给了变量
双引号取消了空格的作用,特殊符号的含义保留。
列出所有的变量用set
位置变量和特殊变量
位置变量:shell 解释执行用户的命令时,将命令执行的第一个字作为命令名,而其他字作为参数,由出现在命令行上的位置确定的参数称为位置参数。位置变量:使用$N来表示。
举例说明:
# ./example.sh file1 file2 file3
$0 这个程序的文件名 example.sh
$n 这个程序的第n个参数值。n=1..n
特殊变量:
有些变量一开始执行脚本的时候就会设定,且不能被修改,但我们不叫它只读的系统变量,而叫它特殊变量,这些变量当一执行程序就有了,以下是一些特殊变量:
$*这个程序的所有参数
$#这个程序参数的个数
$$这个程序的PID
$!执行上一个后台程序的PID
$?执行上一个指令的返回值
举例说明:写一个脚本表示出程序的所有参数进程ID和程序参数的个数。
[*] # vim z.sh
[*] # chmod +x z.sh
[*] # ./z.sh
[*] 表示这个程序的所有参数
[*] 0 表示这个程序的参数的个数
[*] 3486 表示程序进程的ID
[*] 3488 执行上一个就太指令的PID
[*] 3486 表程序的进程ID
[*] # ./z.sh aaa bbb dddcccc加入一些参数
[*] aaa bbb ddd cccc 表示这个程序的所有参数
[*] 4 表示这个程序的参数的个数
[*] 3489 表示程序进程的ID
[*] 3491 执行上一个就太指令的PID
[*] 3489 表程序的进程ID
[*] # cat z.sh查看脚本
[*] #!/bin/bash
[*] echo "$* 表示这个程序的所有参数"
[*] echo "$# 表示这个程序的参数的个数"
[*]
[*]
[*] touch /tmp/a.txt
[*] echo "$$ 表示程序进程的ID"
[*]
[*]
[*] touch /tmp/b.txt &
[*] echo "$! 执行上一个就太指令的PID"
[*] echo "$$ 表程序的进程ID"
[*]
三 test测试语句和if流程控制
&& 代表条件性的AND
|| 代表条件性的OR
test也可以写成[]
数值测试:-gt 是否大于
-ge 是否大于等于
-eq 是否等于
-ne 是否不等于
-lt 是否小于
-le 是否小于等于
IF语法:
if条件
then
语句
fi
扩展; 分号,表示二个命令写在一行,互不影响。
if语法:
if 条件 ;then
命令1
else
命令2
fi
举例说明;写一个脚本,可以判断他是目录,或者是普通文件,或者是设备文件。
[*] # vim fuza.sh
[*] # chmod +x fuza.sh
[*] # ./fuza.sh 执行脚本
[*] ./fuza.sh: line 1: i#/bin/bash: No such file or directory
[*] input a file name 让我输入一个文件名
[*] /etc 输入/etc
[*] /etc is a dir 系统判断他是一个目录
[*] # ./fuza.sh
[*] ./fuza.sh: line 1: i#/bin/bash: No such file or directory
[*] input a file name
[*] /etc/passwd
[*] /etc/passwd is file
[*] # ./fuza.sh
[*] ./fuza.sh: line 1: i#/bin/bash: No such file or directory
[*] input a file name
[*] hhhk
[*] hhhk is an unknow file
[*] # ./fuza.sh
[*] ./fuza.sh: line 1: i#/bin/bash: No such file or directory
[*] input a file name
[*] /dev/sda
[*] /dev/sda is a device file 说它是一个设备文件
[*] # cat fuza.sh
[*] i#/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 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
四 case-for-while语句的使用方法
case: 流程控制语句,适用于多分支。
格式:
case 变量 in
字符串1)命令列表1
;;
....
字符串n)命令列表n
;;
esac
举例说明:
制作一个菜单,可以选择bcd,分别代表backup copy delete
文字部分
[*] # vim case.sh
[*] # chmod +x case.sh
[*] # ./case.sh 执行脚本
[*] ****************************
[*] Please selet you operation:
[*] 1 Copy
[*] 2 Delete
[*] 3 Backup
[*] *****************************
[*] C 我输入C
[*] your seletion is copy说我选择的是copy
[*] # ./case.sh
[*] ****************************
[*] Please selet you operation:
[*] 1 Copy
[*] 2 Delete
[*] 3 Backup
[*] *****************************
[*] D
[*] your seletion is delete
[*] # ./case.sh
[*] ****************************
[*] Please selet you operation:
[*] 1 Copy
[*] 2 Delete
[*] 3 Backup
[*] *****************************
[*] B
[*] your seletion is backup
[*] # cat case.sh
[*] #!/bin/bash
[*] echo "****************************"
[*] echo "Please selet you operation:"
[*] echo " 1 Copy"
[*] echo " 2 Delete"
[*] echo " 3 Backup"
[*] echo "*****************************"
[*] read op
[*] case $op in
[*] C)
[*] echo "your seletion is copy"
[*] ;;
[*] D)
[*] echo "your seletion is delete"
[*] ;;
[*] B)
[*] echo "your seletion is backup"
[*] ;;
[*] *) # 参数*, 匹配所有参数。
[*] echo "invalide selection"
[*] ;;
[*] esac
whlie 是循坏语句,用在循坏中
格式:
while 条件
do
命令
done
话不多说,继续举例说明:
算出一到十的平方,用脚本写出来。
脚本内容:
文字说明:
[*] # vim pingfang.sh
[*] # chmod +x pingfang.sh
[*] # ./pingfang.sh
[*] 1
[*] 4
[*] 9
[*] 16
[*] 25
[*] 36
[*] 49
[*] 64
[*] 81
[*] 100
[*] # cat pingfang.sh
[*] #!/bin/bash
[*] num=1
[*] while [ $num -le 10 ] 当变量小于10的时候执行下面的动作
[*] do
[*] square=`expr $num \* $num` 变量的值相乘。中间要用转移符
[*] echo $square
[*] num=`expr $num + 1` 每次执行加一下
[*] done
五 中双小括号的使用方法
使用(())扩展shell中算数运算的使用方法,使用[]的时候,必须保证运算符与算数之间有空格,四则运算也只能借助expr命令来完成,我们今天的说的(())结构语句,就是对shell中算数及赋值运算的扩展。
举例说明:
依次输出小于100以内2的幂值,输出的结果应该为:2 4 8 16。
脚本的内容:
文字解释:
[*] # vim mizhi.sh
[*] # chmod +x mizhi.sh
[*] # ./mizhi.sh
[*] the while loop example
[*] value of the variable is : 1
[*] value of the variable is : 2
[*] value of the variable is : 4
[*] value of the variable is : 8
[*] value of the variable is : 16
[*] value of the variable is : 32
[*] value of the variable is : 64
[*] the loop execution is finished
[*] # cat mizhi.sh查看一下脚本内容
[*] #!/bin/bash
[*] echo "the while loop example"
[*] echo
[*] var1=1
[*] while ((var1
页:
[1]