【编辑】【Ubuntu笔记】Liunx中的shell脚本

本文详细介绍了shell脚本的概念、基本语法,包括如何创建、执行脚本,使用数组、循环、条件判断,以及涉及的命令如echo、chmod、test、if-then、case语句、函数和各种类型的循环。还探讨了数值计算和文件测试的方法。
摘要由CSDN通过智能技术生成

摘要:

定义:将很多条命令放到一个文件里面,然后直接运行这个文件
shell 脚本类似windows的批处理文件, shell脚本就是将连续执行的命令写成一个文件。shell脚本提供数组、循环、条件判断的等功能。shell脚本一般是Linux运维或煮系统管理员要掌握的, 作为嵌入式开发人员,只需要掌握shell脚本最基础的部分即可。

shell脚本是个纯文本文件,命令从上而下,一行.-行的开始执行.shell脚本扩展名为.sh。shell脚本第一行一定要为:v
#!/bin/bash
表示使用bash。v

echo "Hello World!":在终端上显示一行字符串
chmod 777 my.sh

vi my.sh
    #!/bin/bash
    echo "Hello World!"
ls my.sh -l
#此时是没有可执行权限的
chmod 777 my.sh
ls my.sh -l
./my.sh

shell脚本

交互式shell脚本

#!/bin/bash
echo "please input name:"
read name
echo "your name :" $name

# !/bin/bash
read -p "input your age and height: " age height
echo "your age=$age, your height=$height"

 shell脚本数值计算

仅支持整型,数值计算使用$(表达式)

#!/bin/bash
echo "please input two int num:"
read -p "first num: " first
read -p "second nun: " second
total=$((first+$second))
echo "$first + $second = $total"

text命令

test命令用于查看文件是否存在、权限等信息,可以进行数值,字符,文件三方面的测试。
&&和||命令:
cmd1 && cmd2当cmd1执行完并且正确,那么cmd2开始执行,如果cmd1执行完毕错
误,那么 cmd2 不执行。
cmd1 || cmd2 当 cmd1 执行完并且正确,那么 cmd2不执行,反之 cmd2 执行。

判断文件是否存在

#!/bin/bash
echo "please input two name:"
read -p "first name:" filename
test -e $filename && echo "$filename exist" || echo "$filename no exits"

判断字符串是否相等

#!/bin/bash
echo "please input two string:"
read -p "first string:" firststr
read -p "second string:" secondstr
test $firststr == $secondstr && echo "firststr == secondstr" || echo "firststr != secondstr"

[]判断付符

[  ]只能输入==或者!=,[空格  空格],必须要加空格,变量必须要加引号

#!/bin/bash
echo "please input two string:"
read -p "first string:" firststr
read -p "second string:" secondstr
[ "Sfirststr" == "Ssecondstr" ] && echo "firststr == secondstr" || echo "firststr != secondstr"

默认变量

$0~$n,表示shell 脚本的参数,包括shell脚本命令本身, shlle脚本命令本身为$0

$#:#表示最后一个参数的标号。·
$@:表$1、$2、$3......
 

#!/bin/ bash
echo "file name:" $0
echo "total param num:"$#
echo "whole param:" $@
echo "first param:" $1
echo "second param:" $2

shell脚本条件判断、函数与循环

shell脚本支持条件判断,虽然可以通过&&和||来实现简单的条件判断,但是稍微复杂一点的场景就不适合了。shell脚本提供了if then条件判断语句,写法
if 条件判断: then

//判断成立要做的事情

fi

还有if then else 语句,写法

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

else
//条件判断不成立要做的事情。

fi
或:
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 0
elif [ "$value" == "N" ] || [ "$value" == "n"l; then
    echo "your input is N"
    exit 0
else
    echo "your input can't identify!"
fi


case语句

#!/bin/bash
case $1 in
    "a" )
        echo "paran is:a"
        ;;
    "b")
        echo "param is:b"
        ;;
    * )
        echo "can't identify! "
        ;;
esac

shell脚本函数

function fname(){

}

函数传参

print(){
    echo "param 1 $1"
    echo "param 2 $2"
}
print a b

shell循环

shell脚本也支持循环,比如 while do done,表示当条件成立的时候就一直循环,直到条件不成立。
while[条件]//括号内的状态是判断式do
//循环开始 //循环代码段 done
还有另外一种until do done,表示条件不成立的时候循环,条件成立以后就不循环了,写法如下:
until[条件] do //循环代码段 done
for循环,使用for循环可以知道有循环次数,写法for var in con1 con2 con3
do
//循环代码段 done

#!/bin/bash
while [ "$value" != "close"]
do
    read -p "please input str: " value
done
echo "stop while"

for循环

#!/bin/bash
for name in a b c d
do
    echo "your name : $name"
done
#!/bin/bash
read -p "please input count:" count
total=0
for((i=0; i<=count; i=i+1))
do
    total=s(($total+$i))
done
echo "1+2+. ..+$count=$total"

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值