Shell 脚本基础语法整理


author : lrcoder
date : 2019/06/22


标示符
标示符 含义 助记
-d ××× 如果×××为目录,为真
-f ××× 如果×××为常规文件,为真
-r ××× 如果×××可读,为真 read
-w ××× 如果×××可写,为真 write
-x  ××× 如果×××可执行,为真
-s ××× 如果×××长度不为0,为真
-eq = equal
-ne != unequal
-gt > big than
-ge >= big equal
-lt < little than
-le <= little equal
-e ××× 如果×××存在,为真
××× -a ××× 且 && and
××× -o ××× 或 || or
赋值
name=Pzh            等号前后不能有空格
name="PZH"      变量值中有空格要用双引号
echo ${name}            用${}更保险
只读变量

使用readonly命令

#!/bin/bash
myUrl="www.baidu.com"
readonly myUrl
myUrl="www.12345.com"

运行报错:

pzh@pzh:~/桌面$ ./readonly_test.sh
./readonly_test.sh: 行 4: myUrl: 只读变量
删除变量

使用unset命令可以删除变量,unset命令不能删除只读变量。

#!/bin/bash
name="Pzh"
echo ${name}
##########################
myUrl="www.baidu.com"
readonly myUrl
echo ${myUrl}
##########################
unset name
echo ${name}
##########################
unset myUrl
echo ${myUrl}

运行结果:

pzh@pzh:~/桌面$ ./delet_test.sh
Pzh
www.baidu.com

./delet_test.sh: 第 12 行: unset: myUrl: 无法取消设定: 只读 variable
www.baidu.com
注释
单行注释
# echo "ni" 
多行注释
:<

或者

:'
...
...
...
'

Shell 字符串 (尽量使用双引号字符串)

字符串的定义和输出:
str='this is a string'
str1="i will print \" $str \" !\n"
echo -e ${str1}

运行结果为:

i will print " this is a string " !
字符串的拼接:
str='this is a string'
# 双引号拼接
myStr="hello, "$str"!"
myStr1="hello, ${str}!"
echo -e ${myStr}
echo -e ${myStr1}
# echo -e ${myStr} '\n'${myStr1}

运行结果为:

