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`
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
[ "$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 "^(\.){3}\$" &> /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
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