单纯提供一个相对的解答,并不是标准答案!
单纯就是个解答的参考,写完之后再来这边查查看答案跟你想的一样不一样!?
[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 4096 0 4096 0% /dev tmpfs 906728 640896 265832 71% /dev/shm tmpfs 362692 8292 354400 3% /run /dev/mapper/rocky-root 10475520 4348476 6127044 42% / /dev/vda2 1992552 356272 1515040 20% /boot /dev/mapper/rocky-home 3135488 155116 2980372 5% /home tmpfs 181344 52 181292 1% /run/user/42 tmpfs 181344 36 181308 1% /run/user/0 [student@station5-237 16:14 18 ~]$ df | grep tmpfs devtmpfs 4096 0 4096 0% /dev tmpfs 906728 640896 265832 71% /dev/shm tmpfs 362692 8292 354400 3% /run tmpfs 181344 52 181292 1% /run/user/42 tmpfs 181344 36 181308 1% /run/user/0 [student@station5-237 16:14 19 ~]$ df | grep -v tmpfs 文件系统 1K-区块 已用 可用 已用% 挂载点 /dev/mapper/rocky-root 10475520 4348476 6127044 42% / /dev/vda2 1992552 356272 1515040 20% /boot /dev/mapper/rocky-home 3135488 155116 2980372 5% /home最后可以成功的找到非内存内的实体设备的容量信息!这样比较好查找实际文件系统的资源。
[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/rocky-root 10475520 4348476 6127044 42% / /dev/vda2 1992552 356272 1515040 20% /boot /dev/mapper/rocky-home 3135488 155116 2980372 5% /home
# 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/lvm/lvm.conf /etc/lvm/lvmlocal.conf /etc/resolv.conf /etc/dnf/dnf.conf ...... # K. 承上,同时含有大写或数字 [student@station5-237 17:27 39 ~]$ find /etc | grep '\.conf$' | grep '[[:upper:][:digit:]]' /etc/dnf/protected.d/grub2-tools-minimal.conf /etc/dnf/protected.d/grub2-pc.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 这个指令也终于能用了! 总用量 40 drwxr-xr-x. 2 student student 47 5月 1 09:57 bin -rw-r-----. 1 student student 540 2月 22 10:20 chrony.keys drwxr-xr-x. 2 student student 18 2月 26 16:01 cmd ......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 啰! [student@station5-237 14:45 19 bin]$ source gototmp.sh /tmp [student@station5-237 14:45 19 tmp]$ pwd /tmp # 如果是 source 来运行,那么该脚本内容会导入到目前的 bash 底下,所以就可以切换目录成功!也就是说,运行 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 132 726 5235 /etc/man_db.conf [student@station5-237 21:01 51 ~]$ wc -l /etc/man_db.conf 132 /etc/man_db.conf # 得到为数不少的 132 行! # 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 24 # 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 global(workDirectory="/var/lib/rsyslog") module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") include(file="/etc/rsyslog.d/*.conf" mode="optional") 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 *.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' global(workDirectory="/var/lib/rsyslog") module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") include(file="/etc/rsyslog.d/*.conf" mode="optional") module(load="imuxsock" SysSock.Use="off") module(load="imjournal" StateFile="imjournal.state") *.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 29:# for parameters see http://www.rsyslog.com/doc/imudp.html 30:#module(load="imudp") # needs to be done just once 31:#input(type="imudp" port="514") 34:# for parameters see http://www.rsyslog.com/doc/imtcp.html 35:#module(load="imtcp") # needs to be done just once 36:#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 来决定翻页的情况!