1.实现:执行脚本输入用户名,若该用户存在,输出提示该用户已存在;若该用户不存在,提示用户输入密码,建立用户并设立其密码
#!/bin/bash
RED ()
{
echo -e "\033[31m$*\033[0m"
}
USERADD()
{
read -p "Please input username: " USERNAME
[ "$USERNAME" = "exit" ] && {
echo bye
exit 0
}
useradd $USERNAME &> /dev/null
[ `echo $?` = "0" ] && {
read -p "Please input passwd: " PASSWD
echo $PASSWD | passwd --stdin $USERNAME &> /dev/null
}||{
RED "$USERNAME is exist!"
}
USERADD
}
USERADD
2.实现:简单的秒倒计时
#!/bin/bash
for SEC in {10..0}
do
echo "After ${SEC}s is end" ##-n 循环输出显示在1行
echo -ne "\r" #覆盖之前的输出
sleep 1 #等待1秒再执行
done
~
3.实现:分秒倒计时(循环方法)
#!/bin/bash
SEC=10
MIN=1
for((SEC=10;SEC>=0;SEC--))
do
while [ "$SEC" = "0" -a "$MIN" = "0" ]
do
exit 0
done
while [ "$SEC" = "0" ]
do
echo -ne "After $MIN:$SEC is end! "
echo -ne "\r"
sleep 1
SEC=59
((MIN--))
done
echo -ne "After $MIN:$SEC is end!!"
echo -ne "\r"
sleep 1
done
~
4.实现倒计时器:1分10秒(计算方法)
#!/bin/bash
SEC=10
MIN=1
ALL_SEC=$[60*$MIN+$SEC]
for ((;ALL_SEC>0;ALL_SEC--))
do SEC=$[$ALL_SEC%60]
MIN=$[$ALL_SEC/60]
echo -ne "After $MIN:$SEC is end!"
echo -ne "\r"
sleep 1
done
5.实现:检测指定范围主机是否通信,并将通信的主机ip输出到文件host_ip中
#!/bin/bash
##首先检测host_ip文件是否存在,如果存在将主动权交给用户,这才是体现服务逻辑所在!
[ -e "/mnt/host_ip" ] && {
echo "/mnt/host_ip is exist!"
echo "[O]verwrite [B]ackup [S]kip"
read -p "What do you want action: " WORD
ACTION=`echo $WORD | tr 'a-z' 'A-Z'`
[ "$ACITON" = "O" ] && {
rm -fr /mnt/host_ip
}
[ "$ACTION" = "B" ] && {
mv /mnt/host_ip /mnt/host_ip.bak
}
[ "$ACTION" = "S" ] && {
exit 0
}
}
for ip in {10..20}
do ping -c1 -w1 172.25.254.$ip &> /dev/null && {
echo "172.25.254.$ip" >> /mnt/host_ip
}
done
6.实现 :简易计算器
#!/bin/bash
jisuan()
{
read -p "Please input first digit: " FIRST
[ -z "$FIRST" ] && {
echo -e "\033[31mError:Please input digit!\033[0m"
exit 1
}
read -p "PLease input action for digit: " ACTION
[ -z "$ACTION" ] && {
echo -e "\033[31mErro:Please input action!\033[0m"
exit 3
}
read -p "Please input second digit: " SECOND
[ -z "$SECOND" ] && {
echo -e "\033[31mErro:Please input digit!\033[0m"
exit 2
}
bc<<EOF
$FIRST $ACTION $SECOND
EOF
jisuan
}
jisuan
7.实现:执行users_create.sh userlist passlist 如果userlist 与 passlist 行数不相等报错;两文件不存在报错;用户以存在报错;当用户不存在建立用户并为其设置密码
#!/bin/bash
RED()
{
echo -e "\033[31m$*\033[0m"
}
GREEN()
{
echo -e "\033[32m$*\033[0m"
}
if [ "$#" -lt "2" ]
then RED "Please input your userlist and passlist following $0!!!"
elif [ ! -e "$1" ]
then RED "$1 is not exist!!!Please check!"
elif [ ! -e "$2" ]
then RED "$2 is not exist!!!Please check!"
else [ -e "$1" -a -e "$2" ]
USERNUM=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
PASSNUM=`awk 'BEGIN{M=0}{M++}END{print M}' $2`
[ "$USERNUM" != "$PASSNUM" ] && {
RED "Please check your userlist and passlist!!!"
}
for NUM in `seq 1 $USERNUM`
do
USERNAME=`sed -n ${NUM}p $1`
PASS=`sed -n ${NUM}p $2`
useradd $USERNAME && {
echo $PASS | passwd --stdin $USERNAME
GREEN "$USERNAME created successful!"
}||{
RED "$USERNAME is already exist!!!"
}
done
fi
8.实现:远程连接1-10主机,并且为每个主机分别建立userfile中的用户,如果主机中用户以存在,则不做任何操作;如若不存在,建立用户,并为用户修改密码
#!/bin/bash
Auth_Conenct()
{
/usr/bin/expect <<EOF
spawn ssh [email protected]$1
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect eof
EOF
}
Max_Line=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
for NUM in {1..10}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null
if [ "$?" = "0" ]
then
for Line_Num in `seq 1 $Max_Line`
do
USERNAME=`sed -n ${Line_Num}p $1`
PASSWORD=`sed -n ${Line_Num}p $2`
Auto_Connect 172.25.254.$NUM "id $USERNAME &> /dev/null||useradd $USERNAME && echo $PASSWORD | passwd --stdin $USERNAME" &> /dev/null
fi
done
10.实现:文件类型的判定
#!/bin/bash
while [ `echo $?` = 0 ]
do read -p "Please input your filename: " WORDS
WORD = `echo $WORDS` | tr 'A-Z' 'a-z'
if [ -d "$WORD" ]
then echo -e "\033[32m$WORD is a direcotry!\033[0m"
elif [ -f "$WORD" ]
then echo -e "\033[32m$WORD is a common file!\033[0m"
elif [ -L "$WORD" ]
then echo -e "\033[32m$WORD is a soft file!\033[0m"
elif [ "$WORD" = "exit" ]
then echo "Bye~~~"
exit 1
else
echo -e "\033[31m$WORD is not exist!\033[0m"
fi
done
11.实现文件类型的判定(case)
#!/bin/bash
CHECK_FILE()
{
if [ -e "$1" ]
then $2
$3
fi
}
GREEN()
{
echo -e "\033[32m$*\033[0m"
}
RED()
{
echo -e "\033[31m$*\033[0m"
}
[ "$#" -ge "2" ] && {
case $1 in
-a)
CHECK_FILE $2 " RED $2 is exist!" exit
touch $2
GREEN "$2 is already create!"
;;
-d)
CHECK_FILE $2 "rm -fr $2;GREEN $2 is already delete! " exit
RED "$2 is not exist!"
;;
-p)
CHECK_FILE $2 "cp -rp $2 /mnt" exit
RED "$2 is not exist!"
;;
*)
RED "Please input -a|-d|-p following $0!"
esac
}||{
RED "Please input option and filename!"
}
12.实现:数据库的备份(有不足)
#!/bin/bash
RED()
{
echo -e "\033[31m$*\033[0m"
}
GREEN()
{
echo -e "\033[32m$*\033[0m"
}
[ -z "$1" ]&& {
RED "Error:Please input password for mysql!"
exit 0
}
[ -d "/mnt/mysqldump" ] &> /dev/null &&{
RED "Error:/mnt/mysqldump is already exist!!!"
GREEN "[S]kip [B]ackup [O]verwrite"
read -p "Please input your action for /mnt/mysqldump: " WORD
ACTION=`echo $WORD| tr 'a-z' 'A-Z'`
[ "$ACTION" = "S" ] && {
exit 0
}
[ "$ACTION" = "B" ] && {
mv /mnt/mysqldump /mnt/mysqldump.backsql
}
[ "$ACTION" = "O" ] && {
rm -fr /mnt/mysqldump
}
}
NUM=`mysql -uroot -p$1 -e "SHOW DATABASES;"|awk 'BEGIN{N=0}{N++}END{print N}'`
for M in `seq 3 $[$NUM-1]`
do
DATABASE=`mysql -uroot -p$1 -e "SHOW DATABASES;"|sed -n ${M}p`
mkdir /mnt/mysqldump
touch /mnt/mysqldump/$DATABASE
`mysqldump -uroot -p$1 $DATABASE > /mnt/mysqldump/$DATABASE`
done
13.实现:-d 用户的删除 -c用户密码的改变
#!/bin/bash
[ -z "$1" ]&&{
echo -e "\033[31mError:Please input your options!!\033[0m"
exit 0
}
[ "$1" = "-d" ]&&{
read -p "Please input username: " NAME
userdel -r $NAME
echo -e "\033[31m$NAME is already del!\033[0m"
exit 0
}
[ "$1" = "-c" ]&&{
read -p "Please input username: " NAME1
read -p "Please input password: " -s PASSWD
echo $PASSWD | passwd --stdin $NAME1
exit 0
}
[ "$1" = "-d " ] -o [ "$1" = "-c" ] ||{
echo -e "\033[31mPlease input -d|-c following userctrl.sh\033[0m"
exit 0
}
14.实现:一键搭建论坛
#!/bin/bash
echo "install software..."
yum install httpd mariadb-server php php-mysql -y
sed '/^Listen 80/cListen 8080' -i /etc/httpd/conf/httpd.conf
sed '1askip-networking=1' -i /etc/my.cnf
systemctl start mariadb httpd
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
mysql_secure_installation <<EOF
westos
westos
EOF
wget ftp://172.25.254.250/pub/Discuz_x3.2_SC_UTF8.zip -O /var/www/html/Discuz_X3.2_SC_UTF8.zip
cd /var/www/html
unzip Discuz_X3.2_SC_UTF8.zip &> /dev/null
chmod o+w upload/ -R
setenforce 0
firefox http://localhost:8080/upload