首页 > 代码库 > shell2
shell2
实验12.
case语句
[root@web1 ~]# vim 大哥是个apple
case $1 in
apple)
echo banana
;;
banana)
echo apple
;;
*)
echo error
esac
测试:
[root@web1 ~]# sh 大哥是个apple apple
banana
[root@web1 ~]# sh 大哥是个apple banana
apple
[root@web1 ~]# sh 大哥是个apple yz
error
实验13.
expect自动应答脚本(输入ip和密码,制动远程连接主机)
[root@web1 ~]# yum install expect -y
Loaded plugins: langpacks
rhel_dvd | 4.1 kB 00:00
Resolving Dependencies
.....
[root@web1 mnt]# vim ty16.exp
#!/usr/bin/expect
set IP [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
spawn ssh root@$IP
expect {
"yes/no"
{send "yes\r";exp_continue}
"password:"
{send "$PASS\r"}
}
interact
[root@web1 mnt]# /mnt/ty16.exp 172.25.254.118 redhat
spawn ssh root@172.25.254.118
The authenticity of host ‘172.25.254.118 (172.25.254.118)‘ can‘t be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.25.254.118‘ (ECDSA) to the list of known hosts.
root@172.25.254.118‘s password:
Last login: Wed Dec 14 21:58:09 2016 from 172.25.254.18
[root@server18 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.118 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::5054:ff:fe00:120b prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:12:0b txqueuelen 1000 (Ethernet)
RX packets 16575 bytes 1458237 (1.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9690 bytes 1204275 (1.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
实验14.
教室11-25主机开着的显示主机名,未开就显示关闭
[root@web1 mnt]# vim ty16.exp
#!/usr/bin/expect
set timeout 2
set IP [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
set COMM [ lindex $argv 2 ]
spawn ssh root@$IP $COMM
expect {
"yes/no"
{send "yes\r";exp_continue}
"password:"
{send "$PASS\r"}
{send "$COMM\r"}
}
expect eof
[root@web1 mnt]# vim ty17.sh
#!/bin/bash
for NUM in {17..21}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && (
/mnt/ty16.exp 172.25.254.$NUM westos hostname | grep -E "spawn|root|denied" -v )
done
[root@web1 mnt]# sh ty17.sh
foundation20.ilt.example.com
实验15.
用户的密码匹配实验
[root@web1 mnt]# vim ty20.sh
#!/bin/bash
if
[ -n "$1" -a -n "$2" ]
then
if
[ -e "$1" -a -e "$2" ]
then
MAXUSER=`wc -l $1 | cut -d " " -f 1`
MAXPASS=`wc -l $2 | cut -d " " -f 1`
if
[ "$MAXUSER" -eq "$MAXPASS" ]
then
for NUM in $( seq 1 $MAXUSER )
do
USERNAME=`sed -n ${NUM}p $1`
PASSWORD=`sed -n ${NUM}p $2`
CKUSER=`getent passwd $USERNAME`
[ -z "$CKUSER" ]&&(
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
)||echo "$USERNAME exist !!"
done
else
echo $1 and $2 have different lines
fi
elif
[ ! -e "$1" ]
then
echo "ERROR:$1 is not exist"
else
echo "ERROR:$2 is not exist"
fi
else
echo "ERROR:Please input userfile and password file after command !!"
fi
[root@web1 mnt]# sh ty20.sh userfile passfile
Changing password for user westos.
passwd: all authentication tokens updated successfully.
Changing password for user redhat.
passwd: all authentication tokens updated successfully.
yz exist !!
##引用##
引用有三种类型
1.弱引用
将字符串放置在双引号中,保留字符串中所有字符的文字值,$、`、\和!字符除外。
echo "can I have a $FRUIT"
echo "the current time is $(date+%)."
2.强引用
将字符串放置在单引号中,保留字符串中所有字符的文字值,同时禁用所有扩展。
echo "make$$$Fast"
rm‘untitled folder‘
3.转义
非引用的\是转义字符。它保留了下一个字符的文字值。如\$PATH是确切的字符串$PATH,而不是PATH变量的内容
echo make\$\$\$Fast\!
ls untitled\folder
##shell变量##
若要定义或 指定值:
FRUIT=apple
若要参考或使用变量:
$FRUIT
${FRUIT}
test 和中括号的作用一样
-z为空
-n不为空
本文出自 “12288655” 博客,谢绝转载!
shell2