菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
420
0

Linux试题

原创
05/13 14:22
阅读数 21356
1、编写脚本,统计/etc、/usr、/var目录中有多少个一级子目录和文件

#!/bin/bash
# danran
# time is Mon Jun 5 13:09:12 CST 2017
line1=`ls $1 | wc -l`
line2=`ls $2 | wc -l`
line3=`ls $3 | wc -l`
let sum=$line1+$line2+$line3
echo $sum

2、自动生成脚本

#!/bin/bash
echo "#!/bin/bash
# filename $1
# author:danran
# time is `date +%F`" >$1
chmod +x $1
vim $1

3、编写脚本sumid.sh,计算/etc/passwd文件中的第10个用户和第20个用户的id之和

#! /bin/bash
user10="`head -n $2 $1 | tail -n 1|cut -d: -f3`"
user20="`head -n $3 $1 | tail -n 1|cut -d: -f3`"
let sum=$user10+$user20
echo "user id sum is $sum"

4、编写脚本/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和

#!/bin/bash
spaceline1=`grep "^[[:space:]]*$" $1 | wc -l`
spaceline2=`grep "^[[:space:]]*$" $2 | wc -l`
echo "The sum of space line:$[spaceline1+spaceline2]"

5、如果用户存在cmd1,将显示用户名存在 cmd2,如果用户名不存在,将创建该用户cmd3

id $user &>/dev/null && echo $user is exist || (useradd $user && echo "$user is created")

6、编写脚本createuser.sh,先判断参数是否为一个,如果参数个数不为1,提示usage:createuser.sh username,并退出脚本,返回为100的状态码
对用户名判断,如果存在,提示此用户已存在,创建用户并提示创建成功

#!/bin/bash
[ $# != 1 ] && echo "usage:createuser.sh username" && exit 100
id $1 &> /dev/null && echo "user is exits" ||( useradd $1 && echo "user is create")

7、编写脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

#!/bin/bash
[ $# -lt 1 ] && (echo "please input a arge" && exit ) || echo "`grep "^$" $1 | wc -l`"

8、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

#!/bin/bash
# filename hostping.sh
# author:danran
# time is 2017-06-06
echo $1 | egrep "([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}$" > /dev/null || { echo "danran" ; exit ; }
ping -c4 $1 &> /dev/null && echo "gai ip di zhi ke fangwen" ||echo "ip bu ke fangwen"

9、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满

#!/bin/bash
# filename checkdisk.sh
# author:danran
# time is 2017-06-06
inode=`df -i | grep "^/dev/sd*" | tr -s ' ' '%'| cut -d'%' -f5| sort -r | head -n 1`
disk=`df | grep "^/dev/sd*" | tr -s ' ' '%'| cut -d'%' -f5| sort -r | head -n 1`
[ $inode -gt 80 ] && echo "danran"
[ $disk -gt 80 ] && echo "dan"

10、编写脚本/bin/per.sh,判断当前用户对指定的参数文件,是否不可读并且不可写

#!/bin/bash
# filename per.sh
# author:danran
# time is 2017-06-08
[ ! -r $1 -a ! -w $1 ] && echo "$1 file not read and not write"

11、编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件

#!/bin/bash
# filename excute.sh
# author:danran
# time is 2017-06-08
[ $# == 0 ] && read -p "please input fimename" name || name=$1
[ -f $name ] && [[ "$name" =~ \.sh$ ]] && chmod a+x $name || echo "$name not scripts file

 

发表评论

0/200
420 点赞
0 评论
收藏