hello, this is a string!
hello, this is a string!
获取字符串长度
str='this is a string'
echo ${#str} # 输出 16
提取子字符串

从第2个字符开始截取5个字符

str='this is a string'
echo ${str:1:5} # 输出 his i 
查找子字符串

查找i或a的位置(哪个先出现就计算哪个)

str='this is a string'
echo `expr index "$str" ia` # 输出 3 

Shell 数组

数组的定义
#!/bin/bash
array_name=(1 2 3 4 5)
array_name0=(
value0 
value1
value2
value3
)
array_name1[0]=val0
array_name1[1]=val1
array_name1[2]=val2
array_name1[n]=val3
echo -e ${array_name[@]} '\n'${array_name[n]} '\n'${array_name[0]}
echo "--------------------------------------------------------------"
echo -e ${array_name0[*]} '\n'${array_name1[@]}

运行结果:

1 2 3 4 5 
1  
1
--------------------------------------------------------------
value0 value1 value2 value3 
val3 val1 val2

获取数组长度
# 获取数组元素个数
length=${#array_name[*]}
# 或者
length0=${#array_name0[@]}
# 获取数组单个元素长度
length1=${#array_name1[n]}
echo -e ${length} '\n'${length0} '\n'${length1}
# 输出
# 5
# 4
# 4

Shell 传递参数

#!/bin/bash
echo "shell passing-context-demo!";
echo "show filename : $0";
echo "fir context : $1";
echo "sec context : $2";
echo "thi context : $3";
echo "传递参数的个数 : $#";
echo "将传递的所有参数整合为一个字符串显示: $*"; 

执行命令:

chmod +x shell_context.sh
./shell_context.sh 1 2 3

运行结果:

shell passing-context-demo!
show filename : ./shell_context.sh
fir context : 1
sec context : 2
thi context : 3
传递参数的个数 : 3
将传递的所有参数整合为一个字符串显示: 1 2 3

* 的区别:

$@ 等价与传递多个参数

$* 是将多个参数整合为一个字符串,传第一个字符串

Shell运算符

算数运算符
#!/bin/bash
a=10;
b=20;
add=`expr $a + $b`;
echo -e "a + b = ${add}";# 输出 a + b = 30
sub=`expr $a - $b`;
echo -e "a - b = ${sub}";# 输出 a - b = -10
mul=`expr $a \* $b`; 
echo -e "a * b = ${mul}";# 输出 a * b = 200
div=`expr $a / $b`;
echo -e "a / b = ${div}";# 输出 a / b = 0
rem=`expr $a % $b`;
echo -e "a % b = ${rem}";# 输出 a % b = 10
# ------------------------------------------------------------------------
if [ $a == $b ]
then 
    echo "a equal b"
fi
if [ $a != $b ]
then
    echo "a un equal b" 
fi
# 输出 a un equal b
关系运算符
#!/bin/bash

a=10;
b=20;
# --------------------------------------------
if [ $a -eq $b ]
then
    echo "$a -eq $b : a = b"
else
    echo "$a -eq $b : a != b"
fi
# ---------------------------------------------
if [ $a -ne $b ]
then
    echo "$a -ne $b : a != b"
else
    echo "$a -ne $b : a = b"
fi
# ---------------------------------------------
if [ $a -gt $b ]
then
    echo “$a -gt $b : a > b”
else
    echo "$a -gt $b : a < b"
fi
# ---------------------------------------------
if [ $a -ge $b ]
then 
    echo "$a -ge $b : a >= b"
else
    echo "$a -ga $b : a <= b"
fi
# ---------------------------------------------
if [ $a -lt $b ]
then
    echo "$a -lt $b : a < b"
else
    echo "$a -lt $b : a > b"
fi
# ---------------------------------------------
if [ $a -le $b ]
then
    echo "$a -le $b : a <= b"
else
    echo "$a -le $b : a >= b"
fi

运行结果:

10 -eq 20 : a != b
10 -ne 20 : a != b
10 -gt 20 : a < b
10 -ga 20 : a <= b
10 -lt 20 : a < b
10 -le 20 : a <= b
循环语句
for循环
#!/bin/bash

add=0;
for loop in 1 2 3 4 5 6
do
    add=`expr $loop + $add `
done
echo "the final add : $add"

运行结果:

the final add : 21
while循环
#!/bin/bash
int=1
power=0
while(($int <= 6))
do 
    power=`expr $int + $power`
    let "int++"
done
echo "the final power : $power"

运行结果:

the final power : 21
until循环
#!/bin/bash
a=0
add=0
until [ $a == 6 ]
do
    let "a++"
    add=`expr $add + $a`
done
echo "the final add : $add"

运行结果:

the final add : 21
case循环(综合demo)

demo功能:

用户给出三个操作数:

chooseNum --- 选择计算方式(加、减、乘、除、求余)

val1 ---- 参加运算的第一个数字

val2 ---- 参加运算的第二个数字

返回相应结果;

#!/bin/bash
function sum(){
    
    val1=$1;
    val2=$2;
    val=$(($1+$2));
    echo $val;
}

function sub(){

    val1=$1;
    val2=$2;
    val=$(($1-$2));
    echo $val;
}

function mul(){

    val1=$1;
    val2=$2;
    val=$(($1*$2));
    echo $val;
}

function div(){
    
    val1=$1;
    val2=$2;
    val=$(($1/$2));
    echo $val;
}

function rem(){

    val1=$1;
    val2=$2;
    val=$(($1%$2));
    echo $val;
}

# main code  
chooseNum=$1;
val1=$2;
val2=$3;
case $chooseNum in
    1) echo "you choose sum $val1 and $val2";
    val=$(sum $val1 $val2);
    echo "$val1 + $val2 = $val";
    ;;
    2) echo "you choose sub $val1 and $val2"
    val=$(sub $val1 $val2);
    echo "$val1 - $val2 = $val";
    ;;
    3) echo "you choose mul $val1 and $val2";
    val=$(mul $val1 $val2);
    echo "$val1 * $val2 = $val";
    ;;
    4) echo "you choose div $val1 and $val2";
    val=$(div $val1 $val2);
    echo "$val1 / $val2 = $val";
    ;;
    5) echo "you choose rem $val1 and $val2";
    val=$(rem $val1 $val2);
    echo "$val1 % $val2 = $val";
    ;;
    *) echo "chooseNum must little than 5"
    ;;
esac

运行结果:

# 加
# 命令: ./for.sh 1 2 4
you choose sum 2 and 4
2 + 4 = 6

# 减
# 命令: ./for.sh 2 3 4
you choose sub 3 and 4
3 - 4 = -1

# 乘
# 命令: ./for.sh 3 5 6
you choose mul 5 and 6
5 * 6 = 30

# 除
# 命令: ./for.sh 4 6 3
you choose div 6 and 3
6 / 3 = 2

# 求余
# 命令: ./for.sh 5 7 3
you choose rem 7 and 3
7 % 3 = 1

Shell 函数

#!/bin/bash
function sum(){
    val1=$1;
    val2=$2;
    val=$(($1+$2));
    echo ${val};
}
ret_val=$(sum 10 20);
echo $ret_val
# 输出 30

带有return的函数,可以在调用完函数后使用 $? 来获得函数返回值

Shell 文件包含

1、使用. ./***的形式在A shell文件中调用 B shell文件

2、使用 source ./***的形式在A shell文件中调用 B shell文件

#!/bin/bash
. ./delet_test.sh

运行结果和delet_test.sh相同

你可能感兴趣的:(Shell 脚本基础语法整理)