对”命令“操作的命令
????乍一看這個標題有點拗口,什么叫對“命令”操作的命令呢?其實從字面上就可以理解我要表達的意思,今天這篇博客寫的還是命令,但是這些命令有點不一樣,不一樣的地方在于這些命令操作的對象就是“命令”。
1,? type,type可以查看命令是屬于shell的內建命令還是外部命令
[root@node1?~]#?type?cd cd?is?a?shell?builtin [root@node1?~]#?type?ls ls?is?aliased?to?`ls?--color=auto' [root@node1?~]#?type?cd cd?is?a?shell?builtin [root@node1?~]#?type?vi vi?is?/bin/vitype后可以跟-a選項
[root@node1?/]#?type?-a?pwd pwd?is?a?shell?builtin pwd?is?/bin/pwd2,? whereis,在linux中一切皆文件,命令也不例外,whereis就可以幫你定位這些“命令”文件的為位置和幫助文檔
[root@node1~]#?whereis?vi vi:/bin/vi?/usr/share/man/man1/vi.1.gz?/usr/share/man/man1p/vi.1p.gz [root@node1~]#?whereis?cd cd:/usr/share/man/man1/cd.1.gz?/usr/share/man/man1p/cd.1p.gz [root@node1~]#?whereis?ls ls:?/bin/ls/usr/share/man/man1/ls.1.gz?/usr/share/man/man1p/ls.1p.gz3,? which,which的原意是哪一個,而linux中which可以列出命令的別名,命令的位置,以及命令相關的man文檔的位置
root@node1?~]#?which?ls alias?ls='ls?--color=auto' /bin/ls [root@node1?~]#?which?cd /usr/bin/which:?no?cd?in(/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin) [root@node1?~]#?which?vi /bin/vi [root@node1?~]#?whereis?vi vi:?/bin/vi?/usr/share/man/man1/vi.1.gz/usr/share/man/man1p/vi.1p.gz4,? alias,給命令制定一個別名,用戶可以通過alias對命令做一些貼近個人習慣的定制
[root@node1~]#?alias?la='ls?-al' [root@node1~]#?la total59428 dr-xr-x---.22?root?root?????4096?Jun??6?14:15?. dr-xr-xr-x.25?root?root?????4096?Jun??7?08:34?.. drwxr-xr-x.??2?root?root?????4096?May?6?08:44?.abrt -rw-------.??1?root?root?????1592?May?7?04:40?anaconda-ks.cfg -rw-------.??1?root?root??????534?Jun?6?14:16?.bash_history -rw-r--r--.??1?root?root???????18?May?20??2009?.bash_logout -rw-r--r--.??1?root?root??????176?May?20??2009?.bash_profile -rw-r--r--.??1?root?root??????176?Sep?23??2004?.bashrc drwxr-xr-x.??2?root?root?????4096?May?6?08:46?.cache drwx------.??4?root?root?????4096?May?6?08:44?.config -rw-r--r--.??1?root?root??????100?Sep?23??2004?.cshrc drwx------.??3?root?root?????4096?May?6?08:44?.dbus drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Desktop drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Documents drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Downloads -rw-------.??1?root?root???????16?May?6?08:44?.esd_auth drwx------.??4?root?root????4096?May??6?20:53?.gconf drwx------.??2?root?root?????4096?May?6?20:55?.gconfd drwx------.??5?root?root?????4096?May?6?08:44?.gnome2 -rw-r--r--.??1?root?root??????107?May?6?20:53?.gtk-bookmarks drwx------.??2?root?root?????4096?May?6?08:44?.gvfs -rw-------.??1?root?root??????620?May?6?20:53?.ICEauthority -rw-r--r--.??1?root?root??????623?May?6?20:53?.imsettings.log -rw-r--r--.??1?root?root???48228?May??7?04:40?install.log -rw-r--r--.??1?root?root?????9360?May?7?04:37?install.log.syslog drwxr-xr-x.??3?root?root?????4096?May?6?08:44?.local drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Music drwxr-xr-x.??2?root?root?????4096?May?6?08:44?.nautilus drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Pictures drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Public drwx------.??2?root?root?????4096?May?6?08:44?.pulse -rw-------.??1?root?root??????256?May?6?08:44?.pulse-cookie -rw-------.??1?root?root??????218?May?6?20:55?.recently-used.xbel -rw-r--r--.??1?root?root??????129?Dec?4??2004?.tcshrc drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Templates drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Videos -rw-------.??1?root?root?????3992?Jun?6?14:15?.viminfo -r--r--r--.??1?root?root?60638465?May??6?08:46?VMwareTools-9.6.1-1378637.tar.gz drwxr-xr-x.??7?root?root?????4096?Oct?18??2013?vmware-tools-distrib取消別名
[root@node1~]#?unalias?la [root@node1~]#?la -bash:la:?command?not?foundalias對命令設置別名之后僅僅在當前shell中生效,如果想保存這一設定的話,就必須將其保存在配置文件中
[root@node1~]#?echo?"?alias?la='ls?-al'"?>>?/etc/bashrc此操作會對全局生效,如果僅僅相對某個用戶生效可以
[root@node1~]#?echo?"?alias?la='ls?-al'"?>>?~/.bashrc4,whatis,有了whereis 和which,有沒有what,why,when呢,答案是沒有,但是有個whatis,而且這個whatis可能和你們想的不一樣
[root@node1?~]#whatis?cd cd???????????????????(1p)??-?change?the?working?directory cd?[builtins]????????(1)??-?bash?built-in?commands,?see?bash(1) cd-drive?????????????(1)??-?manual?page?for?cd-drive cd-info??????????????(1)??-?manual?page?for?cd-info cd-paranoia?9.8?(Paranoia?release?III?via?libcdio)?[cd-paranoia](1)??-?an?audio?CD?reading?utility?whichincludes?extra?data?verification?features cd-read??????????????(1)??-?manual?page?for?cd-read?version?0.81原來這個是查看這個命令在man文檔中1-9這九個章節中的哪個章節的
[root@node1?~]#?whatis?passwd passwd???????????????(1)??-?update?user's?authentication?tokens passwd???????????????(5)??-?password?file passwd?[sslpasswd]??(1ssl)??-?compute?password?hashes有的時候你可能會遇到這樣的情況
[root@node1?~]#?whatis?passwd passwd:?nothing?appropriate你只需要makewhatis一下就可以了
[root@node1?~]#?makewhatis?
? ? 最后再說一說命令的搜索路徑,大家都知道當在shell中輸入命令之后,shell就會在文件系統中尋找這個命令對應的文件,而對于這個尋找的路徑大家應該也很熟悉了,就是$PATH這一環境變量
[root@node1?~]#?echo?$PATH /usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin? ? 但是實際上shell在搜索$PATH之前會先去別的地方搜索,而$PATH其實是最后搜索的地方,shell真正的搜索路徑應該是
1,? alias
2,? hashtable
3,? function
4,? builtins
5,? $PATH
1. alias,如果用戶定義了別名的話,那么別名會先被搜索到并且執行,不論是別名是否被保存至配置文件都是如此
[root@node1?~]#?alias?la='ls?-al' [root@node1?~]#?la total?59428 dr-xr-x---.?22?root?root????4096?Jun??6?14:15?. dr-xr-xr-x.?25?root?root????4096?Jun??7?08:34?.. drwxr-xr-x.??2?root?root?????4096?May?6?08:44?.abrt -rw-------.??1?root?root?????1592?May?7?04:40?anaconda-ks.cfg -rw-------.??1?root?root??????534?Jun?6?14:16?.bash_history -rw-r--r--.??1?root?root???????18?May?20??2009?.bash_logout -rw-r--r--.??1?root?root??????176?May?20??2009?.bash_profile -rw-r--r--.??1?root?root??????176?Sep?23??2004?.bashrc drwxr-xr-x.??2?root?root?????4096?May?6?08:46?.cache drwx------.??4?root?root?????4096?May?6?08:44?.config -rw-r--r--.??1?root?root??????100?Sep?23??2004?.cshrc drwx------.??3?root?root?????4096?May?6?08:44?.dbus drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Desktop drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Documents drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Downloads -rw-------.??1?root?root???????16?May?6?08:44?.esd_auth drwx------.??4?root?root?????4096?May?6?20:53?.gconf drwx------.??2?root?root?????4096?May?6?20:55?.gconfd drwx------.??5?root?root?????4096?May?6?08:44?.gnome2 -rw-r--r--.??1?root?root??????107?May?6?20:53?.gtk-bookmarks drwx------.??2?root?root?????4096?May?6?08:44?.gvfs -rw-------.??1?root?root??????620?May?6?20:53?.ICEauthority -rw-r--r--.??1?root?root??????623?May?6?20:53?.imsettings.log -rw-r--r--.??1?root?root????48228?May?7?04:40?install.log -rw-r--r--.??1?root?root?????9360?May?7?04:37?install.log.syslog drwxr-xr-x.??3?root?root?????4096?May?6?08:44?.local drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Music drwxr-xr-x.??2?root?root?????4096?May?6?08:44?.nautilus drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Pictures drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Public drwx------.??2?root?root?????4096?May?6?08:44?.pulse -rw-------.??1?root?root??????256?May?6?08:44?.pulse-cookie -rw-------.??1?root?root??????218?May?6?20:55?.recently-used.xbel -rw-r--r--.??1?root?root??????129?Dec?4??2004?.tcshrc drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Templates drwxr-xr-x.??2?root?root?????4096?May?6?08:44?Videos -rw-------.??1?root?root?????3992?Jun?6?14:15?.viminfo -r--r--r--.??1?root?root60638465?May??6?08:46VMwareTools-9.6.1-1378637.tar.gz drwxr-xr-x.??7?root?root?????4096?Oct?18??2013?vmware-tools-distri2. hashtable
[root@node1?~]#?hash hits??command1?????????/bin/date1?????????/usr/bin/vim1?????????/bin/lshashtable其實是用戶在執行命令之后shell將搜索的結果的緩存,這樣可以提高搜索的效率和速度,但是hash是不會記錄shell的內部命令,functions以及定義路徑的別名的
那什么叫定義了路徑的別名呢
[root@node1?~]#?hash?-r [root@node1?~]#?hash hash:?hash?table?empty清空hash表
[root@node1?~]#?alias?ls='ls' [root?@node1?~]#?ls anaconda-ks.cfg??Desktop??Documents?Downloads??install.log??install.log.syslog??Music?Pictures??Public??Templates??Videos??VMwareTools-9.6.1-1378637.tar.gz??vmware-tools-distrib [root@node1?~]#?hash hits??command1?????????/bin/ls [root@node1?~]#?unalias?ls [root@node1?~]#?hash hits??command 1?????????????/bin/ls為ls指定了沒有指定路徑的別名后,hash結果為1
[root@node1?~]#?alias?ls='/bin/ls' [root@node1?~]#?ls anaconda-ks.cfg??Desktop?Documents??Downloads?????????install.log??install.log.syslog??Music?Pictures??Public?Templates??Videos??VMwareTools-9.6.1-1378637.tar.gz??vmware-tools-distrib [root@node1?~]#?hash hits??command 1?????????????/bin/ls在取消了第一次別名之后再為ls指定一個指定了路徑的別名之后,hash結果仍然為一并未發生變化,證明這一次指定路徑的別名并沒有被緩存只hashtable中
[root@node1?~]#?unalias?ls [root@node1?~]#?ls anaconda-ks.cfg??Desktop?Documents??Downloads?install.log??install.log.syslog??Music?Pictures??Public??Templates??Videos??VMwareTools-9.6.1-1378637.tar.gz??vmware-tools-distrib [root@node1?~]#?hash hits??command2?????????/bin/ls [root@node1?~]#取消別名再執行ls之后,hashtable中變為2
3,function
function指的是一些用戶自定義的函數,且此函數名和shell的命令名稱相同,此時shell會優先執行用戶自定義的函數
[root@node1?~]#?function?cd?{?echo"just?for?a?test";} [root@node1?~]#?cd just?for?a?test在用戶自定了cd這函數之后再輸入cd就是執行剛才用戶定義的函數而不是shell命令cd了
要是想執行原本在shell中cd命令改怎么辦呢只需要在cd前加上builtin就可以
[root@node1?~]#?builtin?cd?/ [root@node1?/]#type -a cd可以看到以下結果
[root@node1?/]#?type?-a?cd cd?is?a?function cd?() {echo?"just?for?a?test" } cd?is?a?shell?builtincd不僅是一個shell內建的命令,而且是用戶定義的函數
4,builtins,接下來是檢索的是shell的內建的命令
5,? 最后才是$PATH中定義的搜索路徑
[root@node1?/]#?echo?$PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin即以下幾個路徑
/usr/lib64/qt-3.3/bin /usr/local/sbin /usr/local/bin /sbin:/bin /usr/sbin /usr/bin /root/bin轉載于:https://blog.51cto.com/viperstars/1423293
總結
以上是生活随笔為你收集整理的对”命令“操作的命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博士期间要注意的几个问题
- 下一篇: ckedit 3.0 配置(一)