1. 程式人生 > >Linux基礎shell腳本題

Linux基礎shell腳本題

shell腳本

1、編寫腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和

解:vim /root/bin/sumid.sh

#!/bin/bash

tenuser=`cat /etc/passwd | head -n10 | tail -n1 |cut -d: -f3`

twentyuser=`cat /etc/passwd | head -n20 |tail -n1 | cut -d: -f3`

let sum=$[tenuser+twentyuser]

echo "The tenuser and twentyuser finally is $sum"


2、編寫腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和

解:vim /root/bin/sumspace.sh

#!/bin/bash

first=`cat $1 | grep "^[[:space:]]*$" | wc -l`

second=`cat $2 | grep "^[[:space:]]*$" | wc -l`

let sum=$[first+second]

echo $sum


3、編寫腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件

解:vim /root/bin/sumfile.sh

#!/bin/bash

first=`ls -l /etc | egrep "(^d.*)|(^-.*)" | wc -l`

second=`ls -l /var | egrep "(^d.*)|(^-.*)" | wc -l`

third=`ls -l /usr | egrep "(^d.*) | (^.*)" | wc -l`

let sum=$[first+second+third]

echo $sum


4、編寫腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小於1,則提示用戶“至少應該給一個參數”,並立即退出;如果


參數個數不小於1,則顯示第一個參數所指向的文件中的空白行數

解:vim /root/bin/argsnum.sh

#!/bin/bash

[ "$#" -lt 1 ] && echo "The must one arg" && exit 20

[ -e $1 ] || { echo "No such file or directory" && exit 30; }

[ "$#" -ge 1 ] && echo "The blankspace is `grep "^[[:space:]]*$" $1 | wc -l`"


5、編寫腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如


果不可ping通,則提示用戶“該IP地址不可訪問”

解:[[ ! $1 =~ (([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) ]]


&& echo "please print right ip format!" && exit 20

`ping $1 -c1 &>/dev/null` && echo "Yes" || echo "No"


6、編寫腳本/root/bin/checkdisk.sh,檢查磁盤分區空間和inode使用率,如果超過80%,就發廣播警告空間將滿

解:vim /root/bin/checkdisk.sh

disk=`df -h | grep /dev/sd | tr -s " " % | cut -d % -f5 | sort -nr | head -n1`

inode=`df -i | grep /dev/sd | tr -s " " % | cut -d % -f5 | sort -nr | head -n1`

[ "$disk" -gt 80 ] && echo "The disk is "$disk"% will man" || echo The disk size is "$disk"%

[ "$inode" -gt 5 ] && echo The inode is "$inode"% man || { echo The indoe size is "$inode" && exit 30; }


7、編寫腳本/bin/per.sh,判斷當前用戶對指定的參數文件,是否不可讀並且不可寫

解:vim /bin/per.sh

#!/bin/bash

[ ! -r "$@" ] && [ ! -w "$@" ] && echo The "$@" is Not have w and r permisson or have some one permisson || echo The "$@"


have w and r permission

#另一種寫法

[ ! \( -r "$@" -a -w "$@" \) ] && echo The "$@" is Not have w and r permisson or have some one permisson || echo The "$@"


have w and r permission


8、編寫腳本/root/bin/excute.sh ,判斷參數文件是否為sh後綴的普通文件,如果是,添加所有人可執行權限,否則提示用戶非腳本文件

解:vim /root/bin/excute.sh

[[ "$@" =~ .*\.sh ]] && `chmod a+x "$@" ` || echo The "$@" is not script file



9、編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統

解:vim /root/bin/nologin.sh

#!/bin/bash

touch /etc/nologin

echo "disabled common user login"


vim /root/bin/nologin.sh

#!/bin/bash

rm -rf /etc/nologin

禁止普通用戶登錄時執行第一個腳本,允許登錄後執行第二個腳本


10、編寫腳本/root/bin/agree.sh,提示用戶輸入各種各樣的Yes或No都可以,輸入錯誤的話提示"請輸入正確選項"

解:vim /root/bin/agree.sh

#!/bin/bash

read -p "Do you agree,Plase print Yes or No: " ANS(#變量名)

[[ "$ANS" =~ [Yy]|[Yy][Ee][Ss]|[Nn][Oo]|[Nn] ]] || { echo "please input right options" && exit 20;}


11、讓所有用戶的PATH環境變量的值多出一個路徑,例如:/usr/local/apache/bin

解:vim /etc/profile

PATH=$PATH:$HOME/bin:/usr/local/apache/bin

source /etc/profile


12、用戶root登錄時,將命令指示符變成紅色,並自動啟用如下別名:

rm=‘rm –i’

cdnet=‘cd /etc/sysconfig/network-scripts/’

editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’

editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ’ (如果系統是CentOS7)

解:vim ~/.bashrc

rm='rm –i'

cdnet='cd /etc/sysconfig/network-scripts/'

editnet='vim /etc/sysconfig/network-scripts/ifcfg-ens33'

export PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\]'


source ~/.bashrc


13、任意用戶登錄系統時,顯示紅色字體的警示提醒信息“Hi,dangerous!”

解:vim /etc/profile.d/danger.sh

#!/bin/bash

echo -e "\033[1;5;31mHi,dangerous!\033[0m"


14、編寫生成腳本基本格式的腳本,包括作者,聯系方式,版本,時間,描述等

解:vim /root/bin/createscript.sh

#!/bin/bash

touch $1

chmod +x $1

echo "#/bin/bash

#*************************************

#Filename: $1

#Author: Fang

#Date: `date "+%Y-%m-%d %H:%M:%S"`

#*************************************" > $1

vim + $1


Linux基礎shell腳本題