bash shell

本文介绍了Shell脚本中的变量使用,包括赋值、输出及运算,强调了变量操作的注意事项。同时讲解了字符串操作,如截取、删除子字符串。此外,还涉及到条件测试命令的用法,如数值和字符串的比较,并展示了条件分支结构的实例。最后,文章提到了while、until循环和for循环的使用,并简单介绍了Shell函数的基本概念。

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

1 shell

shell变量,一般shell的变量赋值的时候不用带“$”,而使用或者输出的时候要带“$”。加减乘除的时候要加两层小括号。括号外面要有一个“$”,括号里面的变量可以不用“$”。需要注意的是,变量赋值,变量使用的时候不能有空格,否则会被解析成命令,报错无此命令。

#!/bin/bash

a=10
b=20
c="this is a test"
d=$((a+b))
e=$((a-b))
f=$((a*b))
g=$((a/b))
h=$((a%b))
i=$((a*3))

echo $c
echo "a = "$a          #输出a的值
echo "b = "$b          #输出b的值
echo "a+b = "${d}      #输出a+b的值
echo "a+b = "$((a+b))  #输出a+b的值
echo $((a+b*a-b/a+a%b+a*2)) #表达式可以很长



sh test.sh
output

this is a test
a = 10
b = 20
a+b = 30
a+b = 30
238

2 shell变量表达式

#!/bin/bash

str="a b c d e f g h i j"

echo "the source string is "${str}                         
echo "the string length is "${#str}                       
echo "the 6th to last string is "${str:5}                  
echo "the 6th to 8th string is "${str:5:2}                 
echo "after delete shortest string of start is "${str#a*f} 
echo "after delete widest string of start is "${str##a*}   
echo "after delete shortest string of end is "${str%f*j}   
echo "after delete widest string of end is "${str%%*j}    



the source string is a b c d e f g h i j
the string length is 19
the 6th to last string is  d e f g h i j
the 6th to 8th string is  d
after delete shortest string of start is  g h i j
after delete widest string of start is 
after delete shortest string of end is a b c d e
after delete widest string of end is 

脚本运行报错

1

2

# sh test.sh

test.sh: 2: test.sh: Bad substitution

   原因

  从 ubuntu 6.10 开始,ubuntu 就将先前默认的bash shell 更换成了dash shell;其表现为 /bin/sh 链接倒了/bin/dash而不是传统的/bin/bash。
解决方法:bash ./test方式运行

3 Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

https://www.runoob.com/linux/linux-shell-test.html

数值测试

参数说明
-eq等于则为真
-ne不等于则为真
-gt大于则为真
-ge大于等于则为真
-lt小于则为真
-le小于等于则为真

 

字符串测试

参数说明
=等于则为真
!=不相等则为真
-z 字符串字符串的长度为零则为真
-n 字符串

字符串的长度不为零则为真

 

num1="ru1noob"
num2="runoob"
if test $num1 = $num2
then
    echo '两个字符串相等!'
else
    echo '两个字符串不相等!'
fi

四.shell条件分支结构语句

#!/bin/bash

echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
fi




echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
else
echo "this file is not a ordinary file."
fi




#!/bin/bash

echo "Please input your math grades"
read grades

if [ $grades -gt 100 ] || [ $grades -lt 0 ];then
echo "Please input the number range in 0 - 100"
fi

if [ $grades -ge 90 ] && [ $grades -le 100 ];then
echo "Your grade is excellent."
elif [ $grades -ge 80 ] && [ $grades -le 89 ];then
echo "Your grade is good."
elif [ $grades -ge 70 ] && [ $grades -le 79 ];then
echo "Your grade is middle."
elif [ $grades -ge 60 ] && [ $grades -le 69 ];then
echo "Your grade is passing."
else
echo "Your grade is badly."
fi

五.shell循环语句

 

#!/bin/bash
#$1 is  sh test4.sh 10
i=$1
while [ $i -gt 0 ]
do
echo $i
((i--))
done

1.while语句

while语句是只要条件为真就执行下面语句。
格式:
while 条件
do
语句
done


#!/bin/bash
i=$1
until [ $i -le 0 ]
do
echo $i
((i--))
done

until语句是只要条件为假就执行下列语句
格式:
until 条件
do
语句
done

#!/bin/bash
for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
   echo $i
done

格式:
for 变量 in 列表
do
语句
done

六.shell函数

格式:
[function] funcName()
{
语句
[return 返回值]
}
返回值是可选的,如果没有显示return 则默认返回最后一条语句执行的结果

Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。

如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值

#!/bin/bash

#打印数字
printNum()
{
   echo $1
}

for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
printNum $i
done



link:https://blog.youkuaiyun.com/qq_18297675/article/details/52693464

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值