1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。

#!/bin/bash
echo Hostname: `hostname`
echo IP address: `ifconfig|egrep -o "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}"|head -n1`
echo System version: `cat /etc/system-release`
echo kernel version: `uname -r`
echo CPU type : `lscpu |grep 'Model name'|sed 's/.*://'|sed 's/^[[:space:]]\+//'`
echo Disk size :`fdisk -l|grep "sda:"|sed 's/.*://'|sed 's/,.*//'|sed 's/^[[:space:]]\+//'`
echo Memory size : `free -m|grep "Mem"|tr -s [[:space:]]|cut -d" " -f2` MB

运行结果

[root@shao ~]# bash systeminfo.sh
Hostname: shao
IP address: 10.1.53.2
System version: CentOS Linux release 7.2.1511 (Core)
kernel version: 3.10.0-327.el7.x86_64
CPU type : Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
Disk size :214.7 GB
Memory size : 1824 MB

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

#!/bin/bash
cp -arv /etc /root/etc`date +%F`

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

#!/bin/bash
echo "The max using rate is:`df |grep "/dev/sd"| cut -c 44-46 | sort -nr | head -1`%"

运行结果

[root@shao ~]# bash disk.sh
The max using rate is: 74%

4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

#!/bin/bash
echo "the links number is:"
netstat -nt |tr -s ' '  |cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr

运行结果

[root@shao ~]# bash links.sh
the links number is:
      2 10.1.53.3

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

#!/bin/bash
echo 'The sum is'
id1=`cat /etc/passwd | sed -n '10p' | cut -d: -f3`
id2=`cat /etc/passwd | sed -n '20p' | cut -d: -f3`
let idsum=$id1+$id2
echo "The sum is:$idsum"
unset idsum

运行结果

[root@shao bin]# bash sumid.sh
The sum is:70

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

#!/bin/bash
file1_blank=`grep "^[[:space:]]*$" $1 | wc -l`
file2_blank=`grep "^[[:space:]]*$" $2 | wc -l`
sumspace=$[$file1_blank+$file2_blank]
echo "The tatal blank line:$sumspace"

运行结果

[root@shao ~]# bash sumspace.sh /etc/passwd /etc/fstab 
The tatal blank line:1

7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件

#!/bin/bash
etcnum=`ls -d /etc/*|wc -l`
varnum=`ls -d /var/*|wc -l`
usrnum=`ls -d /usr/*|wc -l`
echo "the totalfile is $[etcnum+varnum+usrnum]"

运行结果

[root@shao bin]# bash sumfile.sh
the totalfile is 298

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

#!/bin/bash
read -p "Enter a filename:" file
[ -z "$file" ] && echo "please enter a file name" && exit 2
echo -n "The  blank line number is:"
echo `grep -c  '^[[:space:]]*$'  $file`

运行结果

[root@shao ~]# bash sumspace.sh
Enter a filename:
please enter a file name
[root@shao ~]# bash sumspace.sh
Enter a filename:/etc/fstab
The  blank line number is:1

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

