shell脚本练习集合1
生活随笔
收集整理的這篇文章主要介紹了
shell脚本练习集合1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
練習題
- 練習題一:ifconfig 網卡可以顯示此網卡的信息
- 練習題二:找出能登錄系統用戶中UID最大的用戶,并顯示其名稱
- 練習題三:檢測系統中是否存在hellow用戶
- 練習題四:檢測數字是否大于等于10,是輸出yes,不是輸出no
- 練習題五:檢測目標是否存在,并判斷目標的類型
- 練習題六:請顯示系統中能被su命令切換的用戶名稱
- 練習題七:編寫腳本Apache.sh,此腳本接入的什么數字,http的端口就改為這個數字。(假設selinux關閉)
練習題一:ifconfig 網卡可以顯示此網卡的信息
顯示信息包含此網卡使用的ip地址,請用命令過濾此網卡ip,且只顯示ip不顯示其他信息
ifconfig | cut -d: '' 10 | head -2練習題二:找出能登錄系統用戶中UID最大的用戶,并顯示其名稱
grep /bin/bash /etc/passwd | sort -t : -nrk 3 | head -1| cut -d: -f 1練習題三:檢測系統中是否存在hellow用戶
方法一:
vim test.sh;
方法二:
vim test2.sh
練習題四:檢測數字是否大于等于10,是輸出yes,不是輸出no
vim num_check.sh:
#!/bin/bash [ "$1" -ge "0" -a "$1" -lt "10" ] && { echo no } || { echo yes
}
練習題五:檢測目標是否存在,并判斷目標的類型
vim check.sh;
#!/bin/bash [ -z "$1" ] && { echo "Error:nothing fing, Please input something !!" exit }[ -e "$1" ] && { echo "Error: $1 is not exits !!" exit }[ -L "$1" ] && { echo "$1 is a link file" exit }[ -f "$1" ] && {echo " $1 is a common file"exit}[ -d "$1" ] && {echo "$1 is a directory"exit }[ -S "$1" ] && {echo "$1 is a socket"exit}[ -b "$1" ] && {echo "$1 is a device file"exit }[ -c "$1" ] && {echo "$1 is a char"exit }練習題六:請顯示系統中能被su命令切換的用戶名稱
vim su.sh:
#!/bin/bash grep -E “bash$|sh$” /etc/passwd | cut -d : -f 1練習題七:編寫腳本Apache.sh,此腳本接入的什么數字,http的端口就改為這個數字。(假設selinux關閉)
#!/bin/bash setenforce 0 &> /dev/null[ -z "$1" ] && {echo "Error:Please input port following script !!"exit }rpm -q httpd &> /dev/null || {echo "Error:Apache is not installed !!"exit }systemctl status httpd | grep "running" &> /dev/null || {echo "Error:Apache is not running !!"exit }netstat -antlupe | grep -E ":$1\>" &> /dev/null && {echo "Error:$1 is in used !!"exit }sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf systemctl restart httpd總結
以上是生活随笔為你收集整理的shell脚本练习集合1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL语法学习笔记
- 下一篇: 阿里-蚂蚁金服社招面经