#批量修改當前目錄下的文件擴展名,將.doc改成.txtshell
1. [root@svr5 rendir]# vim ../renfilex.sh vim
2. #!/bin/bash 數組
3. for FILE in "$1" bash
4. do 服務器
5. mv $FILE ${FILE%$1}"$2" ssh
6. doneide
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++spa
編寫sumx.sh腳本,從鍵盤讀入一個正整數x,求從1到x的和ip
1. [root@svr5 ~]# vim sumx.sh get
2. #!/bin/bash
3. read -p "請輸入一個正整數:" x
4. x=${x:-1}
5. i=1; SUM=0
6. while [ $i -le $x ]
7. do
8. let SUM+=i
9. let i++
10. done
11. echo "從1到$x的總和是:$SUM"
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
99乘法表
1 #!/bin/bash
2 for((i=1;i<=9;i++))
3 do
4 for ((j=1;j<=9;j++))
5 do
6 echo -ne "$i*$j=$((i*j))\t"
7 done
8 echo
9 done
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
建立用戶
#!/bin/bash
read -p "請輸入添加用戶到個數: " sum
read -p "請輸入用戶到前綴名: " uname
sum=${sum:?}
uname=${uname:?}
i=1
while [ $i -le $sum ]
do useradd $uname$i &> /dev/null
let i++
done
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ping在線的主機並返回結果
[root@B shell]# cat check_veryone_ip.sh
#!/bin/bash
ip=${1:-"127.0.0.1"}
ping -c 3 $ip &> /dev/null
if [ $? -eq 0 ];then
echo $ip is online
else
echo $ip is not online
fi
[root@B shell]#
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
屏幕輸入3個ip地址,保存到ipgrp數組裏,並在屏幕輸出第二個IP
#!/bin/bash
read -p "qing shuru yige ipdizhi: " ipa
read -p "qing shuru yige ipdizhi: " ipb
read -p "qing shuru yige ipdizhi: " ipc
ipgrp=($ipa $ipb $ipc)
echo ${ipgrp[1]}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++expect
從ftp服務器上下載文件到本機
#!/usr/bin/expect
set ip "localhost"
set username "ftp"
set password ""
set path [ lindex $argv 0 ]
set file [ lindex $argv 1 ]
spawn ftp $ip
expect "Name"
send "$username\r"
expect "Password"
send "$password\r"
expect "ftp>"
send "cd $path\r"
expect "ftp>"
send "get $file\r"
expect "ftp>"
send "bye\r"
:wq
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
遠程修改用戶密碼腳本
#!/bin/bash
oldpass=123456
newpass=654321
for ip in $(cat /root/ip.txt)
do
expect <<EOF
spawn ssh root@${ip} "echo $newpass | passwd --stdin root"
expect "(yse/no)" {
send "yes\r"
expect "password"
send "${oldpass}\r"
} "password" { send "${oldpass}\r" }
expect eof
EOF EOF必須頂格寫
done
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
刪除$1文件最後兩行
#!/bin/bash hang=$(cat $1 | wc -l) lianghang=`expr $hang - 1` sed -i ''$lianghang',+1d' $1+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++