shell 案例
1、復制/etc/profile至/tmp/目錄,用查找替換命令刪除/tmp/profile文件中的 行首的空白字符
cp /etc/profile /tmp/
:%s/^ \+//g
2、在vim中設置tab縮進為4個字符
進入擴展模式 : :set tabstop=4
3、20分鐘內通關vimtutor(可參考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)
4、編寫腳本 createuser.sh,實現如下功能:使用一個用戶名做為參數,如果 指定參數的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等信息
#!/bin/bash
cat /etc/passwd|cut -d: -f1|grep $1 &> /dev/null
if [ $? -eq 0 ];then
echo "$1 exist"
else
useradd $1
fi
5、編寫腳本 systeminfo.sh,顯示當前主機系統信息,包括:主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
#!/bin/bash
echo '主機系統信息,包括:主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小'
host_name=`hostname`
IPV4=`ifconfig eth0|grep -o '\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}'|head -n1`
OS=`cat /etc/redhat-release`
neihe=`uname -r`
CPU=`cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c`
neicun=`free -g|grep 'Mem'|tr -s ' ' ':'|cut -d: -f2`
disk=`df -h|tr -s ' ' ':'|cut -d ':' -f 1,2`
echo "####################################"
echo "主機名:$host_name"
echo "IPV4地址:$IPV4"
echo "操作系統版本:$OS"
echo "內核版本:$neihe"
echo "CPU型號:$CPU"
echo "內存大小:$neicun"
echo "硬盤大小:$disk"
echo "####################################"
6、編寫腳本disk.sh,顯示當前硬盤分區中空間利用率最大的值
#!/bin/bash
disk=`df -h|grep "^/dev"|tr -s " " ':'|sort -nr -t: -k5|tail -1|cut -d: -f5`
echo "$disk"

浙公網安備 33010602011771號