#批量修改当前目录下的文件扩展名,将.doc改为.txt

1.          [root@svr5 rendir]# vim ../renfilex.sh

2.            #!/bin/bash

3.            for FILE in "$1"

4.            do

5.            mv $FILE ${FILE%$1}"$2"

6.            done

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

编写sumx.sh脚本从键盘读入一个正整数x,求从1到x的和

1.            [root@svr5 ~]# vim sumx.sh

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
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++