一、Linux Shell基本概念
Linux Shell是一种命令行解释器,用于与Linux操作系统进行交互。它是用户与操作系统内核之间的接口,提供了一种通过命令行输入指令来操作和控制操作系统的方式。当然,shell脚本也会用于windows中,在windows中,常见的则是批处理文件,即.bat文件。不过,一般而言,linux中较为常用。这里讲述关于linux中的shell的概念。
1、Shell脚本
Shell脚本是一种用Shell语言编写的脚本,它可以包含一系列命令和控制结构,用于自动化执行一些任务或操作。常见的Shell语言包括Bash(Bourne Again Shell)、Csh(C Shell)、Ksh(Korn Shell)等。这里以bash为核心进行讲解。
2、命令行编辑
Shell作为命令行解释器,可以接收用户的命令,解析并执行相应的操作。也就是说,用户可以通过命令与操作系统进行交互。如文件和目录操作、进程管理、系统配置和网络操作等,都可以使用shell脚本进行编写,然后通过执行shell脚本即可达到目的,实现简单重复性操作和批量处理的任务进行自动化。shell作为Linux操作系统的重要组成部分,为用户提供了强大的命令行操作和脚本编程的能力。
shell文件一般开头为#!/bin/sh:表示这是一个shell文件
二、Shell基本语法
1、变量
(1)变量的命名规则
通常以字母、下划线、数字组成,不能以数字开头。如var、var1、Ab_c等。
(2)shell变量的格式及规则
①通常变量赋值时“=”号两边不能有空格,因为在shell中,空格往往用来表示命令行的空格
②不可使用shell关键字作为变量名,如for,这样,shell不可区分,会报错
③通常命名常量用大写字母,如PI=3.14
(3)shell常用变量
①$0:脚本名称
②$1,$2...:脚本参数
③$#:表示传递给脚本的参数数量
④$?:表示上一个命令的退出状态
(4)举例说明
#!/bin/bash
echo "hello world?" #echo表示打印
URL="www.baidu.com" #定义一个URL字符串
var="a" #定义一个var,为一个字符
PI=3.14 #定义一个PI,为常量3.14
echo $URL $var $PI #打印变量的值
for n in $URL;do
echo "URL is $n" #通过for打印URL变量的值
done
URL="URL is hello!!" #改变变量URL的值
echo "URL is changed, it is '$URL'" #打印改变之后的URL的值
echo $0 $1 $2 $# $? #分别输出对应的值
2、字符串
(1)赋值
一般有两种方式赋值字符串,单引号和双引号。但两种是不一样的。
①单引号:单引号的字符串会正常打印,但是单引号的变量不会正常打印变量,而是单引号中的变量名;单引号的转义符是无效的,会原样打印;
②双引号:双引号中的变量会正常打印中变量对应的值;双引号支持转义;
str1="this is a string, and double"
str2='this is a not double!!'
my_name="llj"
echo "hello, '${my_name}', $str1"
echo "hello, ${my_name}, $str2"
echo "\"${str1}\""
echo '\'${str2}'\'
echo '${str2}'
echo ''${str2}''
(2)字符串长度
shell与其他语言一样,对于某个字符串,可以获取对应的字符串的长度;
str1="this is a string, and double"
echo "str1的字符串的长度为${#str1}"
(3)字符串子串
str3="hello,world!"
echo "只输出str3中的wo:${str3:6:2}" #从第6个字符开始截取2个字符
(4)举例说明
#!/bin/sh
str1="this is a string, and double"
str2='this is a not double!!'
my_name="llj"
echo "hello, '${my_name}', $str1"
echo "hello, ${my_name}, $str2"
echo "\"${str1}\""
echo '\'${str2}'\'
echo '${str2}'
echo ''${str2}''
echo "str1的字符串的长度为${#str1}"
str3="hello,world!"
echo "str3的{hello,world!}长度为${#str3}"
echo "只输出str3中的wo:${str3:6:2}" #从第6个字符开始截取2个字符
echo `expr index "${str3}" wo` #查找子串,wo哪个字符先出现就取哪个,这里index从1开始取,而不是0
运行输出结果:
hello, 'llj', this is a string, and double
hello, llj, this is a not double!!
"this is a string, and double"
\this is a not double!!\
${str2}
this is a not double!!
str1的字符串的长度为28
str3的{hello,world!}长度为12
只输出str3中的wo:wo
5
3、数组
(1)数组定义与表示
shell中,数组的表示方法与其他语言不同,并且shell中数组也只存在一维数组,不存在二维数组,具体表示方法以及读取数组的方法如下:
#定义一个数组,并循环输出数组的内容
arr=(1 2 3)
arr[0]='a'
echo "arr数组的索引为2的元素为:${arr[2]}"
echo "打印arr整个数组:${arr[@]}"
其中,arr[0]='a',表示将数组arr中的第1位重新赋值;而#{arr[@]} 表示读取整个数组的内容;
(2)数组长度
arr=(1 2 3)
len=${#arr[@]}
echo "arr数组的整个长度为:${len}"
(3)举例说明
#!/bin/sh
#定义一个数组,并循环输出数组的内容
arr=(1 2 3)
arr[0]='a'
echo "arr数组的索引为2的元素为:${arr[2]}"
echo "打印arr整个数组:${arr[@]}"
len=${#arr[@]}
len1=${#arr[*]}
echo "arr数组的整个长度为:len:${len}, len1:${len1}"
arr1=(1 2 "hello,world!")
echo "arr1数组的第三位的字符串:\"${arr1[2]}\"的长度为:${#arr1[2]}"
运行输出结果:
arr数组的索引为2的元素为:3
打印arr整个数组:a 2 3
arr数组的整个长度为:len:3, len1:3
arr1数组的第三位的字符串:"hello,world!"的长度为:12
4、shell运算符
(1)算数运算符
shell与其他语言一样,也支持算数运算,包括“+,-,*,/,%”等
a=1
b=2
sum=`expr $a + $b` #expr为一个算数运算的关键字,“$a+$b”注意空格
echo "a+b的值为:${sum}"
echo "a-b的值为:`expr $a - $b`"
echo "a*b的值为:`expr $a \* $b`" #这里注意*需要转义,一般字符串中包含了一些特殊字符,都需要转义
echo "a/b的值为:`expr $a / $b`" #"/"一般取整,这里输出0
echo "a%b的值为:`expr $a % $b`" #“%”取余,这里取1
(2)关系运算符
关系运算符包括:
-eq:相等
-ne:不相等
-gt:大于
-lt:小于
-ge:大于或等于
-le:小于或等于
a=1
b=2
if [ $a -eq $b ] #-eq,表示相等
then
echo "a等于b"
else
echo "a不等于b"
fi
if [ $a -ne $b ] #-ne,表示不相等
then
echo "a不等于b"
else
echo "a等于b"
fi
(3)布尔运算符
布尔运算法指的类型包括true,false
成功返回true,失败返回false
a=1
b=2
if [ $a != $b ]
then
echo "a!=b:true"
else
echo "a==b:false"
fi
(4)逻辑运算符
逻辑运算符包括&&、||、!
&&:与,也就是python中and
||:或,相当于python中or
!:非,与python类似
a=1
b=2
if [[ $a != $b && $a -eq 1 ]]
then
echo "a!=b:true, a=1"
else
echo "a==b:false, a!=1"
fi
if [[ $a -eq $b || $a -eq 1 ]]
then
echo "a==b||a=1"
else
echo "a!=b || a!=1"
fi
(5)举例说明
#!/bin/sh
#关系运算
a=1
b=2
sum=`expr $a + $b` #expr为一个算数运算的关键字,“$a+$b”注意空格
echo "a+b的值为:${sum}"
echo "a-b的值为:`expr $a - $b`"
echo "a*b的值为:`expr $a \* $b`" #这里注意*需要转义,一般字符串中包含了一些特殊字符,都需要转义
echo "a/b的值为:`expr $a / $b`" #"/"一般取整,这里输出0
echo "a%b的值为:`expr $a % $b`" #“%”取余,这里取1
#关系运算
if [ $a -eq $b ] #-eq,表示相等
then
echo "a等于b"
else
echo "a不等于b"
fi
if [ $a -ne $b ] #-ne,表示不相等
then
echo "a不等于b"
else
echo "a等于b"
fi
#布尔,false,true
if [ $a != $b ]
then
echo "a!=b:true"
else
echo "a==b:false"
fi
#逻辑运算
if [[ $a != $b && $a -eq 1 ]]
then
echo "a!=b:true, a=1"
else
echo "a==b:false, a!=1"
fi
if [[ $a -eq $b || $a -eq 1 ]]
then
echo "a==b||a=1"
else
echo "a!=b || a!=1"
fi
运行输出结果:
a+b的值为:3
a-b的值为:-1
a*b的值为:2
a/b的值为:0
a%b的值为:1
a不等于b
a不等于b
a!=b:true
a!=b:true, a=1
a==b||a=1
4、流程控制
(1)if...else
shell中if...else的使用与python类似,只是在shell中,必须要有fi结尾,以及必须使用then关键字;
a=1
b=2
echo "a=${a}, b=${b}"
if [ $a == $b ] #if中[]为判断语句,条件语句
then
echo "a==b"
elif [ $a -ne $b ]
then
echo "a!=b"
else
echo "a==b"
fi #末尾的fi必须要有
(2)for
shell中for,语句必须存在do...done关键字
a=(1,2,3)
for i in $a;do
echo $i
done
for i in $a
do
echo $i
done
(3)while与until
while((循环条件))...do...done
i=0
while(( $i<5))
do
echo $i
i=`expr $i + 1`
if (($i==3))
then
break
else
echo "i当前不等于3,继续"
continue
fi
done
until((循环条件))...do...done
i=0
until(( $i==7 ))
do
echo $i
i=`expr $i + 1`
done
(4)举例说明
①if语句
#!/bin/sh
a=1
b=2
echo "a=${a}, b=${b}"
if [ $a == $b ] #if中[]为判断语句,条件语句
then
echo "a==b"
elif [ $a -ne $b ]
then
echo "a!=b"
else
echo "a==b"
fi #末尾的fi必须要有
if (( $a == $b )) #与if中[]类似
then
echo "a==b"
elif (( $a > $b ))
then
echo "a>b"
else
echo "a<b"
fi
m=$[1+1]
n=2
if test $[m] -eq $n #if可与test命令一起使用
then
echo "m==n"
else
echo "m!=n"
fi
运行结果输出:
a=1, b=2
a!=b
a<b
m==n
②for语句
#!/bin/sh
a=(1,2,3)
for i in $a;do
echo $i
done
for i in $a
do
echo $i
done
运行结果输出:
1,2,3
1,2,3
③while,until语句
#!/bin/sh
i=0
while(( $i<5))
do
echo $i
i=`expr $i + 1`
if (($i==3))
then
break
else
echo "i当前不等于3,继续"
continue
fi
done
until(( $i==7 ))
do
echo $i
i=`expr $i + 1`
done
运行结果输出:
0
i当前不等于3,继续
1
i当前不等于3,继续
2
3
4
5
6
④其他,case...in...
#!/bin/sh
str="hello, world"
case "$str" in
"he") echo "false"
echo "go on"
;;
"mm") echo "不存在"
;;
"hello, world") echo "true"
echo "已经找到"
;;
"h") echo "ng"
;;
esac
运行结果输出:
true
已经找到
5、函数
(1)不带参数的函数
函数的定义方法为:function func1(){...}
调用方法:func1()
(2)带参数的函数
函数的定义方法为:function func2(){...}
调用方法:func2 3 4 5 #其中,3,4,5为带入函数func2的参数值,在func2中可直接使用$1...
(3)举例说明
#!/bin/sh
function func(){
echo "shell函数的内容"
echo "hello, world!!"
echo "函数带入的参数第一个参数为:$1,第二个为:$2, 两者相加等于:`expr $1 + $2`"
}
echo "开始执行函数"
func 3 4
echo "结果函数"
运行输出结果:
开始执行函数
shell函数的内容
hello, world!!
函数带入的参数第一个参数为:3,第二个为:4, 两者相加等于:7
结果函数