标签:config usr UI 编写 echo test touch 权限 netstat
shell脚本练习题 #!/bin/bash
# 显示电脑硬件信息
echo "主机名: $(hostname)"
echo "IPv4地址: $(ifconfig | head -n2 |tail -n1 |tr -s " " ":"| cut -d":" -f3)"
echo "操作系统版本:$(cat /etc/redhat-release)"
echo "内核版本: $(uname -r)"
echo "CPU型号: $(cat /proc/cpuinfo|grep "model name"|uniq -c|cut -d":" -f2)"
echo "内存大小: $(free -m | head -n2 |tail -n1|tr -s " " ":"|cut -d":" -f2)"
echo "硬盘大小: $(fdisk -l | grep "GB"|cut -d":" -f2|cut -d"," -f1)"
#!/bin/bash
cp -r /etc/ /root/etc"$(date +%F)"
#!/bin/bash
max=$(df |tr -s " " ":"|cut -d ":" -f5|egrep -o "[[:digit:]]*"|sort -nr|head -n1)
echo "当前硬盘分区中空间利用率最大的值为: $max"
#!/bin/bash
links=$(netstat -tan| grep "^[t]\|[u]"|tr -s " " ":"|cut -d":" -f6|uniq -c|sort -nr|egrep "\<(([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])"|tail -n1)
echo "连接数和IP地址分别为:$links"
exit 0
#!/bin/bash
usera=$(cat -n /etc/passwd | head |tail -n1|cut -d":" -f3)
echo "id为10的uid:$usera"
userb=$(cat -n /etc/passwd | head -n20|tail -n1|cut -d":" -f3)
echo "id为20的uid:$userb"
# userall=$usera+$userb
let usera=$usera+$userb
echo "id和为: $usera"
exit 0
#!/bin/bash
a=$(cat $1|grep "^[[:space:]]*$"|wc -l)
b=$(cat $2|grep "^[[:space:]]*$"|wc -l)
#let a=$a+$b
let c=$a+$b
echo "$1,$2两个文件的空白行之和为$c"
exit 0
#!/bin/bash
ef=$(ls /etc/* | grep ".*[:]$"|wc -l)
ed=$(ls /etc/* | grep -v ".*[:]$"| grep -v "^[[:space:]]*$"|wc -l)
vf=$(ls /var/* | grep ".*[:]$"|wc -l)
vd=$(ls /var/* | grep -v ".*[:]$"| grep -v "^[[:space:]]*$"|wc -l)
uf=$(ls /usr/* | grep ".*[:]$"|wc -l)
ud=$(ls /usr/* | grep -v ".*[:]$"| grep -v "^[[:space:]]*$"|wc -l)
echo "/etc目录中一共有$ef个一级子目录,$ed个文件"
echo "/var目录中一共有$vf个一级子目录,$vd个文件"
echo "/usr目录中一共有$uf个一级子目录,$ud个文件"
#!/bin/bash
# ------------------------------------------
# Filename: test.sh(此处会根据文件名自动更换)
# Revision: 1.0
# Date: 2017-01-22(此处会自动变换为当前日期)
# Author: Nanyibo
# Email: nanyibo@gmail.com
# ------------------------------------------
# Copyright: 2017 nanyibo
# License: GPL
#!/bin/bash
touch /root/bin/$1
chmod +x /root/bin/$1
echo "#!/bin/bash
# ------------------------------------------
# Filename: $1(此处会根据文件名自动更换)
# Revision: 1.0
# Date: date +%F(此处会自动变换为当前日期)
# Author: xiaoyu
# Email: 851812316@qq.com
# ------------------------------------------
# Copyright: 2018 xiaoyu
# License: GPL" > /root/bin/$1
vim /root/bin/$1
第一次把课后作业在头天晚上完成了,特此来记录一下。加油~
标签:config usr UI 编写 echo test touch 权限 netstat
原文地址:http://blog.51cto.com/13572413/2063930