脚本中的函数
把一个复杂的语句块定义成一个字符串的方法
Host_Message()
{
read -p "PLease input your action: " Action
[ "Acton" == "exit" ] && exit 0
[ "Acton" == "user" ] && echo You are $USER
[ "Acton" == "hostname" ] && echo $HOST
Host_Message
}
Host_Message
eg:
expect命令
expect命令是自动应答命令用于交互式命令的自动执行,spawn是expect中的监控程序,其运行后会监控命令提出的交互问题
send 发送问题答案给交互命令
"\r" 表示回车
exp_cotinue 表示当前问题不存在时继续回答下面的问题
expect eof 表示问题回答完毕退出 expect环境
interact 表示问题回答完毕留在交互界面
set NAME [ LINDEX $argv n ] 定义变量
注意:我们要使用expect环境,就先要安装 expect 的包。expect的解释器是/usr/bin/expecteg:
#!/usr/bin/expect
spawn /mnt/ask.sh
set timeout 5
expect {
"name" { send "gaoqi\r"; exp_continue }
"old" { send "19\r"; exp_continue }
"objective" { send "student\r"; exp_continue }
"happy" { send "happy\r" }
}
expect eof
也可以使用变量的方式对问题进行赋值:
#!/usr/bin/expect
set Name [ lindex $argv 0 ]
set Old [ lindex $argv 1 ]
set Obj [ lindex $argv 2 ]
set Happy [ lindex $argv 3 ]
spawn /mnt/ask.sh
expect {
"name" { send "$Name\r"; exp_continue }
"old" { send "$Old\r"; exp_continue }
"objective" { send "$Obj\r"; exp_continue }
"happy" { send "$Happy\r" }
}
expect eof
#!/usr/bin/expect
spawn ssh 172.25.254.68
expect {
"connecting" { send "yes\r"; exp_continue }
"password" { send "westos\r" }
}
interact
#!/bin/bash
read -p "What's your name? " Name
read -p "How old are you? " Old
read -p "Which objective are you study? " Obj
read -p "Are you happy?" Happy
echo "$Name is $Old years old,and her job is $Obj fell $Happy!"
expect命令实现ssh自动连接
#!/usr/bin/expect
spawn ssh 172.25.254.68
expect {
"connecting" { send "yes\r"; exp_continue }
"password" { send "westos\r" }
}
interact
#!/bin/bash
/usr/bin/expect <<EOF
spawn ssh root@$1
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "$2\r" }
}
expect eof
EOF
注意:expect和shell环境无法互通。
脚本中语句的控制器
exit n 脚本退出,退出值为n(0~255)
break 提前退出当前循环
continue 提前结束循环内部的命令,但不终止循环
eg:
exit
#!/bin/bash
for i in {1..3}
do
if
[ "$i" = "2" ]
then
echo luck number
exit
echo $i
fi
echo $i
done
continue
#!/bin/bash
for i in {1..3}
do
if
[ "$i" = "2" ]
then
echo luck number
continue
echo $i
fi
echo $i
done
练习
- 输入分和秒开始倒计时
#!/bin/bash
read -p "Please input minute and second: " M S
clear
for((a=(M*60)+S;a>0;a--))
do
echo -n $[a/60]:$[a%60]
echo -ne " \r"
sleep 1
done
- 输入文件名,判断文件类型
#!/bin/bash
while true
do
read -p "Please input file you want to check: " File
[ -e "$File" ] || {
echo "$File is not exist!"
exit
}
if [ -d "$File" ]
then
echo "$File is a directory!"
elif [ -f "$File" ]
then
echo "$File is a file!"
elif [ -L "$File" ]
then
echo "$File is a link!"
elif [ -s "$File" ]
then
echo "$File is a scket!"
elif [ -b "$File" ]
then
echo "$File is a block!"
elif [ -c "$File" ]
then
echo "$File is a character!"
fi
done
运算方式及运算符号
#!/bin/bash
while true
do
echo "You an input +-.....|exit"
read -p "Please input action:" Action
[ "$Action" = "exit" ] &&{
echo bye
exit 0
}
read -p "Please input a number:" a
read -p "Please input another number:" b
bc <<EOF
$a $Action $b
EOF
done
#!/bin/bash
CAL()
{
echo "You an input +-.....|exit"
read -p "Please input action:" Action
[ "$Action" = "exit" ] &&{
echo bye
exit 0
}
read -p "Please input a number:" a
read -p "Please input another number:" b
bc <<EOF
$a $Action $b
EOF
CAL
}
CAL