linux學習作業-第五週

1、顯示當前系統上root、fedora或user1用戶的默認shell;

#!/bin/bash
#Program
#input username ,then print userbash
#2016/08/30 V0.0.1 rex frist 
#註明程序使用的shell,作用,日期,版本
read -p "please input you username.then ,output you default shell :" userbash #讀取輸入的用戶名
if
[ $userbash = " " ];then #如果userbash等於空,則顯示錯誤
echo -e \n" error input username"
else
usershell=$(grep "^$userbash" /etc/passwd |sed 's@.*/@ @g')  #$usershell查詢用戶名開頭,並用sed替換前面
echo -e \n"That's you shell [$usershell]" #顯示用戶shell
exit 0
fi
#上面shell功能是輸入用戶名,查詢用戶的bash信息,當然輸入空則報錯,輸入錯誤的條件沒寫

egrep "root" passwd |sed 's#.*/##g'  #這句也能直接查詢並用替換刪除行首內容



2、找出/etc/rc.d/init.d/functions文件中某單詞後面跟一組小括號的行,形如:hello();

egrep --color "\<[a-z]+\(\)" /etc/rc.d/init.d/functions #\(\)表達括號

3、使用echo命令輸出一個絕對路徑,使用grep取出其基名;

echo /usr/share/doc |grep -o "\<[[:alnum:]]\+\>$"
# grep -o 截取grep內容
# [[:alnum:]]數字加英文
# $行尾


    擴展:取出其路徑名

echo /usr/local/share/info/ |grep -o ".*\<"

.*一組詞
\<句尾錨定


4、找出ifconfig命令結果中的1-255之間數字;

ifconfig |grep -E "(\<[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5]|2[0-4][0-9])\>"
#因爲使用了-E 所以()不需要加\
#上句錨定了句首和句尾\<\>
#匹配數字1-255



5、挑戰題:寫一個模式,能匹配合理的IP地址;

grep -E "(\<[1-9]|[1-9][0-9]|1[0-9][0-9]|25[0-4]|2[0-4][0-9]\>).((\<[0-9]|1[0-9][0-9]|25[0-5]|[1-9][0-9]|2[0-4][0-9]\>)\.){2}(\<[1-9]|[1-9][0-9]|1[0-9][0-9]|25[0-4]|2[0-4][0-9]\>)"

6、挑戰題:寫一個模式,能匹配出所有的郵件地址;

grep ^[0-9,a-z,_]*@[0-9,a-z,_]*.[a-z]* 
#匹配數字,小寫英文與下劃線 @ 數字,小寫英文與下劃線 . 後接英文
egrep --color '(\<^[0-9,a-z,_]*@[0-9,a-z,_]*.[a-z]*)\>'
#上句加了句首句尾錨定


7、查找/var目錄下屬主爲root,且屬組爲mail的所有文件或目錄;

find /var/* -user root -group mail  -ls
#find -user 按照屬主查找
#find -group 按照屬組查找
#加-ls 則顯示詳細信息


8、查找當前系統上沒有屬主或屬組的文件;

find / -nouser -nogroup -ls
# find -nouser 沒有屬主
# find -nogroup 沒有屬組
# 一起用的話,就是沒有家的人兒。 T T

     進一步:查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄

find / -nouser -nogroup -atime -3 -ls
#-atime 訪問時間
#-3 3天內
# 3 3天上下1天
#+3 3天以上(不包含3天)


9、查找/etc目錄下所有用戶都有寫權限的文件;

find /etc -perm -o+w -type f -ls
#-perm 按權限查找
#-ugo 
#+有
#-沒有


10、查找/etc目錄下大於1M,且類型爲普通文件的所有文件;

find /etc  -size +1M -type f -ls
#-size 按照大小查詢
#通常的格式有kKmMgGT(沒開虛擬機,可以用man find :/-size查詢)


11、查找/etc/init.d/目錄下,所有用戶都有執行權限,且其它用戶有寫權限的文件;

find /etc/init.d -perm -o+x,ug+w -type f -ls
# -type 按文件類型選擇
# f爲普通文件
# b塊設備
# d目錄
# c字符設備
# p管道
# l管道


12、查找/usr目錄下不屬於root、bin或hadoop的文件;

find /usr -not \( -user root o -user bin -o -user hadoop \) -ls
find /usr -not \( -user root -o -user bin  \) -ls
# -not 不....
# 如果條件2個以上,並是在()內的話,則使用-o
# 如果沒有使用()的話,則使用 -a 與

13、查找/etc/目錄下至少有一類用戶沒有寫權限的文件;

find /etc -not -perm -222 -ls 
#-perm   可以使用 -uog  用戶、組、其他
#        以及用777數字權限(777是全部權限)
#-333 有一類沒有寫和讀
# 333 完全匹配
#+333 有一類有寫和讀



14、查找/etc目錄下最近一週內其內容被修改過,且不屬於root或hadoop的文件

find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls
#如上解釋,兩個條件間需要用-a連接


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章