shell脚本学习及实践总结

2020-08-18  本文已影响0人  林瑕漓

what is shell and why I should learn shell script

(what)shell脚本即用shell命令执行一些强大的功能。
(why)shell可以在win和linux上执行,熟练掌握脚本使我们操作计算机更灵活快速

应用场景

shell工作原理

和Python一样属于脚本语言,不需要编译,因此效率稍微差一点

一个hello的简单脚本

#!/bin/bash
echo "hello shell"

将shell命令保存为test1.sh,进入的到test1.sh所在目录,输入test1.sh执行脚本如下图所示:

hello shell.png

好了,一个最easy的shell脚本诞生了。了解更多的吧。

定义变量

myfirstshell="hello shell"
Num=1

这里有个坑,定义变量的等于号=前后不能有空格,如果在python语法习惯留空格的话这里要注意!

访问变量

myfirstshell="hello shell"
num=1
echo $myfirstshell  # 带$
echo $num
echo myfirstshell  # 不带$
echo num

这里有个注意点,访问变量必须前缀使用$符号,否则输出的会是变量名而不是变量值!如下图所示:

访问变量.png

加减乘除运算

运算名称 shell对应符号
加号 +
减号 -
乘号 \*
除号 /
注意几点:

案例如下所示:

a=1
b=2
c=3
res=`expr $a + $b + $c`
echo "加法结果为:$res"
res=`expr $a - $b - $c`
echo "减法结果为:$res"
res=`expr $a \* $b \* $c`
echo "乘法结果为:$res"
res=`expr $c / $a / $b`
echo "除法结果为:$res"

加减乘除运算.png
http://note.youdao.com/favicon.ico)

与或非、求余、相等运算

运算名称 shell符号
-a
-o
!
求余 %
不相等 !=
赋值 =
相等 ==

示例:

a=1
b=2
res=`expr $a % $b`  #取余运算
echo "取余结果为$res"

b=3  # 重新赋值
echo "变量b重新赋值为了3-->$b"

if [ $a == $b ]  # 相等运算
then
    echo "变量a与b相等"

elif [ $a != $b ]  # 不相等运算
then
    echo "变量a与b不相等"
fi

取余运算等.png
http://note.youdao.com/favicon.ico)

关系运算

运算名称 shell符号
-eq 两个数相等返回true
-ne 两个数不相等返回true
-gt 左侧数小于右侧返回true
-lt 左侧数大于右侧数返回true
-ge 左侧大于等于右侧返回true
-lt 左侧小于等于右侧返回true

示例

a=1
b=1
if [ $a -eq $b ]  # 两个数相等返回true
then
    echo "-eq两个数相等返回true"
fi

b=2  #b赋值为2
if [ $a -ne $b ]  # 两个数不相等返回true
then
    echo "-ne两个数不相等返回true"
fi

a=3  # a赋值为3
if [ $a -gt $b ]
then
    echo "-gt左侧数大于右侧数返回true"
fi

b=4
if [ $a -lt $b ]
then
    echo "-lt左侧数小于右侧数返回true"
    
fi

a=5
if [ $a -ge $b ]
then
    echo "-ge左侧数大于等于右侧数返回true"
    
fi

b=5
if [ $a -le $b ]
then
    echo "-le左侧数小于等于右侧数返回true"
fi


关系运算.png

字符串运算

shell符号 运算符含义
= 两个字符串相等返回true
-z 字符串长度为0返回true
-n 字符串长度不为0true
!= 两个字符串不相等返回true
str1="hello shell"
str2="hello shell"
if [ "$str1=$str2" ]
then
    echo "字符串相等返回true"
fi

str2="hello Python"  # 这里把str2变量重新赋值
if [ "str1 != str2" ]
then
    echo "字符串不相等返回true"
fi

str1=""
if [ "$str1 -z" ]
then
    echo "字符串长度为0返回true"
    
fi

st1="hello java"
if [ "str1 -n" ]
then
    echo "字符串长度不为0返回true"
fi

这里有个注意点:if [ "str1 -n" ]中 的字符串运算表达式必须加上双引号,否则会提示operator expected的错误

字符串.png

文件运算

shell符号 运算含义
-d file 检测文件是否是目录,是,返回true
-r file 检测文件是否可读,是,返回true
-w file 检测文件是否可写,是,返回true
-x file 检测文件是否可执行,是,返回true
-s file 检测文件是否为空,文件大小是否大于0,不为空返回true
-e file 检测文件(或目录)是否存在,是,返回true

示例:

filename="C:\Users\Administrator\Desktop\yangs"

if [  -d "$filename" ]  
then
    echo "-d file有目录,返回true"

fi
    
if [ -r "$filename" ]
then
    echo "-f file文件可读,返回true"
fi

