1.强弱引用实例
[[email protected] mnt]# vim 1.sh
[[email protected] mnt]# chmod +x 1.sh
[[email protected] mnt]# cat 1.sh
#!/bin/bash
echo '$$$$$ the time now is "'\ ` date +%T `\ '" $$$$$'
[[email protected] mnt]# sh 1.sh
$$$$$ the time now is " 16:57:00 " $$$$$
2.1分10秒的倒计时
[[email protected] mnt]# vim time.sh
[[email protected] mnt]# chmod +x time.sh
[[email protected] mnt]# cat time.sh
#!/bin/bash
MIN=1
for ((SEC=10;SEC>=0;SEC--))
do
echo -ne "After ${MIN}:${SEC}s is end "
sleep 1
echo -ne "\r \r"
while [ "$SEC" -le "0" -a "$MIN" -gt "0" ]
do
echo -ne "After ${MIN}:${SEC}s is end "
echo -ne "\r \r"
((MIN--))
SEC=60
done
done


do
echo -ne "After ${MIN}:${SEC}s is end "
echo -ne "\r \r"
((MIN--))
SEC=60
done
done
3.检测ip地址
[[email protected] mnt]# vim check_ip1.sh
[[email protected] mnt]# chmod +x check_ip1.sh
[[email protected] mnt]# sh check_ip1.sh
Please input you want check ip address: 172.25.254.19
172.25.254.19 is up
[[email protected] mnt]# sh check_ip1.sh
Please input you want check ip address: 172.25.254.23
172.25.254.23 is down
[[email protected] mnt]# cat check_ip1.sh
#!/bin/bash
read -p "Please input you want check ip address: " IP
ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down
4.批量检测ip地址
[[email protected] mnt]# vim check_ip.sh
[[email protected] mnt]# chmod +x check_ip.sh
[[email protected] mnt]# sh check_ip.sh
172.25.254.15 is down
172.25.254.16 is down
172.25.254.17 is down
172.25.254.18 is down
172.25.254.19 is up
172.25.254.20 is down
[[email protected] mnt]# cat check_ip.sh
#!/bin/bash
for NUM in {15..20}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && echo 172.25.254.$NUM is up || echo 172.25.254.$NUM is down
done
5.数字运算符
[[email protected] mnt]# vim check_num.sh
[[email protected] mnt]# chmod +x check_num.sh
[[email protected] mnt]# sh check_num.sh
Please input a number: 7
yes
[[email protected] mnt]# sh check_num.sh
Please input a number: 10
yes
[[email protected] mnt]# sh check_num.sh
Please input a number: 11
no
[[email protected] mnt]# cat check_num.sh
#!/bin/bash
read -p "Please input a number: " NUM
if
[ "$NUM" -ge "0" -a "$NUM" -le "10" ]
then
echo yes
else
echo no
fi
6.文件状态运算符
[[email protected] mnt]# vim check_file.sh
[[email protected] mnt]# chmod +x check_file.sh
[[email protected] mnt]# sh check_file.sh
Please input give me a file
[[email protected] mnt]# sh check_file.sh haha
haha is not exist
[[email protected] mnt]# sh check_file.sh /etc/passwd
/etc/passwd is a file
[[email protected] mnt]# sh check_file.sh /mnt
/mnt is a directory
[[email protected] mnt]# cat check_file.sh
#!/bin/bash
if
[ -e "$1" ]
then
[ -f "$1" -a ! -L "$1" ] && echo $1 is a file
[ -b "$1" ] && echo $1 is a block
[ -c "$1" ] && echo $1 is a count
[ -d "$1" ] && echo $1 is a directory
[ -L "$1" ] && echo $1 is a lik
else
[ -z "$1" ] && echo "Please input give me a file"
[ -n "$1" ] && echo $1 is not exist
fi
7.创建用户并设置密码
[[email protected] mnt]# vim create_user.sh
[[email protected] mnt]# chmod +x create_user.sh
[[email protected] mnt]# sh create_user.sh
please input you want create username: lala
please input lala's password: lala create sucessfullly
[[email protected] mnt]# cat create_user.sh
#!/bin/bash
read -p "please input you want create username: " NAME
read -p "please input ${NAME}'s password: " -s PASS
useradd $NAME
echo $PASS | passwd --stdin $NAME &> /dev/null && echo $NAME create sucessfullly || echo error
8.批量创建用户并设置密码
[[email protected] mnt]# vim userfile
[[email protected] mnt]# cat userfile
user1
user2
user3
[[email protected] mnt]# vim passfile
[[email protected] mnt]# cat passfile
redhat
redhat
redhat
[[email protected] mnt]# vim create_user.sh
[[email protected] mnt]# chmod +x create_user.sh
[[email protected] mnt]# sh create_user.sh
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[[email protected] mnt]# su - user1
[[email protected] ~]$ su - user2
Password:
Last failed login: Wed Jun 21 17:33:18 CST 2017 on pts/1
There were 2 failed login attempts since the last successful login.
[[email protected] ~]$
[[email protected] mnt]# cat create_user.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`
[ "$MAXUSER" -eq "$MAXPASS" ]&&(
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
)||(
echo $1 and $2 have different lines
)
elif
[ ! -e "$1" ]
then
echo "ERROR:$1 is not exsit"
else
echo "ERROR:$2 is not exsit"
fi
else
echo "ERROR: Please input userfile and password file after command !!"
fi
9.自动应答脚本
[[email protected] mnt]# vim ask.sh
[[email protected] mnt]# chmod +x ask.sh
[[email protected] mnt]# cat ask.sh
#!/bin/bash
read -p "Who are you: " who
read -p "How old are you: " old
read -p "Are you happy: " feel
echo $who is $old years old and feel $feel
[[email protected] mnt]# vim expect.exp
[[email protected] mnt]# chmod +x expect.exp
[[email protected] mnt]# cat expect.exp
#!/usr/bin/expect
spawn /mnt/ask.sh
expect "who"
send "tom\r"
expect "old"
send "18\r"
expect "happy"
send "happy\r"
expect eof
exit
[[email protected] mnt]# expect expect.exp
spawn /mnt/ask.sh
Who are you: tom
How old are you: 18
Are you happy: happy
tom is 18 years old and feel happy