Shell从入门到...

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Linux 教程 | 菜鸟教程

文章目录

什么是shell脚本

我们已经能够熟练的在终端中输入命令来完成一些常用的操作但是我们都是一条一条输入命令。这样会很麻烦那么有没有一种方式可以将很多条命令放到一个文件里面然后直接运行这个文件即可肯定有这个就是shell脚本

shell脚本类似windows的批处理文件shell脚本就是将连续执行的命令写成一个文件。

shell脚本提供数组、循环、条件判断的等功能。shell脚本一般是Linux运维或者系统管理员要掌握的作为嵌入式开发人员只需要掌握shell脚本最基础的部分即可。

shell脚本写法

shell脚本是个纯文本文件命令从上而下一行一行的开始执行。shell脚本扩展名为.sh。

shell脚本第一行一定要为:
#!/bin/bash
表示使用bash。

shell脚本语法

交互式shell脚本

read -p

shell脚本的数值计算

shell仅支持整形数值计算使用$((表达式))。

test命令

test命令用于查看文件是否存在、权限等信息可以进行数值字符文件三方面的测试。

&&和||命令

cmd1 && cmd2 当cmd1执行完并且正确那么cmd2开始执行如果cmd1执行完毕错误那么cmd2不执行。
cmd1 || cmd2 当cmd1执行完毕并正确那么cmd2不执行反之cmd2执行。

中括号[]判断符

[ ] == !=

#!/bin/bash
echo "please input two string:"
read -p "first string:" firststring
read -p "second string:" secondstring
#判断两个字符串是否相等   不管是变量还是字符串都要加上 ""
[ "$firststring" == "$secondstring" ] && echo "firststring == secondstring" || echo "firststring != secondstring"
[ "$firststring" == "a b" ] && echo "firststring == a b" || echo "firststring != a b"

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
please input two string:
first string:qwer
second string:qwer
firststring == secondstring
firststring != a b

默认变量

0   0~ 0 n表示shell脚本的参数包括shell脚本命令本身shlle脚本命令本身为$0
$##表示最后一个参数的标号。
$@表$1、$2、$3…

#!/bin/bash

#输出shell脚本的文件名
echo "file name:" $0
#输出整个参数的数量
echo "total param num:" $#
#输出整个参数的内容
echo "first param:" $1
echo "second param:" $2

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh a b
file name: ./test.sh
total param num: 2
first param: a
second param: b

shell脚本条件判断

if 条件判断 ; then
//判断成立要做的事情
fi

#!/bin/bash

read -p "please input(y/n):" value

# || 逻辑或  如果前面为真  后边不执行
#            如果前面为假  后边执行。
if [ "$value" == "Y" ] || [ "$value" == "y" ]; then
    echo "Your input is Y"
    exit
fi

if [ "$value" == "N" ] || [ "$value" == "n" ]; then
    echo "Your input is N"
    exit
fi

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
please input(y/n):Y
Your input is Y

还有if then else 语句写法
if 条件判断 ; then
//条件判断成立要做的事情
else
//条件判断不成立要做的事情。
fi

#!/bin/bash

read -p "please input(y/n):" value

if [ "$value" == "Y" ] || [ "$value" == "y" ]; then
    echo "Your input is Y"
    exit
else 
    echo "Your input is $value"
    exit
fi

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
please input(y/n)😒
Your input is s

if 条件判断 ; then
//条件判断成立要做的事情
elif [条件判断]; then
//条件判断成立要做的事情
else
//条件判断不成立要做的事情。
fi

#!/bin/bash

read -p "please input(y/n):" value

if [ "$value" == "Y" ] || [ "$value" == "y" ]; then
    echo "Your input is Y"
    exit
elif [ "$value" == "N" ] || [ "$value" == "n" ]; then
    echo "Your input is n"
    exit
else 
    echo "Your input is $value"
    exit
fi

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
please input(y/n):t
Your input is t
sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
please input(y/n):y
Your input is Y

case

最后还有case语句
case $变量 in
“第1个变量内容”)
程序段
;; //表示该程序块结束
“第2个变量内容”)
程序段;;
“第n个变量内容”)
程序段
;;
esac

#!/bin/bash

case $1 in
    "a")
    echo "the param is: a"
    ;;
    "b")
    echo "the param is: a"
    ;;
    *)
    echo "can't identify"
    ;;
esac

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh a
the param is: a
sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh b
the param is: a
sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh c
can’t identify

shell脚本函数

shell脚本也支持函数函数写法如下
function fname () {
//函数代码段
}

#!/bin/bash

function help(){
    echo "This is help cmd!"
}

function close(){
    echo "This is close cmd!"
}

case $1 in
    "-h")
    help
    ;;
    "-c")
    close
    ;;
esac

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh -c
This is close cmd!

shell脚本传参

#!/bin/bash

printf(){
    echo "para 1:$1"
    echo "para 2:$2"
}
printf a b

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
para 1:a
para 2:b

shell脚本循环

while循环

shell脚本也支持循环比如 while do done表示当条件成立的时候就一直循环直到条件不成立。

while [条件] //括号内的状态是判断式

do //循环开始
//循环代码段
done

#!/bin/bash

while [ "$value" != "close" ]
do
    read -p "please inpt str value:" value
done

echo "stop while"

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
please inpt str value:qwer
please inpt str value:assdf
please inpt str value:close
stop while

还有另外一种until do done表示条件不成立的时候循环条件成立以后就不循环了写法如下

until [条件]
do
//循环代码段
done

for循环

for循环使用for循环可以知道有循环次数写法

for var in con1 con2 con3……
do
//循环代码段
done

#!/bin/bash

for name in sdp sdp1 sdp2 sdp3
do
    echo "your name $name"
done

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
your name sdp
your name sdp1
your name sdp2
your name sdp3

for循环数值处理写法

for((初始值; 限制值; 执行步长))
do
//循环代码段
done

#!/bin/bash

read -p "please input count:" count

total=0
for ((i=0;i<=count;i++))
do    
    total=$(($total+$i))
done

echo "1+2+..+total = $total"

运行结果

sundp@sundongpeng:~/workspace/linux/c_program/January/Shell_test$ ./test.sh
please input count:100
1+2+…+total = 5050

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: shell