if [ -w "$filename" ]
then
    echo "-w file文件可写,返回true"

fi

if [ -x "$filename" ]
then
    echo "-x file文件可执行,返回true"
fi

filename="C:\Users\Administrator\Desktop\yangsc"
if [ -s "$filename" ]
then
    echo "-f file文件不为空,返回true"
else:
    echo "file 为空"
fi

if [ -e "$filename" ]
then
    echo "-e file存在,返回true"
else
    echo "file文件不存在"
fi

这里需要注意的是:
文件运算.png

字符串拼接及长度

方法及说明 shell语法
拼接1 str1=nameurl
拼接2 str2="nameurl"
拼接3引号间可加内容 str3=name" "url"
拼接4 str4="name:url"
长度 ${#Val}
切片 ${Val:1:2}

这里拼接只示例第3种方式:

str1="I"
str2="shell"
str3=$str1"am"$str2  # 拼接
echo $str3

echo ${#str3}  # 获取长度

echo ${str3:1:2}  # 获取切片字符
拼接.png

数组

方法名称 shell代码
定义数组 array=(6 7 8)
获取下标 ${array[2]}
变量引用下标值 Var=${array[2]}
获取数组长度 length=${#array[*]}
array1=(6 7 8)
echo "这是数组"
index=${array1[2]}
echo "这是数组的第二个下标的值--->$index"
length=${#array1[*]}
echo "这是数组的长度--->$length"

这里的注意点是获取长度和获取下标{}大括号别忘了


数组.png
输出程序
方法名称 shell代码
换行输出 echo "str1 \nstr2"
重定向至指定文件 echo "hello shell" > xx.txt

注意点:

echo -e "hello \nshell"  # 换行输出

echo "hello shell" > shell2.txt  # 重定向

echo `date`  # 输出系统实时时间

echo用法.png
printf函数

打印功能,C语言的printf()类似
使用printf的脚本比echo更好,不会换行,所以需要手动添加\n
示例:

printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg  
printf "%-10s %-8s %-4.2f\n" 阿米 男 67.123 
printf "%-10s %-8s %-4.2f\n" 啊超 男 78.6543 
printf "%-10s %-8s %-4.2f\n" 阿福 男 48.9876

Printf函数.png
判断语句
a=1
b=2
c=1
if [ $a == $b ]
then
    echo "a和b相等"
elif [ $a != $c ]
then
    echo "a和c不相等"
else
    echo "类型不同"
fi

注意点:
if语句.png

for循环

格式:

for i in data/str/array/file
do
    echo $i
done

示例

data="h e l lo shell"
for str in $data  # data为字符串时,输出结果以空格界定单个元素
do
    echo $str
done

#data=(1 2 3)  # 错误写法
#for i in 1 2 3  

for i in 1 2 3  # 数组循环时不能在数组外加括号,并且不能用变量的形式表示数组
do
    echo $i
done
for循环.png
break跳出循环
名称 shell代码
跳出所有循环 break
跳出第N层f循环 break n
跳出当前循环 continue
for i in 1 2 3  
do
    if [ $i == 3 ]
    then
        break   # 当循环到3时,跳出循环,3不会被输出
    fi
    
    echo $i
    
done
break循环.png
函数

无参数函数



test(){  # 无参数无返回值函数
    echo "无参数函数hello shell"
}
test


test1(){  #无参数有返回值
    a=1
    return $a
}
test1
res=$?
echo $res


test2(){  # 有参数有返回值函数
      echo $1
      echo $2
}

test2 2 3
函数.png

重定向

#!/bin/bash
$echo res > file  #将结果写入文件,结果不会在控制台展示,而是在文件中,覆盖写
$echo res >> file  #将结果写入文件,结果不会在控制台展示,而是在文件中,追加写
echo input < file  #获取输入流

IO

名称 代码符号 句柄符号
标准输入 stdin 0
标准输出 stdout 1
stderr stderr 2


一个自动执行python的脚本

博主编写了这个脚本用来跑自己写的接口自动化框架,每执行一次框架都会先清空日志,再将本次的执行日志全部输出到bash终端界面

#  每次启动接口自动化框架前清空日志文件,并将最新的日志输出到bash终端
startpy(){
    for i in 1
    do
        cd ~
        cd d:
        cd untitled
        cd untitled
        cd shopvinterface
        
        cd Report
        true > logs  
        cd ..
        
        
        cd Run
        python RunCase.py
        cd ..
        cd Report
        
        cat logs
    done
    
}
startpy

效果是不是很酷!自从会了shell脚本,妈妈再也不用担心我要加班了。


shell脚本-用于跑自动化框架输出日志.png

文章部分有引用此文https://www.jianshu.com/p/71cb62f08768
及菜鸟教程

上一篇 下一篇

猜你喜欢

热点阅读