ontinue
break
# vim s25.sh
#!/bin/bash
for i in `seq 1 10`
do
if [ $i -eq 5 ]
then
continue
fi
echo $i
done
[test]#chmod +x s25.sh
[test]#./s25.sh
1
2
3
4
6
7
8
9
10
[test]#vim s25.sh
#!/bin/bash
for i in `seq 1 10`
do
if [ $i -eq 5 ]
then
break
fi
echo $i
done
You have new mail in /var/spool/mail/root
[test]#chmod +x s26.sh
[test]#./s26.sh
1
2
3
4
练习:输入用户名和密码 ,如果是root和redhat,输出“欢迎光临“,如果不正确,输出“不正确,请重新输入用户名和密码“。
# vim s27.sh
#!/bin/bash
while :
do
read -p "请输入用户名:" username
read -s -p "请输入密码:" password
if [ $username -eq "root" -a $password -eq "redhat" ]
then
echo "欢迎光临"
break
else
echo "不正确,请重新输入用户名和密码"
fi
done
~
练习: 限制尝试次数为5次,5次以后禁止使用。
# vim s28.sh
#!/bin/bash
i=1
while [ $i -le 5 ]
do
read -p "请输入用户名:" username
read -s -p "请输入密码:" password
echo
if [ $username == "root" -a $password == "redhat" ]
then
echo "欢迎光临"
break
elif [ $i -eq 5 ]
then
echo "你无权使用本程序"
break
else
echo "不正确,你还有$[5-$i]次机会,请重新输>入用户名和密码"
fi
i=$[$i+1]
done
# chmod +x s28.sh
# ./s28.sh
-------------------------------
> < | 结合done 用
1) >
# vim s29.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
done>/test/1.txt
# chmod +x s29.sh
[test]#./s29.sh
[test]#cat /test/1.txt
1
2
3
4
5
6
7
8
9
10
--------------------
2) < while循环
批量添加用户
[test]#vim user_list
utom utomg 2000 /utom /bin/bash utomutom
jarry jarryg 2001 /jarry /bin/zsh jarryjarry
larry larryg 2002 /larry /bin/tcsh larrylarry
#!/bin/bash
#add user scripts
while read line
do
uname=$(echo $line|awk '{print $1}')
gname=$(echo $line|awk '{print $2}')
uid=$(echo $line|awk '{print $3}')
home=$(echo $line|awk '{print $4}')
shell=$(echo $line|awk '{print $5}')
passwd=$(echo $line|awk '{print $6}')
grep "^\<$gname\>" /etc/group &>/dev/null
if [ $? -ne 0 ]
then
groupadd $gname
fi
id $uname &>/dev/null
if [ $? -ne 0 ]
then
useradd -g $gname -u $uid -d $home -s $shell $uname
echo $passwd |passwd --stdin $uname
fi
done
[test]#chmod +x user.sh
[test]#./user.sh
-----------------
练习: 分析/var/log/secure 日志文件,找出登录过自己机器的ip地址,根据登录次数排序。把前5个ip地址 输出到/test/ip.txt文件中。 用iptables 禁止这些ip地址访问自己的机器。iptables -I INPUT -s 来源ip地址 -p tcp --dport 22 -j DROP
# vim ipdeny
#!/bin/bash
grep 'Accepted password' /var/log/secure|awk '{print $11|"sort"}END{close("sort")}'|uniq -c|sort -n -r|sed -n '1,5p'>/test/ip.txt
while read line
do
ip=$(echo $line|awk '{print $2}')
iptables -I INPUT -s $ip -p tcp --dport 22 -j DROP
done
--------------------------
练习 把/test/file.sh 文件传到远程机器上,并执行。
# vim s30.sh
#!/bin/bash
while read ip
do
scp /test/file.sh $ip:/
ssh $ip sh /file.sh
done--------------------------------------
数组和函数
1) 定义数组
[root@teacher test]# sz1=(s1 s2 s3)
[root@teacher test]# echo ${sz1[0]}
s1
[root@teacher test]# echo ${sz1[1]}
s2
[root@teacher test]# echo ${sz1[2]}
s3
[root@teacher test]# echo ${sz1[@]}
s1 s2 s3
[root@teacher test]# echo ${sz1[*]}
s1 s2 s3
[root@teacher test]# echo ${#sz1[*]}
3
[root@teacher test]# sz2[1]=q1
[root@teacher test]# sz2[5]=q2
[root@teacher test]# sz2[8]=q3
[root@teacher test]# echo ${sz2[1]}
q1
[root@teacher test]# echo ${sz2[2]}
[root@teacher test]# echo ${sz2[3]}
[root@teacher test]# echo ${sz2[5]}
q2
[root@teacher test]# echo ${sz2[8]}
q3
[root@teacher test]# vim s31.sh
#!/bin/bash
declare -a arr
for i in `seq 1 5`
do
arr[$i]=$i
done
for data in ${arr[*]}
do
echo ${data}
done
# vim s32.sh
#!/bin/bash
declare -a name
i=1
while read line
do
name[$i]=`echo $line|awk -F: '{print $1}'`
i=$[$i+1]
done
for data in ${name[@]}
do
echo ${data}
done
# chmod +x s32.sh
# ./s32.sh
2) 函数
一定要先定义,再使用
格式: 函数名 () {commands;}
function 函数名 () {commands;}
[root@teacher test]# vim s33.sh
#!/bin/bash
#defination function
hs1 () {
ls /
cd /
cp /bin/ls /tmp/ls
}
#use function
hs1
echo "----------"
hs1
[root@teacher test]# vim s34.sh
#!/bin/bash
#defination function
testip () {
ping -c 2 $1 &>/dev/null
}
read -p "please input ip-address:" ip
testip $ip
if [ $? -eq 0 ]
then
echo $ip > /tmp/ip.txt
fi
You have new mail in /var/spool/mail/root
[root@teacher test]# chmod +x s34.sh
[root@teacher test]# ./s34.sh
please input ip-address:192.168.3.254
[root@teacher test]# cat /tmp/ip.txt
192.168.3.254
# vim s35.sh
#!/bin/bash
#defination function
testip () {
ping -c 2 $1 &>/dev/null
}
wd='192.168.3.'
for i in `seq 1 254`
do
echo $wd$i
done>/tmp/ip1.txt
while read ip
do
testip $ip
if [ $? -eq 0 ]
then
echo $ip >> /tmp/ip.txt
fi
done
---------------------------------
信号捕捉 信号9捕捉不到
trap
# vim s36.sh
#!/bin/bash
trap "echo 气死你,你杀不死我" 2
trap "echo 气死你,就是杀不死我,哈哈哈 " 15
while :
do
echo "有本事,来啊~,来啊~"
sleep 1
done
[root@teacher test]# chmod +x s36.sh
[root@teacher test]# ./s36.sh
#pgrep s36.sh
# kill -2 `pgrep s36.sh`
# kill -2 `pgrep s36.sh`
-----------------------------
expect
# rpm -q expect
# yum install expect -y
# echo > /root/.ssh/known_hosts
# vim auto.exp
#!/usr/bin/expect
set timeout 60
spawn ssh 192.168.3.2
expect "continue connecting"
send "yes\r"
expect "password:"
send "redhat1\r"
interact
# chmod +x auto.exp
# ./auto.exp
--------------------------
脚本加密
# tar zxvf shc-3.8.7.tgz -C /usr/local/src
shc-3.8.7/CHANGES
shc-3.8.7/Copying
shc-3.8.7/Makefile
shc-3.8.7/match
shc-3.8.7/pru.sh
shc-3.8.7/shc-3.8.7.c
shc-3.8.7/shc.1
shc-3.8.7/shc.README
shc-3.8.7/shc.c
shc-3.8.7/shc.html
shc-3.8.7/test.bash
shc-3.8.7/test.csh
shc-3.8.7/test.ksh
# cd /usr/local/src
# cd shc-3.8.7/
# make
# make test
# make strings
# make expiration
# ./shc -e 13/07/2013 -m "please connect [email protected]" -f /test/aaa.sh
---------------------------------
脚本调试
1) 设置断点 echo
2)sh -x file.sh