101、编写一个脚本,进行简单的减法运算,要求提示输入变量
#!/bin/bash read -p "input a number:" num1 read -p "input another number:" num2 let "num3=num1-num2" echo $num3
102、把某个目录下的文件扩展名改为bat,再以时间为文件名压缩打包存放到某个目录。
#!/bin/bash for file in $(ls $1) do new_file=${file%.*}.bat mv ./$1/$file ./$1/$new_file tmp=$(date +%y) tar cvf ./$tmp.tar ./$1 done
103、从网上下载一个文件,保存到指定目录
#!/bin/bash url=http://rs1.bn.163.com/ent/2009/05/20_canquedege.wma dir=~/下载 wget -P $dir $url 10.判断一个数是不是完数。打印出1-1000之间的完数。完数就是约数的和等于自身2倍的数。(6,28,496) #!/bin/bash sub() { i=1; sum=0; while [ $i -le $num ] do let "m=num%i" if [ $m -eq 0 ] then let "sum=sum+i" fi let "i=i+1" done let "a=2*num" if [ $a -eq $sum ] then echo $num fi } num=1 while [ $num -le 1000 ] do sub let "num = num+1" done
104、在某个文件夹下查找含有指定字符串的文件
#!/bin/bash for file in $(ls $2) do bname=$(grep -l $1 $2/$file) basename $bname done 调用方法:./tst 000 bash#在文件夹bash中查找含有“000”的文件。
105、添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。
#!/bin/bash groupadd class1 for i in {9901..9930} do xx=$(echo $i | sed 's/99//'); useradd -g class1 std$xx -p "" done 14.实现自动删除50个账号的功能。账号名为stud1至stud50 #!/bin/bash i=0 while [ i -le 50 ] do let i++ userdel -r stud$i done
106、设计一个shell程序,在每月第一天备份并压缩/etc目录的所有内容,存放在/root/bak目录里,且文件名 为如下形式yymmdd_etc,yy为年,mm为月,dd为日。Shell程序fileback存放在/usr/bin目录下。
vim /usr/bin/fileback.sh #!/bin/bash #fileback.sh #file executable: chmod 755 fileback.sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH filename=`date +%y%m%d`_etc.tar.gz cd /etc/ tar -zcvf $filename * mv $filename /root/bak/ ------------------------------------------------------ vim /etc/crontab 加入 * * 1 * * root ./fileback.sh &
107、设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限,其 中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行。
#!/bin/bash for ((i=1;i<=50;i++)) do mkdir -p /usrdata/user$i cd /usrdata chmod 754 user$i done
108、/tmp路径下有800个文件,文件名的格式是:filename_YYYYMMDD_序列号(从001到999).dat, 例如:filename_20040108_089.dat。现在想把这些文件改名,新文件名的格式是:filename_TODAY(当前日期)_序列号(从500开始,到达999之后从001开始).dat,例如: 把filename_20040108_089.dat改为filename_20041222_589.dat,注意新文件名的序列号的顺序需要和原来的一致,即要做排序处理。
#!/usr/bin/bash DEST_FILE_PART2="_`date '+%Y%m%d'`_" EXT_NAME=".dat" SRC_FILE_LIST=`find /tmp -name "*_*_*$EXT_NAME" -print` for each in $SRC_FILE_LIST; do DEST_FILE_PART1=`echo $each | awk -F"_" '{print $1}'` OLD_NUM=`echo $each | awk -F"_" '{print $3}' | awk -F"." '{print $1}'` DEST_FILE_PART3=`expr $OLD_NUM + 500` [ $DEST_FILE_PART3 -gt 999 ] && DEST_FILE_PART3=`expr $OLD_NUM - 499` && DEST_FILE_PART3=`printf d $DEST_FILE_PART3` DEST_FILE=$DEST_FILE_PART1$DEST_FILE_PART2$DEST_FILE_PART3$EXT_NAME echo "mv $each to $DEST_FILE" mv $each $DEST_FILE done 我的解法: #!/bin/bash new_file=$(date +20%y%m%d); for file in $(ls ~/bash) do i=$(echo "$file" | sed 's/.*_[0-9]*_\([0-9]*\).dat/\1/') #取出序列号 i=$(echo $i | sed 's/^0*\([0-9]*\)/\1/') #去掉序列号前面可能的 ‘0’, let "ii=i+500" if [ $ii -gt 999 ] then let "ii=ii-999" fi ii=`printf d $ii` #添上可能需要的‘0’ update=$(echo "$file" | sed 's/\(.*\)_[0-9]*_[0-9]*.dat/\1_'$new_file'_'$ii'.dat/') mv ~/bash/$file ~/bash/$update done
109、要求:在一个脚本中完成这个程序 1.从文件user.list中取出已给定的用户名和用户组,将这些用户和组按规则添加到系统中 2.从password.list中读取已经给定的用户密码。
user.list如下
zhangsan adminuser,dbuser,updatauser
lisi dbuser,updatauser
wanger updatauser,wheel
#!/bin/bash #group add for x in ‘awk ‘{print $2}’ user.list | sed ’s/,/\n/g’ | sort | uniq -c|sed ’s/[^a-zA-Z]//g” do groupadd $x &> /dev/null done #back message if (($?==0)) then echo “Group Ok!!” else exit 1 fi #user add for i in ‘awk ‘{print $1}’ user.list’ do for y in ‘awk ‘{print $2}’ password.list’ do useradd $i &> /dev/null echo $y | passwd –stdin $i &> /dev/null done done #back message if (($?==0)) then echo “User Ok!” else exit 1 fi #add users to groups for ((q=1;q<=3;q++)) do usermod -G ‘awk “NR==$q {print $2}” user.list | awk ‘{print $2}” ‘awk “NR==$q {print $1}” user.list | awk ‘{print $1}” &> /dev/null done if (($?==0)) then echo “All Finished!” fi
110、Shell脚本阅读(解释下面执行的功能),请挑出下面程序或脚本中的错误,并说明错在哪里。
#!/bin/bash #监控cpuser的point端口是否正常 logname="/home/forum/log/lpointlog.wf" flagfile="/home/forum/log/lognum.txt" lodnum=sed -n "1,1 p"$flagfile newnum=wc -l ${logname} echo $newnum >$flagfile totalnum=expr $newnum -$oldnum tail -n $totalnum $logname |grep "POINT_THREAD WARNING" if [$?==0] then mail -s "cpuser point "端口异常,请处理!" test@aa.com</dev/null fi> 命令行替换用反引号,if [$?==0] 应该写做if [ $?=0 ],用来判断上次命令是否执行成功; 倒数第二行应该是>/dev/null,最后fi后面的>去掉。