1、写一个脚本,完成以下功能
(1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;
(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;
(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;
(4) 分别统计S开头和K开头的文件各有多少;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 DIR=/etc/rc.d/rc3.d/ 5 k_start=0 6 s_start=0 7 for i in $(ls $DIR) 8 do 9 echo $i | grep "^K" &>/dev/null 10 if [ $? -eq 0 ];then 11 line=$( wc -l $DIR$i| cut -d" " -f1 ) 12 let k_start++ 13 echo "$i:stop:$line" 14 else 15 line=$(wc -l $DIR$i | cut -d" " -f1 ) 16 let s_start++ 17 echo "$i:start:$line" 18 fi 19 done 20 echo "the total files start of k are:$k_start" 21 echo "the total files start of s are:$s_start"
2、写一个脚本,完成以下功能
(1) 脚本能接受用户名作为参数;
(2) 计算此些用户的ID之和;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 sum=0 5 if [ $# -gt 0 ];then 6 for i in $* 7 do 8 id $i &>/dev/null 9 if [ $? -eq 0 ];then 10 echo "user $i exist " 11 let sum+=$(id -u $i ) 12 else 13 echo "user $i not exist " 14 fi 15 done 16 else 17 echo "arguments must be great then 0 " 18 exit 1 19 fi 20 echo "the sum of all user id is: $sum"
3、写一个脚本
(1) 传递一些目录给此脚本;
(2) 逐个显示每个目录的所有一级文件或子目录的内容类型;
(3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 function dir_count() 5 { 6 dir_number=0 7 file_number=0 8 cd $1 &>/dev/null 9 if [ $? -eq 0 ];then 10 for j in $(ls $1) 11 do 12 cd $1$j &>/dev/null 13 if [ $? -eq 0 ];then 14 let dir_number++ 15 file_number=$(ls $1$j | wc -l ) 16 else 17 let file_number++ 18 fi 19 done 20 ls $1 21 echo "in $1 ,total dir number is:$dir_number,total file is: $file_number" 22 else 23 echo "$1 is not a directory" 24 fi 25 } 26 27 if [ $# -ge 1 ];then 28 for i in $* 29 do 30 dir_count $i 31 done 32 else 33 echo "argument invalid" 34 exit 1 35 fi
4、写一个脚本
通过命令行传递一个参数给脚本,参数为用户名
如果用户的id号大于等于500,则显示此用户为普通用户;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 if [ $# -eq 1 ];then 5 id_number=$(id -u $1 &>/dev/null) 6 if [ $? -eq 0 ];then 7 if [ $id_number > 500 ];then 8 echo "usual user" 9 else 10 echo "system user" 11 fi 12 else 13 echo "$1 not exist" 14 fi 15 else 16 echo "argument must be only one" 17 fi
5、写一个脚本
(1) 添加10用户user1-user10;密码同用户名;
(2) 用户不存在时才添加;存在时则跳过;
(3) 最后显示本次共添加了多少用户;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 total_user=0 5 for i in {1..10} 6 do 7 id user$i &>/dev/null 8 if [ $? -eq 0 ]; then 9 echo "user$i exist" 10 echo "user$i" | passwd --stdin user$i &>/dev/null 11 echo "user$i change the password" 12 else 13 useradd user$i 14 echo "user$i" | passwd --stdin user$i &>/dev/null 15 echo "user$i add finished" 16 let total_user+=1 17 fi 18 done 19 echo "$total_user add"
6、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 online_number=0 5 no_online_number=0 6 for i in 192.168.88.{100..200} 7 do 8 ping -c 2 -W 2 $i &>/dev/null 9 if [ $? -eq 0 ];then 10 echo "$i is onlline" 11 let online_number++ 12 else 13 echo "$i not online" 14 let no_online_number++ 15 fi 16 done 17 echo "$online_number host online,$no_online_number not online " 18
7、打印九九乘法表;
1 #!/bin/bash 2 #author:BaoZhang 3 # 4 for((i=1;i<10;i++)) 5 do 6 for((j=1;j<=i;j++)) 7 do 8 echo -n "$j*$i=$[ $j * $i ]" 9 echo -n " " 10 done 11 echo "" 12 done ~
原创文章,作者:zhangbao,如若转载,请注明出处:http://www.178linux.com/59460
评论列表(1条)
赞,几个脚本都写的比较好~~继续加油~