#!/bin/bash
[ $# -ne 1 ] && echo "please input one ipaddr" && exit 2
ping -c1 -w1 $1 &> /dev/null && echo "ping successful" || echo "ping failed"

运行结果

[root@shao bin]# bash hostping.sh
please input one ipaddr
[root@shao bin]# bash hostping.sh 10.1.254.254
ping successful

10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,发邮件通知root磁盘满

#!/bin/bash
mount=$((df;df -i) | grep "/dev/sd" | tr -s ' ' | cut -d' ' -f5 | grep -o "[[:digit:]]\+" | sort | tail -1)
[ "$mount" -ge 80 ] && echo "disk almost full" | mail -s "dangerous" root || exit 3

11、指定文件做为参数,判断文件是否为.sh后缀,如果是,添加x权限

#!/bin/bash
read -p "please input a test file: " FILENAME
[ ! -f $FILENAME ] && echo "please input a real file" && exit 100
echo $FILENAME | grep -q '.*\.sh$'  && (chmod +x $FILENAME ; echo "Add x right for the file successfully") || echo "this is not a .sh file"

运行结果

[root@shao bin]# ls
excute.sh  hostping.sh  per.sh  sumfile.sh
[root@shao bin]# bash excute.sh
please input a test file: hostping.sh
Add x right for the file successfully

12、判断输入的IP是否为合法IP

#!/bin/bash
read -p "please input one useful ip:" ip_addr
echo $ip_addr | grep -E "^(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>$" &> /dev/null  && echo "this is a useful ip" || echo "this is not a useful ip"

运行结果

[root@shao bin]# bash checkip.sh
please input one useful ip:10.1.534.1
this is not a useful ip
[root@shao bin]# bash checkip.sh
please input one useful ip:10.1.53.1
this is a useful ip

13、计算1+2+3+...+100

#!/bin/bash
#!/bin/bash
echo -e "\033[34m1-100 all positive integer's sum\033[0m : `echo {1..100} | tr " " "+" | bc`"

运行结果

[root@shao bin]# bash 100sum.sh
1-100 all positive integer's sum : 5050

方法二:seq -s + 1 100 | bc


14、输入起始值A和最后值B,计算从A+(A+1)...+(B-1)+B的总和

#!/bin/bash
read -p "first number:" a
read -p "second number:" b
[ $a -ge $b ] && echo "sum is `seq -s+ $b $a | bc`" || echo "sum is `seq -s+ $a $b | bc`"

运行结果

[root@shao bin]# bash sumAB.sh
first number:4
second number:8
sum is 30
[root@shao bin]# bash sumAB.sh
first number:100
second number:1
sum is 5050

15、chmod -rw /tmp/file1,编写脚本/root/bin/per.sh,判断当前用户对/tmp/file1文件 是否不可读且不可写

#!/bin/bash
[ -r /tmp/file1 -o  -w /tmp/file1 ] && echo "`whoami` can read or write /tmp/file1" || echo  "`whoami` can not read and can not write /tmp/file1"

运行结果

[root@shao bin]# bash per.sh
root can read or write /tmp/file1

16、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统。

禁止

#!/bin/bash
[ -e /etc/nologin ] && echo "the normal user can not login already" ; exit || touch /etc/nologin
echo "disable normal user login "

允许

#!/bin/bash
[ -e /etc/nologin ] && rm -f /etc/nologin 
echo "enable user login "

17、写一个脚本/root/bin/createuser.sh,实现如下功能: 使用一个用户名做为参数,如果指定参数的用户存在,就显 示其存在,否则添加之;显示添加的用户的id号等信息

#!/bin/bash
read -p "please input you name:" name
if cat /etc/passwd | cut -d: -f1 | grep -q "^$name$";then
    echo "the username already exits"
else
    useradd $name
    cat /etc/passwd | grep "^$name\b"
fi

运行结果

[root@shao bin]# bash createuser.sh
please input you name:shao
the username already exits
[root@shao bin]# bash createuser.sh
please input you name:liu
liu:x:1014:1019::/home/liu:/bin/bash

18、写一个脚本/root/bin/yesorno.sh,提示用户输入yes或 no,并判断用户输入的是yes还是no,或是其它信息

#!/bin/bash
[ $# -ne 1 ] && echo "please input an arg" && exit
ans=`echo $1 | tr 'A-Z' 'a-z'`
if [ $ans = yes -o $ans = y ]
then
    echo "yes"
elif [ $ans = n -o $ans = no ]
then
    echo "no"
else
    echo "error"
fi

运行结果

[root@shao bin]# bash yesorno.sh y
yes
[root@shao bin]# bash yesorno.sh 
please input an arg
[root@shao bin]# bash yesorno.sh No
no
[root@shao bin]# bash yesorno.sh YES
yes

方法二

#!/bin/bash
read -p "Please input yes or no:" ans
case $ans in
[yY]|[yY][eE][Ss])
        echo "The user input yes"
        ;;
[nN]|[Nn][oO])
        echo "The user input no"
        ;;
*)
        echo "The user input other words"
esac

19、写一个脚本/root/bin/filetype.sh,判断用户输入文件路 径,显示其文件类型(普通,目录,链接,其它文件类型)

#!/bin/bash
read -p "please input a filename:" filename
! [ -e $filename ] && echo "The filename do not exist" && exit
if  [ -h $filename ] ;then
    echo "filetype is  syslink file"
elif [ -d $filename ] ;then
    echo "filetype is dirction"
elif [ -f $filename ] ;then
    echo "filetype is common file"
else
    echo "filetype is other"
fi

运行结果

[root@shao bin]# bash filetype.sh
please input a filename:/etc
filetype is dirction
[root@shao bin]# bash filetype.sh
please input a filename:/etc/redhhat-release
The filename do not exist
[root@shao bin]# bash filetype.sh
please input a filename:/etc/redhat-release
filetype is  syslink file
[root@shao bin]# bash filetype.sh
please input a filename:/etc/issue
filetype is common file

20、写一个脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数

#!/bin/bash
[ $# -ne 1 ] && echo "Please input a number" && exit
if [[ $1 =~ ^0*[1-9][0-9]*$ ]]
then
    echo "int"
else
    echo "not int"
fi

运行结果

[root@shao bin]# bash  checkint.sh
Please input a number
[root@shao bin]# bash  checkint.sh 0
not int
[root@shao bin]# bash  checkint.sh 12
int
[root@shao bin]# bash  checkint.sh 1.1
not int