單純提供一個相對的解答,並不是標準答案!
單純就是個解答的參考,寫完之後再來這邊查查看答案跟你想的一樣不一樣!?
[student@station5-237 16:07 15 ~]$ man grep
......
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
......
意思是,會捨棄含有關鍵字的行,而列出不含關鍵字的行的意思![student@station5-237 16:14 17 ~]$ df 檔案系統 1K-區段 已用 可用 已用% 掛載點 devtmpfs 918108 0 918108 0% /dev tmpfs 936140 0 936140 0% /dev/shm tmpfs 936140 9372 926768 2% /run tmpfs 936140 0 936140 0% /sys/fs/cgroup /dev/mapper/centos-root 10475520 4681452 5794068 45% / /dev/mapper/centos-home 3135488 70824 3064664 3% /home /dev/vda2 1998672 149448 1727984 8% /boot tmpfs 187228 16 187212 1% /run/user/42 tmpfs 187228 4 187224 1% /run/user/0 [student@station5-237 16:14 18 ~]$ df | grep tmpfs devtmpfs 918108 0 918108 0% /dev tmpfs 936140 0 936140 0% /dev/shm tmpfs 936140 9372 926768 2% /run tmpfs 936140 0 936140 0% /sys/fs/cgroup tmpfs 187228 16 187212 1% /run/user/42 tmpfs 187228 4 187224 1% /run/user/0 [student@station5-237 16:14 19 ~]$ df | grep -v tmpfs 檔案系統 1K-區段 已用 可用 已用% 掛載點 /dev/mapper/centos-root 10475520 4681452 5794068 45% / /dev/mapper/centos-home 3135488 70824 3064664 3% /home /dev/vda2 1998672 149448 1727984 8% /boot最後可以成功的找到非記憶體內的實體裝置的容量資訊!這樣比較好查詢實際檔案系統的資源。
[student@station5-237 16:14 20 ~]$ vim .bashrc alias df2='df | grep -v tmpfs' [student@station5-237 16:47 21 ~]$ source .bashrc [student@station5-237 16:47 22 ~]$ df2 檔案系統 1K-區段 已用 可用 已用% 掛載點 /dev/mapper/centos-root 10475520 4681120 5794400 45% / /dev/mapper/centos-home 3135488 70824 3064664 3% /home /dev/vda2 1998672 149448 1727984 8% /boot
# A. 由 /etc/services 找出關鍵字 [student@station5-237 16:47 23 ~]$ grep 'http' /etc/services # http://www.iana.org/assignments/port-numbers http 80/tcp www www-http # WorldWideWeb HTTP http 80/udp www www-http # HyperText Transfer Protocol ..... # B. 關鍵字在行首 [student@station5-237 16:47 23 ~]$ grep '^http' /etc/services http 80/tcp www www-http # WorldWideWeb HTTP http 80/udp www www-http # HyperText Transfer Protocol http 80/sctp # HyperText Transfer Protocol https 443/tcp # http protocol over TLS/SSL ...... httpx 4180/tcp # HTTPX httpx 4180/udp # HTTPX ...... # C. 有兩個關鍵字組合 [student@station5-237 16:47 23 ~]$ grep '^http[ s]' /etc/services http 80/tcp www www-http # WorldWideWeb HTTP http 80/udp www www-http # HyperText Transfer Protocol http 80/sctp # HyperText Transfer Protocol https 443/tcp # http protocol over TLS/SSL https 443/udp # http protocol over TLS/SSL https 443/sctp # http protocol over TLS/SSL https-wmap 8991/tcp # webmail HTTPS service https-wmap 8991/udp # webmail HTTPS service # D. 關鍵字後接空白字元與 tab 按鈕時 [student@station5-237 16:47 23 ~]$ grep '^http[ s][[:blank:]]' /etc/services http 80/tcp www www-http # WorldWideWeb HTTP http 80/udp www www-http # HyperText Transfer Protocol http 80/sctp # HyperText Transfer Protocol https 443/tcp # http protocol over TLS/SSL https 443/udp # http protocol over TLS/SSL https 443/sctp # http protocol over TLS/SSL # E. 再加上其他額外的關鍵字 [student@station5-237 16:47 23 ~]$ grep '^http.*80' /etc/services http 80/tcp www www-http # WorldWideWeb HTTP http 80/udp www www-http # HyperText Transfer Protocol http 80/sctp # HyperText Transfer Protocol http-mgmt 280/tcp # http-mgmt http-mgmt 280/udp # http-mgmt httpx 4180/tcp # HTTPX httpx 4180/udp # HTTPX # F. 找出星號那一行 [student@station5-237 17:18 29 ~]$ grep '*' /etc/services sql*net 66/tcp # Oracle SQL*NET sql*net 66/udp # Oracle SQL*NET exp1 1021/tcp # RFC3692-style Experiment 1 (*) [RFC4727] exp1 1021/udp # RFC3692-style Experiment 1 (*) [RFC4727] # G. 星號前面是英文喔 (這題就重要了!) [student@station5-237 17:20 31 ~]$ grep '[[:alpha:]]\*' /etc/services sql*net 66/tcp # Oracle SQL*NET sql*net 66/udp # Oracle SQL*NET pvsw-inet 2441/tcp # Pervasive I*net Data Server pvsw-inet 2441/udp # Pervasive I*net Data Server rtimeviewer 6900/tcp # R*TIME Viewer Data Interface # H. 一個數字,且數字前為大寫字元 [student@station5-237 17:22 33 ~]$ grep '[0-9][[:upper:]]' /etc/services 914c/g 211/tcp 914c-g # Texas Instruments 914C/G Terminal ....... # I. 承上,但是數字在行首 [student@station5-237 17:22 34 ~]$ grep '^[0-9][[:upper:]]' /etc/services 3Com-nsd 1742/tcp # 3Com-nsd 3Com-nsd 1742/udp # 3Com-nsd # J. .conf 在結尾喔! [student@station5-237 17:25 36 ~]$ find /etc | grep '\.conf$' /etc/fonts/conf.d/59-liberation-mono.conf /etc/fonts/conf.d/59-lohit-devanagari.conf /etc/fonts/conf.d/59-liberation-sans.conf ...... # K. 承上,同時含有大寫或數字 [student@station5-237 17:27 39 ~]$ find /etc | grep '\.conf$' | grep '[[:upper:][:digit:]]' /etc/fonts/conf.d/66-sil-nuosu.conf /etc/fonts/conf.d/31-cantarell.conf ......
# A. 找出結尾的關鍵字 [student@station5-237 17:40 46 ~]$ grep 'bash$' /etc/passwd root:x:0:0:root:/root:/bin/bash student:x:1000:1000:student:/home/student:/bin/bash ...... # B. 承上,但是取代資料!要注意, / 在 sed 內是特殊字元,所以要跳脫! [student@station5-237 17:55 47 ~]$ grep 'bash$' /etc/passwd | sed 's/\/bin\/bash/\/sbin\/nologin/g' root:x:0:0:root:/root:/sbin/nologin student:x:1000:1000:student:/home/student:/sbin/nologin ...... # C. 開始字元的轉化! [student@station5-237 17:57 48 ~]$ grep 'bash$' /etc/passwd | sed 's/\/bin\/bash/\/sbin\/nologin/g' | tr 'a-z' 'A-Z' ROOT:X:0:0:ROOT:/ROOT:/SBIN/NOLOGIN STUDENT:X:1000:1000:STUDENT:/HOME/STUDENT:/SBIN/NOLOGIN
# A. 使用 bash 或 sh 去執行,而不是直接執行 myid.sh 的方法 [student@station5-237 14:22 4 bin]$ sh myid.sh This script will show your accout messages. The 'id' command output is: uid=1000(student) gid=1000(student) groups=1000(student) ..... your user's home is: /home/student your history record: 10000 your command aliases: your home dir's filenames: myid.sh: 列 12: ll:命令找不到 # 你沒看錯~執行之後,確實會有某部份的問題發生!這是等等會回答的部份! # B. 在執行前,先顯示腳本程式碼的過程,加上 -x 即可! [student@station5-237 14:25 5 bin]$ sh -x myid.sh + echo 'This script will show your accout messages.' This script will show your accout messages. + echo 'The '\''id'\'' command output is: ' The 'id' command output is: + id uid=1000(student) gid=1000(student) groups=1000(student) ...... + echo 'your user'\''s home is: /home/student' your user's home is: /home/student + echo 'your history record: 10000' your history record: 10000 + echo 'your command aliases: ' your command aliases: + alias + echo 'your home dir'\''s filenames: ' your home dir's filenames: + ll /home/student myid.sh: 列 12: ll:命令找不到 # 你會發現,指令運作前,會以 + 先顯示程式碼喔!這個動作在檢視哪邊出錯很有用! # C. 若需要直接執行,就需要給予 r, x 的權限才行!否則會出錯!如下: [student@station5-237 14:26 6 bin]$ myid.sh -bash: /home/student/bin/myid.sh: 拒絕不符權限的操作 # 確實可以直接執行 (因為在 $PATH 路徑內),但是權限不足! [student@station5-237 14:29 7 bin]$ ll myid.sh -rw-rw-r--. 1 student student 341 5月 3 14:22 myid.sh # 確實是沒有 x 的權限! [student@station5-237 14:29 9 bin]$ chmod a+x myid.sh [student@station5-237 14:29 10 bin]$ myid.sh This script will show your accout messages. The 'id' command output is: uid=1000(student) gid=1000(student) groups=1000(student) ...... your user's home is: /home/student your history record: 10000 your command aliases: your home dir's filenames: /home/student/bin/myid.sh: 列 12: ll:命令找不到 # 加上 x 的權限,並且放在 $PATH 路徑內,就能直接執行了! # D. 使用絕對路徑來執行: [student@station5-237 14:29 11 bin]$ /home/student/bin/myid.sh # E. 使用本目錄底下的方式來執行: [student@station5-237 14:33 12 bin]$ ./myid.sh # F. 上面的幾個方法都是額外產生一個 process 來執行 bash,若須原本環境: [student@station5-237 14:33 13 bin]$ source myid.sh This script will show your accout messages. The 'id' command output is: uid=1000(student) gid=1000(student) groups=1000(student) ..... your user's home is: /home/student your history record: 10000 your command aliases: alias cp='cp -i' <==這裡終於有 alias 了! alias df2='df | grep -v tmpfs' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' ....... your home dir's filenames: 總計 44 <==ll 這個指令也終於能用了! drwxrwxr-x. 2 student student 47 5月 3 14:22 bin drwxrwxr-x. 2 student student 18 4月 15 08:34 cmd drwx------. 2 student student 6 3月 1 15:35 Desktop drwx------. 2 student student 6 3月 1 15:35 Documents ......shell script 有各種執行的方法,每一種方法獲得的結果可能都不太一樣!最跟人家不同的,大概就是 source 或者是 . 這種方案!因為那是將指令丟到本來的 bash 去執行,而不是產生一個新的 bash 程序喔!相當不同!
# 這裡說的是直接執行,並不是使用 source 的方案。先看看執行的行為: [student@station5-237 14:45 19 bin]$ gototmp.sh /tmp [student@station5-237 14:47 20 bin]$ pwd /home/student/bin # 事實上,真的這隻腳本『有去過 /tmp 』目錄,只是那是另一個 bash 的事情! ──(本bash)──┬---------- (sleep) ---------┬──> │ │ └── gototmp.sh (/tmp) ──┘ # 大致如上圖示,只有切換到下面的環境時,才是 /tmp 囉!也就是說,執行 gototmp.sh 時,那是另一支 bash 的環境,並不是本來的 bash !而 gototmp.sh 執行完畢後, 該程序就消失,因此就又回到原本的 bash 環境下!所以,原本所在的工作目錄是不會改變的!
[student@station5-237 14:58 23 tmp]$ vim ~/myenv.sh # 環境設定檔 MYIP=$( ifconfig ens3 | grep 'inet '| awk '{print $2}' ) mywork="/usr/local/libexec" alias megacli="/opt/mega/cli/command"
[student@station5-237 15:06 29 tmp]$ echo $MYIP [student@station5-237 15:06 30 tmp]$ source ~/myenv.sh [student@station5-237 15:06 31 tmp]$ echo $MYIP 172.16.5.237
[root@station5-237 ~]# cd /usr/local/bin/ [root@station5-237 bin]# vim listcmd.sh #!/bin/bash echo -e "\nThis shell script will list your command's full path name and permissions.\n" read -p 'Please input a command name: ' cmd cmdpath=$( which ${cmd} ) ls -l ${cmdpath} getfacl ${cmdpath} exit 0 [root@station5-237 bin]# chmod a+x listcmd.sh
[root@station5-237 bin]# cd /usr/local/bin [root@station5-237 bin]# cp listcmd.sh listcmd2.sh [root@station5-237 bin]# vim listcmd2.sh #!/bin/bash echo -e "\nThis shell script will list your command's full path name and permissions.\n" cmd=${1} #read -p 'Please input a command name: ' cmd cmdpath=$( which ${cmd} ) ls -l ${cmdpath} getfacl ${cmdpath} exit 0
[student@station5-237 16:07 14 ~]$ vim ~/bin/mypi2.sh #!/bin/bash # Program: # User input a scale number to calculate pi number. # History: # 2015/07/16 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH num=${1} echo -e "This program will calculate pi value. \n" echo -e "You should input a float number to calculate pi value.\n" # 判斷是否有非為數字的字元,若有,則會出現,若無則是空白 checking=$( echo ${num} | grep '[^0-9]' ) if [ "${num}" == "" -o "${checking}" != "" ]; then num=20 fi if [ "${num}" -le 10 ]; then num=10 elif [ "${num}" -ge 10000 ]; then num=10000 fi echo "Use scale number: ${num}" echo -e "Starting calculate pi value. Be patient." time echo "scale=${num}; 4*a(1)" | bc -lq # 開始測試執行看看 [student@station5-237 16:10 16 ~]$ mypi2.sh This program will calculate pi value. You should input a float number to calculate pi value. Use scale number: 20 <==因為外帶參數為空白,所以預設 20 ! Starting calculate pi value. Be patient. 3.14159265358979323844 [student@station5-237 16:10 17 ~]$ mypi2.sh 1u9u94 This program will calculate pi value. You should input a float number to calculate pi value. Use scale number: 20 <==因為外帶參數有非數值,所以預設 20 Starting calculate pi value. Be patient. 3.14159265358979323844 [student@station5-237 16:10 18 ~]$ mypi2.sh 50 This program will calculate pi value. You should input a float number to calculate pi value. Use scale number: 50 <==正常數值的使用 Starting calculate pi value. Be patient. 3.14159265358979323846264338327950288419716939937508
[student@station5-237 16:35 25 ~]$ cp ~/bin/mypi2.sh ~/bin/mypi4.sh [student@station5-237 16:37 26 ~]$ vim ~/bin/mypi4.sh #!/bin/bash # Program: # User input a scale number to calculate pi number. # History: # 2015/07/16 VBird First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH num=${1} echo -e "This program will calculate pi value. \n" echo -e "You should input a float number to calculate pi value.\n" case ${num} in "20") echo "Your input is 20" ;; "100") echo "Your input is 100" ;; "1000") echo "Your input is 1000" ;; *) echo "Usage: ${0} 20|100|1000" exit 0 ;; esac echo -e "Starting calculate pi value. Be patient." time echo "scale=${num}; 4*a(1)" | bc -lq比較重要的就只有那一行『 Usage 』,因為那一行就是要顯示『使用者應該要怎麼下達指令』的重要參考項目囉!
# a. 先確認行數,可以使用 wc 來處理即可 [student@station5-237 20:51 50 ~]$ wc /etc/man_db.conf 131 721 5165 /etc/man_db.conf [student@station5-237 21:01 51 ~]$ wc -l /etc/man_db.conf 131 /etc/man_db.conf # 得到為數不少的 131 行! # b. 去除不要的,再加以計算 [student@station5-237 21:02 52 ~]$ egrep -v '^$|^#' /etc/man_db.conf MANDATORY_MANPATH /usr/man MANDATORY_MANPATH /usr/share/man MANDATORY_MANPATH /usr/local/share/man MANPATH_MAP /bin /usr/share/man MANPATH_MAP /usr/bin /usr/share/man MANPATH_MAP /sbin /usr/share/man ...... [student@station5-237 21:02 53 ~]$ egrep -v '^$|^#' /etc/man_db.conf | wc -l 23 # c. 只列出某幾行 [student@station5-237 21:02 54 ~]$ egrep -v '^$|^#' /etc/man_db.conf | grep '^MANPATH_MAP' MANPATH_MAP /bin /usr/share/man MANPATH_MAP /usr/bin /usr/share/man MANPATH_MAP /sbin /usr/share/man MANPATH_MAP /usr/sbin /usr/share/man ......
# a. 一樣,取出設定檔內主要的設定,不要的部份不顯示 [student@station5-237 21:03 55 ~]$ egrep -v '^$|^#' /etc/rsyslog.conf module(load="imuxsock" # provides support for local system logging (e.g. via logger command) SysSock.Use="off") # Turn off message reception via local log socket; # local messages are retrieved through imjournal now. module(load="imjournal" # provides access to the systemd journal StateFile="imjournal.state") # File to store the position in the journal global(workDirectory="/var/lib/rsyslog") module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") include(file="/etc/rsyslog.d/*.conf" mode="optional") *.info;mail.none;authpriv.none;cron.none /var/log/messages authpriv.* /var/log/secure mail.* -/var/log/maillog cron.* /var/log/cron *.emerg :omusrmsg:* uucp,news.crit /var/log/spooler local7.* /var/log/boot.log # b. 再將不要的 # 註解部份刪除 [student@station5-237 21:06 56 ~]$ egrep -v '^$|^#' /etc/rsyslog.conf | sed 's/#.*$//g' module(load="imuxsock" SysSock.Use="off") module(load="imjournal" StateFile="imjournal.state") global(workDirectory="/var/lib/rsyslog") module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") include(file="/etc/rsyslog.d/*.conf" mode="optional") *.info;mail.none;authpriv.none;cron.none /var/log/messages authpriv.* /var/log/secure mail.* -/var/log/maillog cron.* /var/log/cron *.emerg :omusrmsg:* uucp,news.crit /var/log/spooler local7.* /var/log/boot.log # c. 找出兩個關鍵字 [student@station5-237 21:10 57 ~]$ egrep -n 'tcp|udp' /etc/rsyslog.conf 18:# for parameters see http://www.rsyslog.com/doc/imudp.html 19:#module(load="imudp") # needs to be done just once 20:#input(type="imudp" port="514") 23:# for parameters see http://www.rsyslog.com/doc/imtcp.html 24:#module(load="imtcp") # needs to be done just once 25:#input(type="imtcp" port="514") 79:#Target="remote_host" Port="XXX" Protocol="tcp") # d. 所以,可能就是 514 號啊!
# a. 先建立這隻腳本看看! [root@station5-237 bin]# vim /usr/local/bin/lm #!/bin/bash files=${@} ls -al ${@} | more [root@station5-237 bin]# chmod a+x /usr/local/bin/lm # b. 測試執行看看結果! [root@station5-237 bin]# lm /etc 總計 1384 drwxr-xr-x. 135 root root 8192 5月 2 15:44 . dr-xr-xr-x. 17 root root 261 4月 15 10:48 .. -rw-r--r--. 1 root root 44 3月 4 00:06 adjtime -rw-r--r--. 1 root root 1518 9月 10 2018 aliases drwxr-xr-x. 3 root root 65 2月 26 09:06 alsa drwxr-xr-x. 2 root root 4096 2月 26 09:08 alternatives -rw-r--r--. 1 root root 541 11月 9 00:47 anacrontab -rw-r--r--. 1 root root 55 11月 9 00:21 asound.conf -rw-r--r--. 1 root root 1 5月 11 2019 at.deny drwxr-x---. 4 root root 100 2月 26 09:15 audit drwxr-xr-x. 3 root root 228 2月 26 09:10 authselect drwxr-xr-x. 4 root root 71 2月 26 09:06 avahi drwxr-xr-x. 2 root root 136 2月 26 09:06 bash_completion.d -rw-r--r--. 1 root root 3001 9月 10 2018 bashrc -rw-r--r--. 1 root root 429 11月 9 04:35 bindresvport.blacklist drwxr-xr-x. 2 root root 6 11月 9 07:14 binfmt.d drwxr-xr-x. 2 root root 23 2月 26 09:03 bluetooth -rw-r-----. 1 root brlapi 33 2月 26 09:05 brlapi.key --更多-- # 接下來可以按下 [enter] / [space] / q 來決定翻頁的情況!