shell脚本

考试题:脚步测试 

1.写一个脚本

(1) 接受一个以上文件路径作为参数,一个路径或多个路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 并且说明一共统计了有多少文件,且一共多少行;
  #!/bin/bash
#
declare -i i=0
if [ $# -lt 1 ];then
echo "please input at least one file path"
exit 1
fi
for file in $*;do
if [ -f $file ];then
echo "The $file line number : $(wc -l $file | cut -d' ' -f1)"
let i++
else
echo "Please input the correct file path"
fi
done
echo "File counts: $i"

2、写一个脚本

(1) 传递两个以上的字符串当作用户名,可以是3个也可以是多个哦,不限于两个哦;
(2) 创建这些用户;且密码同用户名;
(3) 统计一共创建了几个用户;
 #!/bin/bash
#
#create user and pasword=username count usernames
declare -i i=0
if [ $# -lt 2 ];then
echo "Please at least input two username."
exit 1
fi
for username in $@;do
id $username &> /dev/null
if [ $? -eq 0 ];then
echo "$username exist"
else
useradd $username
echo "$username" | passwd --stdin $username &> /dev/null
let i++
fi
done
echo "add user count:$i"

3、写一个脚本

0)给脚本传递两个数值用于指定用户的范围,例如:1 20 则创建20个用户 或 101 200 则创建100个用户
1)批量新建用户,如:student1---student20, 或 student101---student200;
2)密码与用户名相同;
3)如果用户已经存在就不统计其UID,并给出提示
4)计算新建用户的UID之和;
#!/bin/bash
#
declare -i sum=0
for i in {1..20}; do
id visitor$i &>/dev/null && echo "visitor$i has existed!" || useradd visitor$i &>/dev/null && echo "Add visitor$i success!"
let sum+=$(id -u visitor$i)
let i++
fi
if id $1 &> /dev/null;then
uid=$(grep "^\\" /etc/passwd | cut -d: -f3)
if [ $uid -ge 500 ];then
echo "$1 is regular user"
else
echo "$1 is system user"
fi
else
echo "id sum is $sum"
fi

4、写一个脚本

1)分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
 #!/bin/bash
#
declare -i sum1=0
declare -i sum2=0
for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab};do
sum1+=$( grep -c '^#' $i )
sum2+=$( grep -c '^[[:space:]]*$' $i )
done
echo "# line number: $sum1"
echo "space line number: $sum2"

5、写一个脚本

1)显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和
  #!/bin/bash
#
declare -i sum=0;
declare -i i=0;
USERSHELL=$(grep "/bin/bash" /etc/passwd | awk -F':' '{print $1}'\t)
USERUID=$(grep "/bin/bash" /etc/passwd | awk -F':' '{print $3}'\t)
for i in $USERUID;do
let sum+=$i
done
echo "user id sum=$sum"
echo "user is $USERSHELL"
echo "userid is $USERUID"

6、写一个脚本

1)使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;
2)在线的主机使用绿色显示;
3)不在线的主使用红色显示;
4)请使用函数完成
#!/bin/bash

if [ $# -lt 2 ];then
echo "172.16.250.1-172.16.250.254"
exit 1
if
START=$1
END=$2

ping(){
ping -ci -w1 172.16.250.255$1 &> /dev/null && echo -e "\E[1;32m 192.168.7.13$1 Online \033[0m" || echo -e "\E[1;31m 172.16.250.255$1 Offline \033[0m"
}
for i in `seq $1 $2`; do
Ping $i
done

7、写一个脚本

(1) 添加10用户user1-user10;密码同用户名;
(2) 用户不存在时才添加;存在时则跳过;
(3) 最后显示本次共添加了多少用户;
#!/bin/bash
if [ $# -lt 1 ];then
echo "At least one username"
exit 1
fi
if id $1 &> /dev/null;then
uid=$(grep "^\\" /etc/passwd | cut -d: -f3)
if [ $uid -ge 500 ];then
echo "$1 is regular user"
else
echo "$1 is system user"
fi
else
echo "$1 is not exists"
fi

8、打印逆序九九乘法表;

给脚本传递一个参数,如果是9那么打印逆序九九乘法表,如果是8那么打印逆序八八乘法表
#!/bin/bash

declare -i k=9
while [ $k -ge 1 ]; do
for i in `seq 1 $k| sort -n -r`;do
echo -n "$i X $k = $[ $i*$k ] "
done
echo ""
let k--
done

9、写一个脚本

(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;
(2) 如果存在,则显示此设备上的所有分区信息;如果不存在,则显示设备不存在;
#!/bin/bash
if [ $# -lt 1 ];then
echo "At least one username"
exit 1
fi
if id $1 &> /dev/null;then
uid=$(grep "^\\" /etc/passwd | cut -d: -f3)
if [ $uid -ge 500 ];then
echo "$1 is regular user"
else
echo "$1 is system user"
fi
else
echo "$1 is not exists"
fi

10、写一个脚本

传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;
(1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.gz;
(2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.bz2;
(3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/tmp目录中,并命名为/tmp/etc-20170613.tar.xz;
(4) 其它任意值,则显示错误压缩工具,并执行非正常退出;
#!/bin/bash
#
[ -d /backups ] || mkdir /backups
read -p "pelase input a argu(gzip/bzip2/xz):" argu

case $argu in
gzip)
tar -Pzcf /backups/etc-`date +%Y%m%d`.tar.gz /etc # 加P,是整个目录拷贝,不会提示“tar: 从成员名中删除开头的“/””
;;
bzip2)
tar -Pjcf /backups/etc-`date +%Y%m%d`.tar.bz2 /etc
;;
xz)
tar -PJcf /backups/etc-`date +%Y%m%d`.tar.xz /etc
;;
*)
echo "error compression tools"
;;
esac

转载于:https://www.cnblogs.com/472603lin/p/7375762.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值