1.判斷字符串是否為空
#!/usr/bin/bash
while : #:默認值為真
do
read -p "請輸入你的密碼: " a
pass=123456
if [ -z $a ];thenecho "您輸入的密碼不能為空"exit 1
elseif [ $a = $pass ];thenecho "登錄成功"breakelseecho "您的密碼輸入有誤,請重新輸入"fi
fi
done
2.判斷文件是否存在
#/bin/bash
if [ -f /opt/a.txt ];thenecho "ok"
elseecho "不存在,正在創建"touch /opt/a.txt
fi
3.必須在腳本后加上適當的參數腳本才能正確執行
#!/bin/bash
if [ "$1" = "hello" ]; thenecho "Hello! How are you ?"
elif [ "$1" = "" ]; thenecho "You MUST input parameters"
elseecho "The only accept parclear ameter is hello"
fi
4.測試ip地址主機位從2到100的機器是否存活,并把存活的機器記錄到文本文件alivehost.txt內。(使用ping命令)
#!/usr/bin/bash
src_ip="192.168.246"
for i in {2..254}
do{ping -c1 $src_ip.$i &>/dev/nullif [ $? -eq 0 ];thenecho "alive: $src_ip.$i" >> ip_up.txtecho "alive: $src_ip.$i"elseecho "down: $src_ip.$i" >> ip_down.txtecho "down: $src_ip.$i"fi} &
done
wait
echo "finish..."
5.根據用戶輸入foo打印bar,輸入bar打印foo,否則輸入只能輸入foo|bar
#!/usr/bin/env bash
case $1 infoo)echo "bar";;bar)echo "foo";;*)echo "Usage:$0 '{foo|bar}'";;
esac
6.隨機數生成8位隨機密碼
#!/bin/bash
string="AFJALKGJglekrjgkASKJFEKFELjjglkerjgwej235834095ejglkdjg"
sum=''
for ((i=1; i<=8; i++)) #指定循環8次
dolen=${#string} #獲取變量的長度num=$[RANDOM % $len] #使用變量長度中生成隨機數(作為取字符的索引)value=${string:$num:1} #截取指定索引的字符sum+=$value #字符追加到sum變量中
done
echo $sum
7.for循環批量創建用戶
#!/usr/bin/bash
read -p "請設置用戶名/數量/密碼: " prefix num pass
cat <<-EOF
用戶前綴:$prefix
用戶數量:$num
用戶密碼:$pass
EOF
for i in $(seq 1 $num)
do
user=$prefix$i
id $user &> /dev/null
if [ $? -eq 0 ];thenecho "$user is already exist!"exit 0
elseuseradd $user &> /dev/null && echo $pass | passwd --stdin $user &>/dev/null
fi
done
echo "starting create users..."
8.通過一個文件批量創建用戶:
#背景:寫一個腳本,滿足以下需求及應用,如一個文件的內容如下,根據文件內容實現批量創建用戶,第一列為用戶名,第二列為密碼
[root@localhost script]# vim user_pass.txt #創建用戶和密碼文件
user1 123456
user2 654321
user3 1122334
user4 4433221
user5 9876543
[root@localhost script]# vim create_user.sh #編寫腳本
#!/usr/bin/bash[ $UID -ne 0 ] && exit 1
while read line
douser=`echo $line | awk '{print $1}'`pass=`echo $line | awk '{print $2}'`id $user &> /dev/null || useradd $user && echo $pass | passwd $user --stdin
done < /opt/test/script/user_pass.txt
9.隨機數實現猜數字游戲
#!/bin/bash
num=$[RANDOM % 10]
i=1 #定義i為循環條件
a=0 #定義a為猜的次數變量
while [ $i -eq 1 ]
do
read -p "請輸入你猜的數字(0~10):" cif [ $c -eq $num ];thenecho "恭喜你猜對了!"let a++let i++elif [ $c -gt $num ];thenecho "你猜高了,請繼續!"let a++elif [ $c -lt $num ];thenecho "你猜低了,請繼續!"let a++fi
done
echo "你一共猜了${a}次。"
10.建立ssh免密連接腳本
[root@mysql-server script]# cat shuzu.sh
#!/bin/bash
server_ip=(192.168.209.166 192.168.209.168 192.168.209.169)
server_name=(web-1 web-2 mysql-server)
for (( i=0; i<${#server_ip[@]}; i++ ))
doecho $i-----${server_ip[i]}-----${server_name[i]}
done
echo "請輸入要連接的序號: " && read n
if [[ $n -ge 0 && $n -lt ${#server_ip[@]} ]];thenecho "正在鏈接${server_ip[n]}"sleep 1/root/expect/expect.sh ${server_ip[n]}
elseecho "請輸入范圍內的序號..."sleep 1
fi
/root/expect/expect.sh
#!/usr/bin/expect
set user root
set pass 1
set ip [ lindex $argv 0 ]
set timeout 10spawn ssh $user@$ip
expect {"yes/no" { send "yes\r"; exp_continue }"password:" { send "$pass\r" };
}
interact