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 |
|
原因
从 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