首页 / LINUX / Linux基础shell脚本题
Linux基础shell脚本题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux基础shell脚本题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4144字,纯文字阅读大概需要6分钟。
内容图文
![Linux基础shell脚本题](/upload/InfoBanner/zyjiaocheng/1089/8ac06d9cdb5a4272aa70ba92d4bc8b70.jpg)
解:vim /root/bin/sumid.sh
#!/bin/bash
tenuser=`cat /etc/passwd | head -n10 | tail -n1 |cut -d: -f3`
twentyuser=`cat /etc/passwd | head -n20 |tail -n1 | cut -d: -f3`
let sum=$[tenuser+twentyuser]
echo "The tenuser and twentyuser finally is $sum"
2、编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
解:vim /root/bin/sumspace.sh
#!/bin/bash
first=`cat $1 | grep "^[[:space:]]*$" | wc -l`
second=`cat $2 | grep "^[[:space:]]*$" | wc -l`
let sum=$[first+second]
echo $sum
3、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
解:vim /root/bin/sumfile.sh
#!/bin/bash
first=`ls -l /etc | egrep "(^d.*)|(^-.*)" | wc -l`
second=`ls -l /var | egrep "(^d.*)|(^-.*)" | wc -l`
third=`ls -l /usr | egrep "(^d.*) | (^.*)" | wc -l`
let sum=$[first+second+third]
echo $sum
4、编写脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果
参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
解:vim /root/bin/argsnum.sh
#!/bin/bash
[ "$#" -lt 1 ] && echo "The must one arg" && exit 20
[ -e $1 ] || { echo "No such file or directory" && exit 30; }
[ "$#" -ge 1 ] && echo "The blankspace is `grep "^[[:space:]]*$" $1 | wc -l`"
5、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如
果不可ping通,则提示用户“该IP地址不可访问”
解:[[ ! $1 =~ (([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) ]]
&& echo "please print right ip format!" && exit 20
`ping $1 -c1 &>/dev/null` && echo "Yes" || echo "No"
6、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
解:vim /root/bin/checkdisk.sh
disk=`df -h | grep /dev/sd | tr -s " " % | cut -d % -f5 | sort -nr | head -n1`
inode=`df -i | grep /dev/sd | tr -s " " % | cut -d % -f5 | sort -nr | head -n1`
[ "$disk" -gt 80 ] && echo "The disk is "$disk"% will man" || echo The disk size is "$disk"%
[ "$inode" -gt 5 ] && echo The inode is "$inode"% man || { echo The indoe size is "$inode" && exit 30; }
7、编写脚本/bin/per.sh,判断当前用户对指定的参数文件,是否不可读并且不可写
解:vim /bin/per.sh
#!/bin/bash
[ ! -r "$@" ] && [ ! -w "$@" ] && echo The "$@" is Not have w and r permisson or have some one permisson || echo The "$@"
have w and r permission
#另一种写法
[ ! \( -r "$@" -a -w "$@" \) ] && echo The "$@" is Not have w and r permisson or have some one permisson || echo The "$@"
have w and r permission
8、编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
解:vim /root/bin/excute.sh
[[ "$@" =~ .*\.sh ]] && `chmod a+x "$@" ` || echo The "$@" is not script file
9、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
解:vim /root/bin/nologin.sh
#!/bin/bash
touch /etc/nologin
echo "disabled common user login"
vim /root/bin/nologin.sh
#!/bin/bash
rm -rf /etc/nologin
禁止普通用户登录时执行第一个脚本,允许登录后执行第二个脚本
10、编写脚本/root/bin/agree.sh,提示用户输入各种各样的Yes或No都可以,输入错误的话提示"请输入正确选项"
解:vim /root/bin/agree.sh
#!/bin/bash
read -p "Do you agree,Plase print Yes or No: " ANS(#变量名)
[[ "$ANS" =~ [Yy]|[Yy][Ee][Ss]|[Nn][Oo]|[Nn] ]] || { echo "please input right options" && exit 20;}
11、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin
解:vim /etc/profile
PATH=$PATH:$HOME/bin:/usr/local/apache/bin
source /etc/profile
12、用户root登录时,将命令指示符变成红色,并自动启用如下别名:
rm=‘rm –i’
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ’ (如果系统是CentOS7)
解:vim ~/.bashrc
rm='rm –i'
cdnet='cd /etc/sysconfig/network-scripts/'
editnet='vim /etc/sysconfig/network-scripts/ifcfg-ens33'
export PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\]'
source ~/.bashrc
13、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
解:vim /etc/profile.d/danger.sh
#!/bin/bash
echo -e "\033[1;5;31mHi,dangerous!\033[0m"
14、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
解:vim /root/bin/createscript.sh
#!/bin/bash
touch $1
chmod +x $1
echo "#/bin/bash
#*************************************
#Filename: $1
#Author: Fang
#Date: `date "+%Y-%m-%d %H:%M:%S"`
#*************************************" > $1
vim + $1
原文:http://blog.51cto.com/kaikai0720/2045134
内容总结
以上是互联网集市为您收集整理的Linux基础shell脚本题全部内容,希望文章能够帮你解决Linux基础shell脚本题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。