Shell脚本
位置变量
$0 : shell 脚本的名字
$1-$9 : 第一个参数~第九个参数,空格做为分隔符(如果想输出 10 可以使用 10 可以使用 10可以使用{10}作为一个整体)
$# : 位置参数的个数[不包括shell脚本本身]
$*:所有的位置参数
$@:所有的位置参数
$?:上一条命令的执行状态或获取shell函数的返回值注:0代表状态为真,非0代表状态为假
$$:获得我们shell脚本的进程号pid
代码实例:创建start.sh文件
echo 'num:' $#
echo 'all:' $@
echo 'pid:' $$
执行脚本
bash start.sh 10 20 30 40 50
num: 5
all: 10 20 30 40 50
pid: 5560
功能性语句
输入功能-read
read var1 var2
echo "var1:" $var1
echo "var2:" $var2
输出结果:
bash start.sh
10 20 30
var1: 10
var2: 20 30
shell编程中若是利用read函数读取参数的时候,若是输入>大于当前参数,则当前参数前面依次对齐,最后一个存储所有输入之后剩余的参数,若是输入=当前参数,正常输入 若是输入<当前参数,取输入的个数与当前参数对齐,不足者补空格;
算数计算—expr
read var1 var2
echo "var1:" $var1
echo "var2:" $var2
add=`expr $var1 + $var2` #注意+号左右要有空格
sub=`expr $var1 - $var2`
exc=`expr $var1 \* $var2` #加上转义字符\
mut=`expr $var1 / $var2`
echo "add="$add
echo "sub="$sub
echo "exc="$exc
echo "mut="$mut
运算结果:
bash start.sh
1 2
var1: 1
var2: 2
add=3
sub=-1
exc=2
mut=0
测试——test
注:test 测试的时候,若是用到 =,= 两边要有空格
字符串
= 测试两个字符串是否相等
!= 测试两个字符串是否不相等
-z 测试字符串长度是否为0
-n 测试字符串是否不为0
正确返回0,错误返回1
代码示例:
read string1 string2
test string1 = string2
echo "string1=" $string1
echo "string2=" $string2
echo "string1 equals string2 ?:" $?
test string1 != string2
echo "string1 not equals string2?" $?
test -n string1
echo "string1 length is zero ?" $?
test -z string2
echo "string2 length is not zero" $?
输出结果:
bash start.sh
hello
string1= hello
string2=
string1 equals string2 ?: 0
string1 not equals string2? 0
string1 length is zero ? 0
string2 length is not zero 1
整数
-eq 等于
-ne 不等于
-ge 大于等于
-le 小于等于
-gt 大于
-lt 小于
示例:
echo please input two nums:
read num1 num2
test $num1 -eq $num2
echo "num1=num2?" $?
test $num1 -ne $num2
echo "num1!=num2" $?
test $num1 -gt $num2
echo "num1>num2" $?
test $num1 -lt $num2
echo "num1<num2" $?
运行结果:
bash start_01.sh
please input two nums:
10 20
num1=num2? 1
num1!=num2 0
num1>num2 1
num1<num2 0
逻辑运算
-a 且的关系连接多个命令
-o 或的关系连接多个命令
文件
-d : 测试是否是一个目录文件
-f : 测试是否是一个普通文件
-w : 测试是否可写
-r : 测试是否可读
-x : 测试是否可执行
echo please input filename:
read var
test -f $var
echo "$var is file?" $?
test -d /home/linux
echo "/home/linux is directory?" $?
运行结果:
bash start_02.sh
please input filename:
start_02.sh
start_02.sh is file? 0
/home/linux is directory? 0
if语句
单分支if…else…
echo -n "Please input a dirname : "
read dirname
if [ -d $dirname ]
then ls $dirname
else
echo $dirname is not directory!
fi
语法:
if [ 表达式 ] //注意空格 [ 表达式 ]=test 表达式
then 命令1 ...
else 命令n ...
fi
多分支语句if…elseif …else
echo please input a number
read number
if [ $number -ge 0 -a $number -le 100 ]
then
echo $number belong to "[0,100]"
elif [ $number -gt 100 -a $number -le 200 ]
then
echo $number belong to "(100,200]"
elif [ $number -gt 200 -a $number -le 300 ]
then
echo $number belong to "(200,300]"
else
echo too long
fi
case语句
匹配常量
echo please input string
read string
case $string in
"A")
echo "A"
;;
"B")
echo "B"
;;
"C")
echo "C"
;;
"D"|"E"|"F")
echo "D or E or F"
;;
*)
echo other
esac
匹配变量
echo please input string
stringA=A
read string
case $string in
$stringA)
echo $stringA
;;
"B")
echo "B"
;;
"C")
echo "C"
;;
"D"|"E"|"F")
echo "D or E or F"
;;
*)
echo other
esac
匹配字符串列表
echo please input string
stringA=A
read string
case $string in
$stringA)
echo $stringA
;;
"B")
echo "B"
;;
"C")
echo "C"
;;
"D"|"E"|"F")
echo "D or E or F"
;;
[GHIJKL])
echo "GHIJKL"
;;
[M-Z])
echo "M-Z"
;;
*)
echo other
esac
循环语句
1、while
sum=0
i=1
while [ $i -le 100 ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "sum=$sum i=$i"
运行结果:
bash start_for.sh
sum=5050 i=101
2、for
i=1
for str in "hello" "world" "hi"
do
echo $i : $str
i=`expr $i + 1`
done
运行结果:
bash start_for.sh
1 : hello
2 : world
3 : hi
应用:
打印 /目录下的文件名
i=1
for str in "hello" "world" "hi"
do
echo $i : $str
i=`expr $i + 1`
done
运行结果:
1 : bin
2 : boot
3 : cdrom
4 : dev
5 : etc
6 : home
7 : initrd.img
8 : initrd.img.old
9 : lib
10 : lib32
11 : lib64
12 : libx32
13 : lost+found
14 : media
15 : mnt
16 : opt
17 : proc
18 : root
19 : run
20 : sbin
21 : snap
22 : srv
23 : swapfile
24 : sys
25 : tmp
26 : usr
27 : var
数组
直接定义
array=(1 2 3 "hello")
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}
单元素定义
array[0]=1
array[1]="hello"
array[2]=2
array[3]="world"
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}
获取数组全部元素使用
S H E L L A R R A Y [ ∗ ] 或 {SHELL_ARRAY[*]}或 SHELLARRAY[∗]或{SHELL_ARRAY[@]}
获取数组长度
使用KaTeX parse error: Expected '}', got '#' at position 2: {#̲SHELL_ARRAY[*]}…{#SHELL_ARRAY[@]}
获取单个元素的长度
SHELL_ARRAY[3]="hello Shell"
echo ${#SHELL_ARRAY[3]} //获取四个元素长度为11
循环遍历数组
array[0]=1
array[1]="hello"
array[2]=2
array[3]="world"
i=0
for item in ${array[@]}
do
echo array[$i]= $item
i=`expr $i + 1`
done
array[0]= 1
array[1]= hello
array[2]= 2
array[3]= world
删除数组
可以使用 unset
unset 数组名 :删除整个数组
unset 数组名[下标] :删除数组中该下标的元素
array[0]=1
array[1]="hello"
array[2]=2
array[3]="world"
i=0
for item in ${array[@]}
do
echo array[$i]= $item
i=`expr $i + 1`
done
echo -----------
#删除数组中最后一个元素
unset array[`expr ${#array[@]} - 1 `]
echo ${array[@]}
运行结果:
array[0]= 1
array[1]= hello
array[2]= 2
array[3]= world
-----------
1 hello 2
关联数组
shell 中还提供了一种关联性数组,在使用关联数组前,必须先使用declare -A声明它
declare -A ARGLIST
ARGLIST=([name1]=1 [name2]=2 [name3]=3) #定义数组
echo ${ARGLIST[@]} //输出数组内的全部元素--输出顺序和定义的顺序无关 linux中是反过来的
echo ${ARGLIST[name1]}
echo ${!ARGLIST[@]} //输出所有的下标
echo ------------------------
for key in ${!ARGLIST[@]} //通过遍历下标,输出数组内的全部元素
do
echo ARGLIST[$key]=${ARGLIST[$key]}
done
3 2 1
1
name3 name2 name1
------------------------
ARGLIST[name3]=3
ARGLIST[name2]=2
ARGLIST[name1]=1
函数
函数的定义
方法1:
function 函数名()
{
命令1 命令2 ...
}
方法2:
函数名(){
命令1 命令2 ...
}
函数的调用
fun1(){
echo fun1 is called.
num=10
return $num
}
echo $0 is start!
fun1 #函数的调用
echo $? #获取返回值
start_fun.sh is start!
fun1 is called.
10
shell中的变量默认为全局变量,当shell函数调用后生效
fun1(){
echo fun1 is called.
num=10
return $num
}
echo $0 is start!
echo first num=$num
fun1
echo second num=$num
echo $?
输出结果:
start_fun.sh is start!
first num=
fun1 is called.
second num=10
0
函数传参
fun1(){
echo fun1 is called.
echo $1
echo $2
echo $3
}
echo $0 is start!
fun1 10 20 30