Shell从入门到...

本文介绍了Linux中的Shell脚本基础知识,包括什么是Shell脚本、如何编写及执行脚本、基本语法、数值计算、条件判断(如if-else、case)、循环结构(while、for)以及函数的使用。此外,还涉及到了参数传递和交互式脚本的编写方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值