shell编程

shell数组

声明:
declear -a 数组名

赋值:
数组名=(值1 值2 值3 …)

取值:
${数组名[下标]}
${数组名[*]}

[root@centOS /]$ arry=(a b c d e f)
[root@centOS /]$ echo ${arry[1]}
b
[root@centOS /]$ echo ${arry[*]}
a b c d e f

shell中的输入输出标准文件
stdin标准输入
stdout标准输出
stderr标准错误输出
输入输出重定向
输入重定向command<file
输出重定向command>file
输出附加重定向command >>file

read命令,读入数据

read 变量1 【变量2】…

echo命令,输出数据
echo {$变量1}

[root@centOS /]# read x y
1 2
[root@centOS /]# echo $x
1
[root@centOS /]# read x y
1 2 3
[root@centOS /]# echo $y
2 3
[root@centOS /]# read x y z
1 2
[root@centOS /]# echo $z

#z没有被赋值

-e解析

[root@centOS /]# echo -e "Hello \c" ;echo World
Hello World
[root@centOS /]# echo -e "Hello \nWorld"
Hello
World

测试语句

用来测试语句来计算判断条件的值。返回真假。1、使用test关键子;2、使用方括号
text -f “$1”
[-f “$1” ] #操作符前后留一个空格
文件测试
-r文件存在且可读,返回为真
-w文件存在且可写,返回为真
-f 文件存在且是普通文件,返回为真
-d 文件存在且是目录文件,返回为真
-p 文件存在且是FIFO文件,返回为真
-s 文件存在且不为空,返回为真
如:test -f “$1”等价于 [-f “$1”]

字符串测试

str1 =str2 ,两字符串相等为真测试条件为真
str1 =str2 ,两字符串不相等为真测试条件为真
-n str 字符串的长度不为0
-z str 字符串的长度为0
test “$1”="$2" 等价于 ["$1"="$2"]#参数1字符串等于参数2的字符串

数值测试:test “$s1" -gt 10
-eq等于
-ne不等于
-lt小于
-le小于等于
-gt大于
-ge大于等于
[root@centOS /]# test 3 -lt 5
[root@centOS /]# echo $?
0

逻辑操作符进行组合的测试语句

a逻辑与 -o逻辑或 !逻辑非 ()圆括号用于表达式分组括号前后应有空格并用转义字符
[root@centOS /]# test 3 -lt 5 -a 10 -lt 5 #3<5且10<5
[root@centOS /]# echo $?
1
[root@centOS /]# test 3 -lt 5 -o 10 -lt 5 #3<5或10<5
[root@centOS /]# echo $?
0

shell程序的控制语句

if 判断条件
then 命令1
else 命令2
fi

#判断用户是否存在,并向它发送消息

echo "type in the user name"
read user
if who|grep $user
then echo "hello! $user"|write $user
else echo "$user has not logged in the system"
fi
if test -f "$1"
then echo "$1 is an ordinary file"
else echo "$1 is not an ordinary file"
fi

多路分支

if 判断条件
then 命令1
elif 判断条件
then 命令2

else 命令n
fi

#输入(1-10)之间的一个数,并判断他是否小于5

echo ‘key in a number(1-10)’
read a
if["$a" -lt 1 -o "$a" -gt 10]
then echo "Error Number"
elif[!"$a" -lt 5]
then echo "it's not less 5."
else echo "it's less 5."
fi

case语句

case string in
str1)
commands-list-1;;
str2)
commands-list-2;;

strn)
commands-list-n;;

case语句特点
每一个正则表达式唯一,不应该重复出现
case语句以case开头,以关键字esac结束

for语句

for variable [in argument-list] #[argument-list]为变量值表
do
command
done

for a in 1 2 3 4 5
do
echo $a
done

变量列表可以用$*代替,变量依次取位置参数值

for i in {n1..n2}
do
echo $i
done
for ((i=1;i<=5;i++))
do
echo $i
done

while语句

while expression
do
command-list
done

#判断是否是普通文件,若是,则显示内容;否则显示它不是文件名的信息。

while[$1]
do
if[ -f $1]
then echo "display:$1"
cat $1
else echo "$1 is not a file name."
fi
shift
done

利用while循环输出0到10之间的整数

x=1
while[$x -le 10]
do
echo $x
x='expr $x+1'
done

break和continue语句
break从循环中跳出
continue跳出本次循环回到本层循环的开头

算数运算

expr n1 运算符 n2
例如:expr 15 * 15

小括号
((n1 运算符 n2))
不用小括号需要转义运算符

let n = n1 运算符 n2

函数function

先定义后使用,直接使用函数名调用

function mul( ){
echo($1*$2)
}

mul $1 $2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈勇劲

你的鼓励实我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值