企業面試題 京東 問題1:使用Linux命令查詢file1中空行所在的行號。 [root@server ~]# cat file1 問題1:使用Linux命令查詢file1中空行所在的行號。 [root@server ~]# awk '/^$/{print NR}' file1 2 問題2:有文件c ...
企業面試題
京東
問題1:使用Linux命令查詢file1中空行所在的行號。
[root@server ~]# cat file1
問題1:使用Linux命令查詢file1中空行所在的行號。
[root@server ~]# awk '/^$/{print NR}' file1
2
問題2:有文件chengji.txt內容如下''
張三 40
李四 50
王五 60
使用Linux命令計算第二列的和並輸出。
[root@server ~]# cat chengji.txt
張三 40
李四 50
王五 60
[root@server ~]# awk '{sum+=$2}END{print sum}' chengji.txt
150
搜狐 訊網
問題1:Shell腳本里如何檢查一個文件是否存在?如果不存在該如何處理?
[root@server ~]# cat 3.sh
#!/bin/bash
read -p "please input filename:" filename
if [ -f $filename ]
then
echo "file exist"
else
echo "file is not exist"
touch $filename
fi
新浪
問題1:用shell寫一個腳本,對文本中無序的一列數字排序
[root@server ~]# vim test.txt
[root@server ~]# vim sort.sh +
[root@server ~]# chmod 755 sort.sh
[root@server ~]# ./sort.sh
12
21
45
56
89
169
256
646
659
6595
7879
65965
[root@server ~]# cat sort.sh
#!/bin/bash
sort -n test.txt
金和網路
問題1:請用shell腳本寫出查找當前文件夾(/home)下所有的文本文件內容中包含有字元”shen”的文件名稱
小米
問題1:
一個文本文件info.txt的內容如下:
aa,201
zz,502
bb,1
ee,42
每行都是按照逗號分隔,其中第二列都是數字,請對該文件按照第二列數字從大到小排列
[root@server ~]# sort -t "," -k 2 -nr info.txt
美團
問題1:編寫腳本實現以下功能;
每天早上5點開始做備份
要備份的是/var/mylog里所有文件和目錄可以壓縮進行備份
備份可以保存到別一臺器上192.168.1.2 FTP帳號 aaa 密碼 bbb
要求每天的備份文件要帶有當天的日期標記
[root@server ~]# vim bak.sh
#!/bin/bash
bakfir=log
date='date +%F'
cd /var
tar zcf ${bakdir}_${date}.tar.gz ${bakdir}
sleep 1
ftp -n <<- EOF
open 192.168.142.129 #遠程ftp伺服器IP
user aaa bbb
put mylog_*.tar.gz
bye
EOF
[root@server ~]# vim /etc/crontab
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
00 05 * * * /bin/bash /root/bak.sh
問題2:請用shell腳本創建一個組class、一組用戶,用戶名為stdX,X從01-30,並歸屬class組
#!/bin/bash
id -g class &>/dev/null || groupadd class
user=std
for i in {01..30}
do
id -u ${user}$i &>/dev/null || useradd -G class ${user}$i
done
百度
· ## 處理以下文件內容,將功能變數名稱取出併進行計數排序,如處理:
http://www.baidu.com/more/
http://www.baidu.com/guding/more.html
http://www.baidu.com/events/20060105/photomore.html
http://hi.baidu.com/browse/
http://www.sina.com.cn/head/www20021123am.shtml
http://www.sina.com.cn/head/www20041223am.shtml
cut -d'/' -f3 test.txt | sort | uniq -c | sort -nr
奇虎360
1、寫一個腳本查找最後創建時間是3天前,尾碼是*.log的文件並刪除。**
find / -name “*.log” -ctime +3 -exec rm -f {} \;
2、寫一個腳本將某目錄下大於100k的文件移動至/tmp下。
for i in `find /test -type f -size +100k`;do cd /test && mv $i /tmp;done
3、寫一個腳本進行nginx日誌統計,得到訪問ip最多的前10個(nginx日誌路
徑:/home/logs/nginx/default/access.log
awk '{a[$1]++}END{for (j in a) print a[j],j}' /home/logs/nginx/default/access.log|sort -nr|head
4、寫一個腳本把指定文件里的/usr/local替換為別的目錄。
sed 's:/user/local:/tmp:g' filename
滴滴出行
1、指令:ls | grep “[ad]*.conf” 命令解釋正確的是:
正確答案: B
A 顯示包含a 或者d 為開頭,後接任何字元,再後面是.conf字元的文件(或目錄)
B 顯示包含a 或者d 出現0 次或無數次,後面是.conf字元的文件(或目錄)
C 顯示包含字母a 或者d出現0次或1次,後面是.conf字元的文件(或目錄)
D 顯示從字母a 到d ,後接任何字元,再後面是.conf字元的文件(或目錄)
2、找出IO重定向執行結果與其他三個不同的:
正確答案: B
A ./run.sh >run.log 2>&1;
B ./run.sh 2>&1 >run.log;
C ./run.sh &>run.log;
D ./run.sh 2>run.log >&2
3、一個文件,大概1億行,每行一個ip,將出現次數最多的top10輸出到一個新的文件中
awk -F" " '{IP[$1]++}END{for (i in IP) print IP[i],i}' /var/log/filename.log |sort -k2 -rn |head -10 >text.txt
awk -F" " '{print $1}' /var/log/filename.log |sort -n |uniq -c |sort -rn -k1 |head -10 > text.txt
練習
案例1:監控硬體信息
[root@master shell]# cat info.sh
#!/bin/bash
#顯示伺服器硬體信息.
echo -e "\033[34m---------伺服器硬體信息---------\033[0m"
echo -e "\033[32m網卡信息如下:\033[0m"
ifconfig ens33 | grep "inet "
# ifconfig 需要裝包net-tools 註意網卡名稱
echo -e "\033[32m剩餘記憶體容量信息如下:\033[0m"
grep MemAvailable /proc/meminfo
echo -e "\033[32m磁碟容量信息如下:\033[0m"
df -h /
echo -e "\033[32mCPU信息如下:\033[0m"
grep "model name" /proc/cpuinfo
[root@master shell]# ./info.sh
---------伺服器硬體信息---------
網卡信息如下:
inet 192.168.11.112 netmask 255.255.255.0 broadcast 192.168.11.255
剩餘記憶體容量信息如下:
MemAvailable: 596376 kB
磁碟容量信息如下:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.5G 16G 9% /
CPU信息如下:
model name : AMD Ryzen 7 4800H with Radeon Graphics
model name : AMD Ryzen 7 4800H with Radeon Graphics
model name : AMD Ryzen 7 4800H with Radeon Graphics
model name : AMD Ryzen 7 4800H with Radeon Graphics
案例2:數據計算
[root@master shell]# cat calc.sh
#!/bin/bash
#計算1+2+3,...,+n的和,可以使用n*(n+1)/2公式快速計算結果
read -p "請輸入一個正整數:" num
sum=$[num*(num+1)/2]
echo -e "\033[32m$num以內整數的總和是:$sum\033[0m"
#使用三角形的底邊和高計算面積:A=1/2bh
read -p "請輸入三角形底邊長度:" bottom
read -p "請輸入三角形高度:" hight
A=$(echo "scale=1;1/2*$bottom*$hight" | bc)
echo -e "\033[32m三角形面積是:$A\033[0m"
#梯形面積:(上底邊長度+下底邊長度)*高/2
read -p "請輸入梯形上底邊長度:" a
read -p "請輸入梯形下底邊長度:" b
read -p "請輸入梯形高度:" h
A=$(echo "scale=2;($a+$b)*$h/2" | bc)
echo -e "\033[32m梯形面積是:$A\033[0m"
#使用A=πr2公式計算圓的面積,取2位小數點精度,π=3.14
read -p "請輸入圓的半徑:" r
A=$(echo "scale=2;3.14*$r^2" | bc)
echo -e "\033[32m圓的面積是:$A\033[0m"
[root@master shell]# ./calc.sh
請輸入一個正整數:6
6以內整數的總和是:21
請輸入三角形底邊長度:2
請輸入三角形高度:3
三角形面積是:3.0
請輸入梯形上底邊長度:3
請輸入梯形下底邊長度:4
請輸入梯形高度:2
梯形面積是:7.00
請輸入圓的半徑:5
圓的面積是:78.50
案例3:自動配置yum源
[root@master shell]# cat yum.sh
#!/bin/bash
#定義YUM源路徑
URL=ftp://192.168.4.1/centos
#創建YUM源配置文件
echo "[CentOS]
name=centos
baseurl=$URL
gpgcheck=0" > /etc/yum.repos.d/yum.repo
[root@master shell]# cat /etc/yum.repos.d/yum.repo
[CentOS]
name=centos
baseurl=ftp://192.168.4.1/centos
gpgcheck=0
案例4:監控系統信息
[root@master shell]# cat info2.sh
#!/bin/bash
#本腳本獲取系統各項性能參數指標,並與預設閾值進行比較
#time:時間,loalip:eth0網卡IP,free_mem:剩餘記憶體大小,free_disk:剩餘磁碟大小
#cpu_load:15min平均負載,login_user:登錄系統的用戶,procs:當前進程數量
local_time=$(date +"%Y%m%d %H:%M:%S")
#註意網卡名稱,ifconfig需要安裝net-tools
local_ip=$(ifconfig ens33 | grep netmask | tr -s " " | cut -d" " -f3)
free_mem=$(cat /proc/meminfo |grep Avai | tr -s " " | cut -d" " -f2)
free_disk=$(df | grep "/$" | tr -s " " | cut -d' ' -f4)
cpu_load=$(cat /proc/loadavg | cut -d' ' -f3)
login_user=$(who | wc -l)
procs=$(ps aux | wc -l)
#當剩餘記憶體不足1GB時發送郵件給root進行報警
[ $free_mem -lt 1048576 ] && \
echo "$local_time Free memory not enough.
Free_mem:$free_mem on $local_ip" | \
#安裝mailx工具
mail -s Warning root@localhost
#當剩餘磁碟不足10GB時發送郵件給root進行報警
[ $free_disk -lt 10485760 ] && \
echo "$local_time Free disk not enough.
root_free_disk:$free_disk on $local_ip" | \
mail -s Warning root@localhost
#當CPU的15min平均負載超過4時發送郵件給root進行報警
result=$(echo "$cpu_load > 4" | bc)
[ $result -eq 1 ] && \
echo "$local_time CPU load to high
CPU 15 averageload:$cpu_load on $local_ip" | \
mail -s Warning root@localhost
#當系統實時線上人數超過3人時發送郵件給root進行報警
[ $login_user -gt 3 ] && \
echo "$local_time Too many user.
$login_user users login to $local_ip" | \
mail -s Warning root@localhost
#當實時進程數量大於500時發送郵件給root進行報警
[ $procs -gt 500 ] && \
echo "$local_time Too many procs.
$procs proc are runing on $local_ip" | \
mail -s Warning root@localhost
案例5:判斷用戶名與密碼是否為空
版本1:
[root@master shell]# cat user_v1.sh
#!/bin/bash
read -p "請輸入用戶名:" user
read -s -p "請輸入密碼:" pass
if [ ! -z "$user" ];then
useradd "$user"
fi
if [ ! -z "$pass" ];then
echo "$pass" | passwd --stdin "$user"
fi
echo
版本2:
[root@master shell]# cat user_v2.sh
#!/bin/bash
read -p "請輸入用戶名:" user
read -s -p "請輸入密碼:" pass
if [ ! -z "$user" ] && [ ! -z "$pass" ];then
useradd "$user"
echo "$pass" | passwd --stdin "$user"
fi
案例6:測試主機是否ping通
[root@master shell]# cat if_ping.sh
#!/bin/bash
#ping通腳本返回up,否則返回down
if [ -z "$1" ];then
echo -n "用法: 腳本 "
echo -e "\033[32m功能變數名稱或IP\033[0m"
exit
fi
#-c(設置ping的次數),-i(設置ping的間隔描述),-W(設置超時時間)
ping -c2 -i0.1 -W1 "$1" &>/dev/null
if [ $? -eq 0 ];then
echo "$1 is up"
else
echo "$1 is down"
fi
案例7:猜數字
[root@master shell]# cat guess_num.sh
#!/bin/bash
#腳本自動生成10以內的隨機數,根據用戶的輸入,輸出判斷結果.
clear
num=$[RANDOM%10+1]
read -p "請輸入1-10之間的整數:" guess
if [ $guess -eq $num ];then
echo "恭喜,猜對了,就是:$num"
elif [ $guess -lt $num ];then
echo "Oops,猜小了."
else
echo "Oops,猜大了."
fi
案例8:列印九九乘法表
[root@master shell]# cat 99.sh
#!/bin/bash
for((i=1;i<=9;i++))
do
for((j=1;j<=i;j++))
do
echo -n "$i*$j=$[i*j] "
done
echo
done
案例9:猜隨機數
[root@master shell]# cat guess_random.sh
#!/bin/bash
num=$[RANDOM%10+1]
while :
do
read -p "請輸入1-10之間的整數:" guess
if [ $guess -eq $num ];then
echo "恭喜,猜對了,就是:$num"
exit
elif [ $guess -lt $num ];then
echo "Oops,猜小了."
else
echo "Oops,猜大了."
fi
done
案例10:一鍵部署FTP伺服器
[root@master shell]# cat install_vsftp.sh
#!/bin/bash
#安裝ftpd軟體,修改配置文件,設置匿名用戶可以上傳文件.
if rpm -q vsftpd &> /dev/null ;then
echo "vsftpd已安裝."
else
yum -y install vsftpd &> /dev/null
fi
systemctl restart vsftpd
案例11:監控網路流量
[root@master shell]# cat net.sh
#!/bin/bash
while :
do
clear
echo '本地網卡ens33流量信息如下: '
ifconfig ens33 | grep "RX pack" | tr -s " " | cut -d" " -f6
ifconfig ens33 | grep "TX pack" | tr -s " " | cut -d" " -f6
sleep 1
done
案例12:統計閏年
[root@master shell]# cat leap.sh
#!/bin/bash
#功能描述(Description):判斷有序的數字是否為閏年
#條件1:能被4整除但不能被100整除)條件2:能被400整除
#滿足條件1或條件2之一就是閏年
for i in {1..5000}
do
if [[ $[i%4] -eq 0 && $[i%100] -ne 0 || $[i%400] -eq 0 ]];then
echo "$i:是閏年"
else
echo "$i:非閏年"
fi
done
案例13:計算等差數列之和
[root@master shell]# cat sum.sh
#!/bin/bash
#功能描述(Description):計算等差數列之和1+2+3+,...,+100
sum=0;i=1
while [ $i -le 100 ]
do
let sum+=$i
let i++
done
echo -e "1+2+3+,...,+100的總和為:\033[1;32m$sum\033[0m"
案例14:判斷用戶輸入
[root@master shell]# cat case1.sh
#!/bin/bash
read -p "請輸入redhat|fedora:" key
case $key in
redhat)
echo "fedora.";;
fedora)
echo "redhat.";;
*)
echo "必須輸入redhat或fedora."
esac
[root@master shell]# cat case2.sh
#!/bin/bash
read -p "Are you sure?[y/n]:" sure
case $sure in
y|Y|yes|YES)
echo "you enter $sure,OK";;
n|N|NO|no)
echo "you enter $sure,OVER";;
*)
echo "error";;
esac
作者:ChAn
出處:http://www.cnblogs.com/sre-chan/
-------------------------------------------
個性簽名:今天做了別人不想做的事,明天你就做得到別人做不到的事,嘗試你都不敢,你拿什麼贏!
如果覺得這篇文章對你有小小的幫助的話,記得在右下角點個“推薦”哦,博主在此感謝!