一 test条件判断
test命令可用于评估bash脚本中的表达式。它评估其参数所指定的表达式,如果表达式
为true,返回零退出状态,如果表达式为false,则返回非零退出状态。test具有替代语
法,使用方括号"[]"将表达式括起来,这样更易于阅读。
语法:test EXPRESSION 或 [EXPRESSION]
【1】非零或零长度字符串运算符:test -{n|z} STRING
[root@desktop ~]# a=1
[root@desktop ~]# test -n "$a" ;echo $?
0 ###查看"$a"是否为非0,显示退出状态。是非0,退出为0
[root@desktop ~]# test -n "$b" ;echo $?
1 ###查看"$b"是否为非0,显示退出状态。$b不存在。退出为1
[root@desktop ~]# test -z "$a" ;echo $?
1 ###查看"$a"是否为0,显示退出状态。不是0,退出为1
[root@desktop ~]# [ -n "$a" ]&& echo yes
yes ###判断"$a"是否为存在,存在显示yes。
[root@desktop ~]# [ -n "$b" ]&& echo yes||echo no
no ###判断"$b" 是否为存在,存在显示yes不存在显示no。
[root@desktop ~]# [ -z "$b" ]&& echo yes||echo no
yes ###判断"$b"是否为不存在,不存在显示yes存在显示no。
[root@desktop ~]# [ -z "$a" ]&& echo yes ||echo no
no ###判断"$a"是否为不存在,不存在显示yes存在显示no
[root@desktop ~]# cd /mnt
[root@desktop mnt]# ls
[root@desktop mnt]# vim check_ip.sh
###################start#####################
#!/bin/bash
[ -n "$*" ] &&(
ping -c1 -w1 $* &> /dev/null && echo $* is up || echo $* is down
)||(
echo "please give me a ipaddress"
)
###################end#####################
-n "$*" 查看"$*"存在吗?
&& ##判断命令
ping -c1 -w1 $* &> /dev/null && echo $* is up || echo $* is down ###存在; 查看能否ping通,通显示此主机开机,不通,显示此主机关机 ;且每隔1秒换行,每个一秒执行ping命令。
echo "please give me a ipaddress" ###不存在;显示请给我一个IP地址
[root@desktop mnt]# chmod +x /mnt/check_ip.sh
[root@desktop mnt]# /mnt/check_ip.sh
please give me a ipaddress
[root@desktop mnt]# /mnt/check_ip.sh 172.25.254.44
172.25.254.44 is up
[root@desktop mnt]# vim check_ip1.sh
[1]+ Stopped vim check_ip1.sh
[root@desktop mnt]# vim check_ip.sh
[root@desktop mnt]# fg
vim check_ip1.sh
###################start#####################
#!/bin/bash
while
[ -z "$*" ]
do
echo "please give me a ipaddress"
exit 1 ###给了IP后-z "$*"不为0,退出为1
done
ping -c1 -w1 $* &> /dev/null && echo $* is up || echo $* is down
###################end#####################
[root@desktop mnt]# chmod +x /mnt/check_ip1.sh
[root@desktop mnt]# /mnt/check_ip1.sh
please give me a ipaddress
[root@desktop mnt]# /mnt/check_ip1.sh 172.25.254.44
172.25.254.44 is up
【2】字符串比较运算符:=、!=
[root@desktop mnt]# cd
[root@desktop ~]# a=1
[root@desktop ~]# b=1
[root@desktop ~]# [ "$a" = "$b" ]&& echo yes ||echo no
yes
[root@desktop ~]# b=2
[root@desktop ~]# [ "$a" = "$b" ]&& echo yes ||echo no
no
[root@desktop ~]# [ "$a" != "$b" ]&& echo yes ||echo no
yes
[root@desktop ~]# [ ! "$a" = "$b" ]&& echo yes ||echo no
yes ### ! 判断等式不成立,判断正确
[root@desktop ~]# [ ! "$a" != "$b" ]&& echo yes ||echo no
no ### ! 判断等式不成立,判断错误
【3】数字比较运算符:-eq、-ne、-lt、-le、-gt、-ge
初始值:a=1;b=2;
[root@desktop ~]# [ "$a" -eq "$b" ]&& echo yes ||echo no
no ###eq 等于
[root@desktop ~]# [ "$a" -le "$b" ]&& echo yes ||echo no
yes ###le 小于等于
[root@desktop ~]# b=1
[root@desktop ~]# [ "$a" -le "$b" ]&& echo yes ||echo no
yes
[root@desktop ~]# [ "$a" -lt "$b" ]&& echo yes ||echo no
no ###lt 小于
[root@desktop ~]# [ "$a" -gt "$b" ]&& echo yes ||echo no
no ###gt 大于
[root@desktop ~]# [ "$a" -ge "$b" ]&& echo yes ||echo no
yes ###gt 大于等于
[root@desktop ~]# [ "$a" -ne "$b" ]&& echo yes ||echo no
no ###ne 不等于
[root@desktop ~]# b=2
[root@desktop ~]# [ "$a" -ne "$b" ]&& echo yes ||echo no
yes
[root@desktop ~]# cd
[root@desktop ~]# cd /mnt
[root@desktop mnt]# ls
check_ip1.sh check_ip.sh
[root@desktop mnt]# vim check_num.sh
###################start#####################
#!/bin/bash
while
[ -z "$*" ]
do
echo "please give me two number" ##判断[ -z "$*" ]正确,显示"please give me two number"
exit 1 给了值之后判断[ -z "$*" ]错误,退出状态为1
done
NUM=$[$1+$2] ##判断[ -z "$*" ]错误,将两数相加求和,
[ $NUM -le 10 ] && echo "the results is smaller than 10" || echo "the results is biger than 10" ###比较和与10的大小,比10小显示the results is smaller than 10;比10大显示the results is biger than 10;
###################end#####################
[root@desktop mnt]# chmod +x check_num.sh
[root@desktop mnt]# /mnt/check_num.sh
please give me two number
[root@desktop mnt]# /mnt/check_num.sh 2 3
the results is smaller than 10
[root@desktop mnt]# /mnt/check_num.sh 2 10
the results is biger than 10
[root@desktop mnt]# vim check_num1.sh
###################start#####################
#!/bin/bash
while
[ -z "$1" -o -z "$2" ] ###判断 "$1" 或者 "$2"有一个不存在
do
echo "please give me two number after /mnt/check_num1.sh
you can use /mnt/check_num1.sh like this:
[/mnt/check_num1.sh num1 num2]"
exit 1
done
((NUM=$1+$2)) ###"$1" 或者 "$2"两个都存在
while
[ "$NUM" -lt 10 ]
do
echo "$1+$2 is smaller than 10"
exit 0
done
echo "$1+$2 is biger than 10"
###################end#####################
[root@desktop mnt]# chmod +x check_num1.sh
[root@desktop mnt]# /mnt/check_num1.sh
please give me two number after /mnt/check_num1.sh
you can use /mnt/check_num1.sh like this:
[/mnt/check_num1.sh num1 num2]
[root@desktop mnt]# /mnt/check_num1.sh -2 3
-2+3 is smaller than 10
[root@desktop mnt]# /mnt/check_num1.sh -2 20
-2+20 is biger than 10
【4】文件状态运算符:test -{b|c|e|f|d|r|w|x|s|L} FILE/DIRECTORY
查看文件的形式
|r|w|x| ###测试文件是否是可读可写可执行文件
[root@server0 ~]# [ -b /dev/sda ]; echo $?
1 ####-b 检测文件是否是块设备
[root@server0 ~]# [ -c /dev/zero ]; echo $?
0 ####-b 检测文件是否是字符设备
[root@server0 ~]# [ -e /etc/passwd ]; echo $?
0 ###-e 测试文件是否存在
[root@server0 ~]# [ -f /etc/passwd ]; echo $?
0 ###-f 测试文件是否为普通文件
[root@server0 ~]# [ -d /etc/passwd ]; echo $?
1 ###-d 测试文件是否为目录
[root@server0 ~]# [ -L /etc/passwd ]; echo $?
1
[root@desktop mnt]# [ -b "/dev/vdb" ]&& echo yes ||echo no
yes
[root@desktop mnt]# [ -c "/dev/pts/0" ]&& echo yes ||echo no
yes
[root@desktop mnt]# [ -e "/mnt" ]&& echo yes ||echo no
yes
[root@desktop mnt]# [ -e "/m" ]&& echo yes ||echo no
no
[root@desktop mnt]# [ -d "/mnt" ]&& echo yes ||echo no
yes
[root@desktop mnt]# [ -f"/mnt" ]&& echo yes ||echo no
yes
[root@desktop mnt]# [ -x "/mnt" ]&& echo yes ||echo no
yes
[root@desktop mnt]# [ -s "/dev/pts/0" ]&& echo yes ||echo no
no
[root@desktop mnt]# touch file
[root@desktop mnt]# [ -s "/mnt/file" ]&& echo yes ||echo no
no ###-s 测试文件长度是否为0
[root@desktop mnt]# echo ll>file
[root@desktop mnt]# [ -s "/mnt/file" ]&& echo yes ||echo no
yes
[root@desktop mnt]# ln -s /mnt/file /mnt/file1
[root@desktop mnt]# [ -L "/mnt/file" ]&& echo yes ||echo no
no ###-L 测试文件是否为链接
[root@desktop mnt]# [ -L "/mnt/file1" ]&& echo yes ||echo no
yes
[root@desktop mnt]# [ -h "/mnt/file" ]&& echo yes ||echo no
no
[root@desktop mnt]# vim check_file.sh
###################start#####################
#!/bin/bash
while [ -z "$*" ] 判断/mnt/check_file.sh之后是否为空
do ###为空作:
echo "please give me an file"
exit 1
done ###不为空
[ -e "$1" ] || (echo $1 is not exist!;exit 1)##当文件不存在是显示文件不存在
[ -d "$1" ] && echo "This file is direc" ###-d 测试文件是否为目录
[ -c "$1" ] && echo "This file is ASCII"
[ -s "$1" ] && echo "This file is sockt" ###-s 测试文件长度是否为0
[ -L "$1" ] && echo "This file is link" ###-L 测试文件是否为链接
[ -f "$1" ] && echo "This file is file" ###-f 测试文件是否为普通文件
[ -b "$1" ] && echo "This file is block"
[ -e "$1" ] && echo "not file"###-e 测试文件是否存在
##############end######################
[root@desktop mnt]# /mnt/check_file.sh /mnt
This file is direc
This file is sockt
[root@desktop mnt]# /mnt/check_file.sh
please give me an file
[root@desktop mnt]# /mnt/check_file.sh /m
/m is not exist!
[root@desktop mnt]# /mnt/check_file.sh /dev
This file is direc
This file is sockt
[root@desktop mnt]# /mnt/check_file.sh /mnt/file1
This file is sockt
This file is link
This file is file
【5】二进制文件运算符:-ef、-nt、-ot
[root@server0 bin]# [ /bin/mount -ef /usr/bin/mount ]; echo $?
0 #######-ef 两个文件是否相同
[root@server0 bin]# [ /bin/mount -nt /usr/bin/mount ]; echo $?
1 ######### -nt 第一个文件是否比第二个新
[root@server0 bin]# [ /bin/mount -ot /usr/bin/mount ]; echo $?
1 ######### -ot 第一个文件是否比第二个旧
【6】逻辑运算符:-o、-a、!、&&、||
[root@server0 bin]# [ 2 -gt 1 -a 1 -gt 2 ]; echo $?
1 ###判断两个等式都成立,显示退出状态 -a 与关系
[root@server0 bin]# [ 2 -gt 1 -o 1 -gt 2 ]; echo $?
0 ##判断两个等式其中一个成立,显示退出状态 -o 或关系
[root@server0 bin]# [ ! 2 -gt 1 ]; echo $?
1 ##! 非关系 判断等式不成立,显示退出状态
二 if语句
if命令检查if后面的命令或列表的退出值。如果第一个命令评估为true/零,则运行then
之后的命令列表,直至任一else。如果第一个命令评估为false/非零,则运行else与fi之
间的命令列表(反向平写if,标记if块的结束)。
语法:if command; then command; command2; else command3; fi
示例:
[root@desktop ~]# a=1
[root@desktop ~]# if
> [ "$a" -eq "1" ]
> then
> echo yes
> elif
> [ "$a" -eq "2" ]
> then
> echo yes 2
> else
>
> echo no
> fi
yes
[root@desktop ~]# if [ "$a" -eq "1" ]; then echo yes; elif; [ "$a" -eq "2" ]; then echo yes 2; else echo no; fi
-bash: syntax error near unexpected token `;' ###if命令不能是这种方式
[root@desktop mnt]# vim /mnt/check_file1.sh
##############start######################
#!/bin/bash
if
[ -z "$*" ] ###判断/mnt/check_file1.sh之后是否为空
then ####为空
echo "user: /mnt/check_file1.sh file|directory"
elif
[ ! -e "$*" ] ###判断/mnt/check_file1.sh之后文件是不存在的
then ###判断正确
echo "$* is not exist!! "
elif
[ -L "$*" ] ###判断/mnt/check_file1.sh之后文件是链接
then ###判断正确
echo "$* is a soft link"
else ###判断错误
echo "no"
fi
##############end######################
[root@desktop mnt]# /mnt/check_file1.sh
-bash: /mnt/check_file1.sh: Permission denied
[root@desktop mnt]# chmod +x /mnt/check_file1.sh
[root@desktop mnt]# /mnt/check_file1.sh
user: /mnt/check_file1.sh file|directory
[root@desktop mnt]# /mnt/check_file1.sh /m
/m is not exist!!
[root@desktop dev]# /mnt/check_file1.sh /etc/system-release
/etc/system-release is a soft link
[root@desktop dev]# /mnt/check_file1.sh /mnt
no
[root@desktop dev]# /mnt/check_file1.sh /dev
no
三 case语句
case语句 :它能够把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪
部分代码。
case "$1" in
start)
systemctl start $2
;;
stop)
systemctl stop $2
;;
reload|restart)
systemctl stop $2
systemctl start $2
;;
*)
echo "Usage: $0 (start|stop|restart|reload)"
;;
esac
[root@desktop mnt]# vim ctrl_user.sh
[root@desktop mnt]# chmod +x ctrl_user.sh
[root@desktop mnt]# vim file
[root@desktop mnt]# cat file
westos
linux
[root@desktop mnt]# /mnt/ctrl_user.sh
INPUT: ctrl_user.sh create|delete userfile
[root@desktop mnt]# /mnt/ctrl_user.sh delete
please give me a userfile
[root@desktop mnt]# /mnt/ctrl_user.sh create
please give me a userfile
[root@desktop mnt]# /mnt/ctrl_user.sh create /mnt/file
Changing password for user westos.
passwd: all authentication tokens updated successfully.
Changing password for user linux.
passwd: all authentication tokens updated successfully.
[root@desktop mnt]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)
[root@desktop mnt]# id linux
uid=1002(linux) gid=1002(linux) groups=1002(linux)
[root@desktop mnt]# /mnt/ctrl_user.sh delete /mnt/file
[root@desktop mnt]# id linux
id: linux: no such user
[root@desktop mnt]# id westos
id: westos: no such user
[root@desktop mnt]# /mnt/ctrl_user.sh delete /mnt/f
/mnt/f is not exist
[root@desktop mnt]# /mnt/ctrl_user.sh create /mnt/file
Changing password for user westos.
passwd: all authentication tokens updated successfully.
Changing password for user linux.
passwd: all authentication tokens updated successfully.
[root@desktop mnt]# /mnt/ctrl_user.sh create /mnt/file
westos is exist
linux is exist
##############start ######################
#!/bin/bash
case $1 in
create) ###建立用户
if ##如果
[ -z "$2" ] 查看"$2"是否不存在
then ##不存在,然后
echo "please give me a userfile" ##显示给一个userfile
elif ###否则
[ ! -e "$2" ] 查看"$2" 是否正确
then ####不正确
echo $2 is not exist ###显示 文件不存在
else ###其他 ,文件存在且正确
for NAME in `cat $2` ###用户名字从文件中查看
do
USER=`getent passwd $NAME`
if
[ -z "$USER" ]
then ###用户不存在
useradd $NAME ###建立用户
echo westos | passwd --stdin $NAME ##修改密码
else
echo $NAME is exist ###用户已存在 ,显示已存在
fi
done
fi
;;
delete) ####删除
if
[ -z "$2" ]查看"$2"是否不存在
then##不存在,然后
echo please give me a userfile##显示给一个userfile
elif###否则
[ ! -e "$2" ]查看"$2" 是否正确
then####不正确
echo $2 is not exist###显示 文件不存在
else
for NAME in `cat $2` ###用户名字从文件中查看
do userdel -r $NAME ##删除用户
done
fi
;;
*)
echo "INPUT: ctrl_user.sh create|delete userfile" 上面情况都不是;显示INPUT: ctrl_user.sh create|delete userfile
esac
##############end######################
四 使用函数
pathmunge () {
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
}
...
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
[root@desktop mnt]# vim ctrl_user1.sh
[1]+ Stopped vim ctrl_user1.sh
[root@desktop mnt]# vim ctrl_user.sh
[root@desktop mnt]# fg
vim ctrl_user1.sh
[root@desktop mnt]# chnod +x ctrl_user1.sh
bash: chnod: command not found...
[root@desktop mnt]# chmod +x ctrl_user1.sh
[root@desktop mnt]# /mnt/ctrl_user1.sh
INPUT: ctrl_user1.sh create|delete userfile
[root@desktop mnt]# /mnt/ctrl_user1.sh create
please give me a userfile
[root@desktop mnt]# /mnt/ctrl_user1.sh create /mnt/file
westos exist
linux exist
[root@desktop mnt]# /mnt/ctrl_user1.sh delete /mnt/file
[root@desktop mnt]# /mnt/ctrl_user1.sh create /mnt/file
Changing password for user westos.
passwd: all authentication tokens updated successfully.
Changing password for user linux.
passwd: all authentication tokens updated successfully.
[root@desktop mnt]# /mnt/ctrl_user1.sh create /mn
/mn is not exist
##############start######################
#!/bin/bash
ACTION () { 建立函数
if
[ -z "$4" ] ###"$4" 是否为空
then ###"$4" 为空
echo "please give me a userfile" ##显示给一个userfile
elif ###"$4" 不为空,
[ ! -e "$4" ]###查看文件是否正确
then ###文件不正确
echo $4 is not exist ####显示文件不存在
else###文件正确
for NAME in `cat $4`###用户名字从文件中查看
do
USER=`getent passwd $NAME`
if
[ $1 "$USER" ] ###是否建立|删除用户
then
$2 $NAME ###建立用户,修改密码
[ "$2" = "useradd" ]&& (echo westos | passwd --stdin $NAME)
else ###否则,删除用户
echo $NAME $3
fi
done
fi
}
if
[ "$1" = "create" ]
then
ACTION -z "useradd" 'exist' $2 ####调用函数
elif
[ "$1" = "delete" ]
then
ACTION -n "userdel -r" 'not exist' $2 ####调用函数
else
echo "INPUT: ctrl_user1.sh create|delete userfile"
fi
##############end######################
五 expect语句
在shell中利用expect实现自动应答脚本。
# cat talk
echo "who are you?"
read who
echo "hello, $who"
echo "are you
happy?"
read answer
echo "why?"
read answer
# cat auto
#!/usr/bin/expect
#set timeout 10
spawn ./talk
expect "who"
send "firefly\n"
expect "happy?"
send "Yes,I am happy.\n"
expect "why?"
send "任性!\n"
expect eof
exit
[root@desktop mnt]# vim ask.sh
###########
#!/bin/bash
read -p "Who are you: " USER
read -p "How old are you: " AGE
read -p "What's your class: " CLASS
read -p "Are you happy: " FEEL
echo $USER is ${AGE}\'s old study $CLASS and FEEL $FEEL
###########
[root@desktop mnt]# chmod +x ask.sh
[root@desktop mnt]# /mnt/ask.sh
Who are you: zhang
How old are you: 18
What's your class: linux
Are you happy: happy
zhang is 18's old study linux and FEEL happy
[root@desktop mnt]# yum install expect -y
[root@desktop mnt]# which expect
/usr/bin/expect
[root@desktop mnt]# vim answer.exp
[root@desktop mnt]# expect answer.exp
spawn /mnt/ask.sh
Who are you: zhang
How old are you: 18
What's your class: linux
Are you happy: happy
zhang is 18's old study linux and FEEL happy
#!/usr/bin/expect
这一行告诉操作系统脚本里的代码使用那一个shell来执行。
set timeout 10
设置后面所有的expect命令的等待响应的超时时间,单位为秒。
spawn talk
spawn是expect的内部命令,作用是给后面的shell指令加个壳,用来传递交互指令。
expect "who"
判断上次输出结果里是否包含“who”的字符串,如果有则立即返回,否则等待超时时间后返回。
send "westos\n"
执行交互动作,相当于手工输入"westos"。
expect eof
作用是在输出中搜索文件结束符,如果没有这一行,脚本会立即退出,得不到正确结果。
interact
执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。否则退出登录。
$argv 参数数组
expect脚本可以接受从bash传递过来的参数.可以使用[lindex $argv n]获得,n从0开始,分别表示第
一个,第二个,第三个....参数。
#######################
#!/usr/bin/expect###告诉操作系统脚本里的代码使用那一个shell来执行
spawn /mnt/ask.sh###用来传递交互指令
expect "Who"判断上次输出结果里是否包含“who”
send "zhang\r"执行交互动作,相当于手工输入
expect "How"
send "18\r"
expect "class"
send "linux\r"
expect "happy"
send "happy\r"
expect eof ###作用是在输出中搜索文件结束符
###########################
[root@desktop mnt]# vim answer1.exp
[root@desktop mnt]# expect answer1.exp yan 12 abc "no happy"
spawn /mnt/ask.sh
Who are you: yan
How old are you: 12
What's your class: abc
Are you happy: no happy
yan is 12's old study abc and FEEL no happy
#########################
#!/usr/bin/expect
set NAME [ lindex $argv 0 ]###设置变量NAME
set AGE [ lindex $argv 1 ]###设置变量AGE
set CLASS [ lindex $argv 2 ]###设置变量CLASS
set FEEL [ lindex $argv 3 ]###设置变量FEEL
spawn /mnt/ask.sh 监视/mnt/ask.sh
expect "Who" ####遇见 Who
send "$NAME\r" ####调用变量NAME
expect "How"
send "$AGE\r"
expect "class"
send "$CLASS\r"
expect "happy"
send "$FEEL\r"
interact 执行完成后保持交互状态
#############################
自动登陆:ipb不定密码确定
[root@desktop mnt]# vim ip.exp
##########################
#!/usr/bin/expect
set ip [ lindex $argv 0 ]###设置变量ip
spawn ssh root@$ip ####监视 ssh root@$ip
expect "yes/no" #####提问
send "yes\r" ####应答yes
expect "password:" #####提问
send "westos\r" ####应答密码
interact
##########################
[root@desktop mnt]# chmod +x /mnt/ip.exp
[root@desktop mnt]# /mnt/ip.exp 172.25.254.44
spawn ssh root@172.25.254.44
yes
root@172.25.254.44's password:
Last login: Wed Jun 21 23:04:40 2017 from 172.25.254.144
自动登陆:ipb不定密码不确定
[root@desktop mnt]# vim ip.exp
[root@desktop mnt]# /mnt/ip.exp 172.25.254.44 westos
spawn ssh root@172.25.254.44
yes
root@172.25.254.44's password:
Last failed login: Wed Jun 21 23:23:02 CST 2017 from 172.25.254.144 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Wed Jun 21 23:14:20 2017 from 172.25.254.144
[root@foundation44 ~]# logout
[root@desktop mnt]# /mnt/ip.exp 172.25.254.144 redhat
spawn ssh root@172.25.254.144
yes
root@172.25.254.144's password:
Last login: Wed Jun 21 11:27:27 2017 from 172.25.254.144
自动应答: /mnt/host
[root@desktop mnt]# vim host.sh
###################
#!/bin/bash
if [ -n "$*" ] ####查看"$*"是否为存在
then ####存在
MAX_LINE=`wc -l $* | awk '{print $1}'` ####设置文件内容的最大行数。取每行的第一端字节
for NUM in `seq 1 $MAX_LINE` ####设置变量NUM的取值
do
ip=`sed -n ${NUM}p $* | awk '{print $1}'` ###ip 取每行的第一段字节
PASS=`sed -n ${NUM}p $* | awk '{print $2}'`###PASS 取每行的第二段字节
echo ip is $ip ####显示格式
echo PASS is $PASS
done
fi
###########################
[root@desktop mnt]# vim host
[root@desktop mnt]# cat host
172.25.254.44 westos
172.25.254.144 rdhat
[root@desktop mnt]# chmod +x host.sh
[root@desktop mnt]# /mnt/host.sh /mnt/host
ip is 172.25.254.44
PASS is westos
ip is 172.25.254.144
PASS is redhat
自动应答: /mnt/host hostname
[root@desktop mnt]# vim host.sh
######################################
#!/bin/bash
if [ -n "$*" ]
then
MAX_LINE=`wc -l $* | awk '{print $1}'`
for NUM in `seq 1 $MAX_LINE`
do
IP=`sed -n ${NUM}p $* | awk '{print $1}'`
PASS=`sed -n ${NUM}p $* | awk '{print $2}'`
/mnt/ip.exp $IP $PASS hostname | tail -n 1 执行/mnt/ip.exp $IP $PASS hostname 命令,过滤最后一行
done
else
echo "Useage:check_host.sh <filename>"
fi
#######################################
[root@desktop mnt]# vim ip.exp
################################
#!/usr/bin/expect
set ip [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
set COMMAND [ lindex $argv 2 ] ####添加COMMAND 变量
spawn ssh root@$ip $COMMAND #####命令添加COMMAND
expect "yes/no"
send "yes\r"
expect "password: "
send "$PASS\r"
interact
####################################3
[root@desktop mnt]# /mnt/host.sh /mnt/host
foundation44.ilt.example.com
desktop44
六 环境变量
shell和脚本使用变量来存储数据 ,有些变量可以连同它们的内容传递给子进程,这些
变量我们称之为环境变量。
【1】使用env命令显示所有环境变量
[student@desktop44 ~]$ env
XDG_SESSION_ID=70
HOSTNAME=desktop44
SHELL=/bin/bash
TERM=xterm-256color
HISTSIZE=1000
USER=student
LS_COLORS=rs=0:di
使用set命令现实所有本地定义的shell变量
Bash启动脚本在用户登录的时候,会运行全局变量文件/etc/profile,和用户自定义变量文件
~/.bash_profile去初始化它们的环境变量。
/etc/profile #####全局变量文件
\_ /etc/profile.d/*.sh
~/.bash_profile #####用户自定义变量文件
\_ ~/.bashrc
\_ /etc/bashrc
环境变量三个级别:
(1)shell级别:更换shell后不存在
[root@desktop mnt]# echo $LINUX
[root@desktop mnt]# LINUX=redhat
[root@desktop mnt]# echo $LINUX
redhat
[root@desktop mnt]# bash
[root@desktop44 mnt]# echo $LINUX
(2)用户级别:切换用户后失效
修改 ~/.bash_profile
[root@desktop44 mnt]# vim ~/.bash_profile
[root@desktop44 mnt]# echo $LINUX
[root@desktop44 mnt]# exit
exit
[root@desktop mnt]# exit
logout
Connection to 172.25.254.144 closed.
[kiosk@foundation44 Desktop]$ ssh root@172.25.254.144
root@172.25.254.144's password:
Last login: Wed Jun 21 11:28:19 2017 from 172.25.254.144
[root@desktop44 ~]# echo $LINUX
redhat
[root@desktop44 ~]# su - student
[student@desktop44 ~]$ echo $LINUX
[student@desktop44 ~]$ exit
logout
(3)系统级别
修改:/etc/profile
[root@desktop44 ~]# vim /etc/profile
[root@desktop44 ~]# su - student
Last login: Wed Jun 21 13:16:30 EDT 2017 on pts/0
[student@desktop44 ~]$ echo $LINUX
redhat
【2】使用别名
(1)alias命令可以用来自定义属于自己的系统命令(临时生效)
[student@desktop44 ~]$ alias####查看别名
[student@desktop44 ~]$ alias xie='vim'####设置别名
[student@desktop44 ~]$ alias
(2)写入~/.bashrc 文件永久生效。
[root@desktop44 ~]# vim ~/.bashrc
[root@desktop44 ~]# source .bashrc #####使文件生效
[root@desktop44 ~]# alias
(3)删除别名: unalias mycomm
[root@desktop44 ~]# vim ~/.bashrc
[root@desktop44 ~]# source .bashrc
[root@desktop44 ~]# alias