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

解: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