shell中的常用语句

shell中的常用语句

$0	##表示脚本本身
$1可以是个命令
$1 "命令"

for语句

for x in {1..7}/1,3,5/`seq 1 4`/`seq 1 2 10` 
do
done

seq 中间的是步长(差)
sleep 1	##一秒变一次
eg: 
echo -n	$t
echo -ne "\r"
sleep 1			##实现一秒变一次且不换行;倒计时效果

在这里插入图片描述

while语句

当条件成立做什么

while true 
do
done

until语句

until false
do
done
只有条件不满足时才跳出循环

if语句

if
then
elif
then
elif
then
...
else			##以上情况都不符合
fi

case语句

case  $1 in
	xx|xx)
	action1
	;;	
	xx)
	action2
	;;	
	*)
	last action
esac

expect语句

expect 是自动应答器,自动应答命令用于交互式命令的自动执行
spawn 是 expect 中的监控程序,其运行后会监控命令提出的
交互问题
send:发送问题答案给交互命令
"\r"表示回车
exp_continue 表示当问题不存在时继续回答下面的问题
expect eof 表示问题回答完毕退出 expect 环境
interact 表示问题回答完毕留在交互界面
set NAME [ lindex $argv n ]定义变量

vim ask.sh
#!/bin/bash
read -p "what's your name: " NAME
read -p "How old are you: " AGE
read -p "Which class you study: " CLASS
read -p "You feel happy or terrible ?" FEEL
echo $NAME is $AGE\'s old and $NAME is feel $FEEL

chmod  +x ask.sh
./ask.sh <<EOF
>xx
>xx
>xx
>xx
>EOF
##也可以得到回应

vim ask.exp
#!/usr/bin/expect
set NAME [ lindex $argv 0 ]
spawn /ask.sh		
expect {
	"*name:"  { send "$NAME\r" ; exp_continue}
	"*old*"	  { send "18\r" ; exp_continue }
	"*study:" { send "linux\r" ; exp_continue }
	"*feel*"  { send "happy\r"}
}
expect eof



set xxx [lindex $argv 0]	##定义一个变量xxx为xx.exp后跟的第一个字符
set xxx [lindex $argv 1]

set timeout 5	##等待5s应答,不回应就跳过

将expect整合到shell环境

vim ask_exp.sh
#!/bin/bash
/usr/bin/expect <<EOF
set NAME [ lindex $argv 0 ]
spawn /ask.sh		
expect {
	"*name:"  { send "$NAME\r" ; exp_continue}
	"*old*"	  { send "18\r" ; exp_continue }
	"*study:" { send "linux\r" ; exp_continue }
	"*feel*"  { send "happy\r"}
}
expect eof
EOF

脚本中的语句控制器

exit n		##脚本退出,退出值为 n
break		##退出当前循环
continue	##提前结束循环内部的命令,但不终止循环

otehers

ctrl+v+m = ^M	##unix下换行符 ^M 的拼写
a||b&&c			##a不成立的情况下执行b,b成功的情况下执行c
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
shell,可以使用条件语句来判断一个条件是否成立,从而控制程序的执行流程。 常用的条件语句有以下几种: 1. if语句 if语句用于判断一个条件是否成立,如果成立则执行指定的代码块,否则跳过。 if语句的语法如下: ``` if [ condition ] then # code to be executed if condition is true fi ``` 其,`condition`是一个表达式,如果表达式的值为true,则执行`then`和`fi`之间的代码块。 例如,判断一个变量是否为空: ``` if [ -z "$var" ] then echo "var is empty" fi ``` 2. if-else语句 if-else语句用于在判断一个条件的情况下,同时执行两个不同的代码块。 if-else语句的语法如下: ``` if [ condition ] then # code to be executed if condition is true else # code to be executed if condition is false fi ``` 其,`condition`是一个表达式,如果表达式的值为true,则执行`then`和`else`之间的代码块,否则执行`else`和`fi`之间的代码块。 例如,判断一个变量是否为空并输出不同的提示信息: ``` if [ -z "$var" ] then echo "var is empty" else echo "var is not empty" fi ``` 3. if-elif-else语句 if-elif-else语句用于在判断多个条件的情况下,同时执行多个不同的代码块。 if-elif-else语句的语法如下: ``` if [ condition1 ] then # code to be executed if condition1 is true elif [ condition2 ] then # code to be executed if condition2 is true else # code to be executed if all conditions are false fi ``` 其,`condition1`和`condition2`是两个表达式,如果`condition1`的值为true,则执行`then`和`elif`之间的代码块;如果`condition1`的值为false且`condition2`的值为true,则执行`elif`和`else`之间的代码块;否则执行`else`和`fi`之间的代码块。 例如,判断一个变量的值是否等于某个字符串并输出不同的提示信息: ``` if [ "$var" = "hello" ] then echo "var is hello" elif [ "$var" = "world" ] then echo "var is world" else echo "var is neither hello nor world" fi ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值