学习期间写过一些shell脚本,
测试过程:vi test.sh 后把程序写入其中,保存退出。然后改变文件属性:chmod +x test.sh 最后执行:./test.sh
for语句测试:
1)
#!/bin/bash
for num in 1 2 3
do
echo "hello,num=$num"
done
2)
#!/bin/bash
for ((i=1;i<=3;i=i+1 ));do echo hello,$i;done
if语句测试:
#!/bin/bash
echo "please run the program with more than one param"
if [ "$1" = '' ] #$1是只运行该程序时可加参数1,如./iftest.sh 1 注意等号旁边和中括号的空格
then
echo "error"
else
echo "ls内容为\n:" #echo '$1':$1
echo `ls`
fi
while语句测试:
1)
#!/bin/bash
m=0
while [ $m -lt 10 ]
do
echo $m
m=`expr $m + 1` #注意m旁边的空格
done
2)
declare -i m=0 #别一种在算术运算时声明变量的方法
while [ $m -lt 10 ]
do
echo $m
m=$m+1
done
until语句测试:
1)
#!/bin/bash
declare -i m=10
until [ $m -lt 10 ]
do
echo $m
m=$m-1
done
2)
declare -i m=0
while [ $m -lt 10 ]
do
echo $m
m=$m+1
done
sed测试:
#!/bin/bash
#sed '/^root/ !s/bash/nologin/' /etc/passwd
man sed | col -b > fortest.sh
sed '1,$s/is/--end/' fortest.sh
sed '1,$s/is/--end/g' fortest.sh
sed '/is/=' fortest.sh
引号测试:
#!/bin/bash
var=hello
echo "var is $var"
echo 'var is $var'
echo "var is \$var"
echo `ls`
var2=date
echo `$var2`
case语句测试:
#!/bin/bash
for (( ;; ))
do
echo "please input a key:"
read word
case $word in
1)
echo "your chioce is the one"
;;
2)
echo "your chioce is the two"
;;
3)
echo "your chioce is the three"
;;
q)
echo "your choice is exit"
exit
;;
*)
echo "illegal choice"
exit
esac
done
" $ "符号测试:
#!/bin/bash
echo "please run with more than one parm";
echo "program name \$0:"$0;
echo "first param \$1:"$2;
echo "first param \$$:"$$;
echo "first param \$*:"$*;
数组的使用:
#!/bin/bash
hostip=("100","101","102","103","104","105","106","107","108","109","110")
hostpass=("123456","123456","123456","123456","123456","123456","123456","123456","123456","123456","123456")
i=1
while [ $i -lt 12 ] ; do
ssh
[email protected][$i]
done
重启别人电脑的shell: #这个好像有点问题,需再测试下
#!/usr/bin/expect
spawn ssh
[email protected]
expect "password:"
send "123456\n"
expect "$"
send "reboot\n"
#expect "password:"
#send "123456\n"
expect eof
查找文件,需指定查找目录和文件的修改时间:
#!/bin/bash
path=$1
date=$2
if [ -z $path ]
then
echo "Please input find path:(eg:/dev/abc/)"
read path
fi
if [ -z $date ]
then
echo "Please input find date:(eg:2006-04-23)"
read date
fi
ls -l $path --time-style=long-iso | grep "$date $time"
递归算法:
1)
#!/bin/bash
function myls()
{
local y=`ls`;
echo $y;
for z in $y;do
if [ -d $z ];then
echo "进入子目录";
cd `pwd`/$z;
myls;
echo "返回上一级目录";
cd..;
fi
done
}
echo "please input a directory:"
read x
cd $x
myls;
2)#!/bin/bash
check_dir()
{
if [ -d $y ];then
echo "$y是一个目录";
ls -l $y
else
echo "$y是一个文件";
fi
}
echo "please input a directory:"
read y
x=`ls $y`
echo $x
for y in $x;do
check_dir
done;
备份脚本:
#!/bin/bash
/bin/tar -zcf /var/mail
/bin/cp /var/mail.tar.gz /root
查找目录:
#!/bin/bash
ls -l | grep ^d
#输出当前目录下的所有目录
更新ftp服务器上的文件:
#!/bin/bash
echo "open 10.0.2.224" > /tmp/ftp1.cmd
echo "user ubunadm 123456" >> /tmp/ftp1.cmd
echo "get `date +%Y`/`date +%Y%m`/`date +%d`/file01 /root/copy/file02" >> /tmp/ftp1.cmd
ftp -nv < /tmp/ftp1.cmd
echo "quit" >> /tmp/ftp1.cmd
echo "open 10.0.2.224" > /tmp/ftp.cmd
echo "user ubunadm 123456" >> /tmp/ftp.cmd
j=`date +%Y`/`date +%Y%m`/`date +%d`
echo "$j"
echo "cd $j" >> /tmp/ftp.cmd
cd /root/copy
m=`ls -l|awk '{print $5}'`
n=$m
while true ; do
echo "大小 $m and $n"
if [ $m -eq $n ] ; then
echo "OK!"
n=$m
echo "**********************************************"
echo "size file01" >> /tmp/ftp.cmd
x=`ftp -nv < /tmp/ftp.cmd`
echo "--------------------------"
echo "文件内容为:$x"
echo "--------------------------"
m=`echo $x | awk '{print $31}'`
echo "--------------------------"
echo "文件大小为:$m"
echo "--------------------------"
else
echo "get `date +%Y`/`date +%Y`${x:0:2}/${y:0:2}/file01 /root/copy/file02" >> /tmp/ftp1.cmd
ftp -nv < /tmp/ftp1.cmd
n=$m
echo "更新成功"
fi
sleep 3
done
制作菜单脚本:
1)
#!/bin/bash
x=0
while [ $x -ne 5 ]; do
echo "List Directory......1"
echo "Change Directory....2"
echo "Edit File...........3"
echo "Remove File.........4"
echo "Exit Menu...........5"
echo "Please choose one:"
read x
case $x in
1)echo "current directory is:"
ls `pwd`;;
2)echo "Enter target directory:/"
echo "List Directory......1"
echo "Change Directory....2"
echo "Edit File...........3"
echo "Remove File.........4"
echo "Exit Menu...........5"
echo "Please choose one:"
read y
case $y in
1)echo "current directory is:"
ls `pwd`;;
2)echo "Please a path:"
read z
cd $z;;
3)echo "Please input a file:"
read i
vi $m;;
4)echo "Please input a file";
read j
rm -rf $n;;
5)echo "Exit";;
esac
;;
3)echo "Please input a file:"
read m
vi $m;;
4)echo "Please input a file";
read n
rm -rf $n;;
5)echo "Exit";
esac
done
2)
#!/bin/bash
x=1
while [ $x -ne 0 ]; do
echo "1.ls - list current directory"
echo "2.cd - change directory"
echo "3.mkdir - create a directory"
echo "4.rm - remove"
echo "0.quit"
echo please input:
read x
echo input:$x
case $x in
1)echo "ls - list current directory";;
2)echo "cd - change directory";;
3)echo "mkdir - create a directory";;
4)echo " 1.file - remove a file
2.directory - remove a directory"
read y
echo input is:$y
case $y in
1)echo "file - create a directory";;
2)echo "directory - remove a directory";;
*)echo "bad";;
esac
;;
0)echo "bad";
esac
done
大量发邮件脚本:
1)
#!/bin/bash
exec 3<friends;#将名字,拼音,邮件分成三列输入friends中
exec 0<&3-;
while read a b c ; do#读文件里的三列
echo $a $b $c;
echo "Hi,$b!
Happy......
...
.... `date`" >mymail#生成一封邮件
mail -S "Happy New Year!" $c < mymail
done
3<&0-
2)
#!/bin/bash
exec 3<friends;#将名字,拼音,邮件分成三列输入friends中
exec 0<&3-;
while read a b c ; do#读文件里的三列
echo $a $b $c;
mail -S "Happy New Year!" $c <<Delimit
Hi,$b!
Happy......
...
.... `date`"#用临时文档( <<Delimit 文件内容 Delimit )输入邮件内容
Delimit
done
3<&0-
ssh自动登陆另一台机:
1)
#!/usr/bin/expect
spawn ssh [lindex $argv 0]
set password [lindex $argv 1]
expect "*password:"
send "$password\r"
expect eof
interact #把控制权交给用户
大批量创建用户和修改密码:
#!/bin/bash
#此脚本适合于ubuntu下
#此小脚本为方便需要批量添加大量用户的管理员而写,密码默认设置为用户名.
read -p "请输入你想要添加的用户名和需要的个数(如:xuanfei 100):" a b
for((i=1;i<=$b;i++))
do
useradd -m $a$i && echo "$a$i:$a$i" > swp && chpasswd < swp && pwconv && echo "添加$a$i用户成功"
done
rm -rf swp
2)
#!/bin/bash
#此脚本适合于Redhat下
for((i=0;i<10;i++))
do
useradd user$i
echo "加用户 $user 成功"
echo "user$i" | passwd --stdin user$i
done