bash之命令執行的順序
一般來說,在shell提示符號下輸入的指令再按下enter後
系統會依以下的順序來找到命令再去執行,執行完成即結束
1.以絕對(相對)路徑表示的命令
e.g.,
$ /bin/ls
2.以alias別名裡有設定的名稱
e.g.,
$ alias
alias mv='mv -i'
alias rm='rm -i'
alias cp='cp -i'
alias telnet='/usr/bin/telnet-ssl -8'
如果要暫時脫逸(escape)有設alias的指令
可以在指令前加一個backslash「\」
$ \rm /tmp/file
執行時就不會有「-i」詢問你是否確定的效果.
3.在function裡有定義的函數名稱
e.g.,
(定義一個function,名稱CatPasswd)
$ CatPasswd (){ cat /etc/passwd; }
$ CatPasswd | head -n1
root:x:0:0:root:/root:/bin/bash
4.在shell裡面的內建命令
e.g.,
$ cd
$ exit
$ type
(可以用type這個builtin command來判斷)
$ type cd
cd is a shell builtin
$ type exit
exit is a shell builtin
$ type echo
echo is a shell builtin
(其實echo指令有內建命令和外部命令兩種)
(要執行外部命令的echo要用明確路徑/bin/echo的方式)
5.PATH環境變數裡指定的路徑
就是你打echo $PATH命令所顯示出來的那些路徑啦
當你打的命令在上面4個都找不到的情況下,就會往這些path路徑裡找
最後,如果bash都找不到的話,就會跟你說command not found
文章標籤
全站熱搜

不過我今天在看 《bash shell入門 第三版》 ISBN: 986-7794-59-1 這本oreilly的書第306頁時,裡面提到有些shell內建變數 的執行順序會優於function 像break、exit、exec、continue等等 都會優先於函式,但是呢,經過我的測試, 先定義一些這些名稱的function再去執行,還是function優先啊 奇怪,難不成我被oreilly擺了一道?
*****
*****
這麼優的部落格一定要持續下去!
很用心的blog,推推哦
*****
*****
*****
*****
*****
好文章,希望能一直看到您的PO文
http://ericbbs.blogspot.com/2007/10/expect.html ----- Expect 教程中文版 本文出自: 作者: 葫蘆娃 翻譯 (2001-09-12 10:00:00) [版權聲明] Copyright(c) 1999 本教程由*葫蘆娃*翻譯,並做了適當的修改,可以自由的用非商業目的。 但Redistribution時必須拷貝本[版權聲明]。 [BUG] 有不少部分,翻譯的時候不能作到“信,達”。當然了,任何時候都沒有做到“雅”,希望各位諒解。 [原] Don Libes: National Institute of Standards and Technology libes@cme.nist.gov
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
http://kezeodsnx.pixnet.net/blog/post/24948849 -- [轉載請註明出處] http://kezeodsnx.pixnet.net/blog 作者: kezeodsnx 每個cmd都需要stdin, stdout, stderr,正常情況下,cmd從stdin (keyboard)讀資料,輸入結果到stdout (monitor),輸出錯誤到stderr (monitor)。如果這3個file descriptor有問題,就會出現以下情形: 先把stdout 關掉,再下ls: user@user-ubuntu:~$ exec >&- user@user-ubuntu:~$ ls ls: write error: Bad file descriptor xterm 也在initialize時,開啟了3個terminal device(/dev/pts/n),之後的bash均繼承xterm的file descriptor。 Shell 有10個file descriptor,即0~9。其中0, 1, 2就是大家都知道的stdin, stdout, stderr。剩下的比較少看到有人用,我自己是沒用過。通常把0, 1, 2導來導去就很複雜了。3~9通常是給opening file使用,一種用法為在一連串的I/O redirect後,為了復原,可在redirect之前先複製到3~9其中一個,將來再複製回來。 與I/O redirect相關的cmd有pipe (|), tee, exec。 pipe蠻常用,是把目前的stdin 導到下一個cmd的stdin,例如ps aux |grep init,將ps的結果(stdout)當成grep的stdin。 tee是將stdout複製(到檔案),通常我的用法是make | tee /home/user/build.log。 exec蠻特別,通常的理解是取代,但當其操作的對像是file descriptor時,不會有取代發生。 2個rule: 1. < 是input,> 是output 2.<預設值是0,>預設值是1。即<與0<同,>與 1>同。下面的例子若有疑問,可試著代入展開。 熱身: >&n: 即1>&n,也就是將stdout複制給 file descriptor n (使用dup(2)) <&n: 即0<&n,將file descriptor n 複製到stdin <&-: 即0<&-,關閉stdin >&-: 即1>&-,關閉stdout 了解之後,可看實例。如果你是使用ubuntu,請看/etc/rc6.d/S40umountfs,其中有段如下 do_stop () { exec 9<&0 &1: 把stderr複制到stdout。乍看之下,好像沒什麼特別的,因為stderr和stdout都是monitor。但當stdout是其他file 或file descriptor時,效果就不同了 例如: #stdout無輸出,因此1>log後,log為市。stderr還是由monitor輸出err user@user-ubuntu:~$ bad_cmd > log bash: bad_cmd: command not found user@user-ubuntu:~$ cat log #將stdout 導到log, 再將stderr複製到stdout,因此err都導到log裡 user@user-ubuntu:~$ bad_cmd > log 2>&1 user@user-ubuntu:~$ cat log bash: bad_cmd: command not found
黃郁熙 http://www.study-area.org/cyril/scripts/scripts/
EXPECT教程中文版 http://bbs.mychat.to/reads.php?tid=92053
http://www.study-area.org/cyril/opentools/opentools/book1.html
http://www.study-area.org/cyril/scripts/scripts/node6.html ----- 第一種方法是在shell script 文字檔前指出shell scripts解讀的程式在那(也就是 我們的shell)然後把文字檔的執行權限打開,照一般執行可執行檔方式執行或者叫 一個shell來解釋文字檔test.sh。 $ test.sh $ /bin/sh test.sh $ ( . test.sh; ) $ exec test.sh 第二種方法是用命令''.''或者source執行。 $ . test.sh $ source test.sh $ { test.sh; } $ eval '. test.sh' 差別在於一些設定只有在這個shell下的才算數,而喚起另一個shell就是另一個 不相干的世界, 也就是用第一種方法執行的script中變數的設定,不會影響到原來的shell變數。 這個相當重要。ksh沒有source這個命令,所以最好不要用source。 中括號( )表示用另一個subshell大括號,{ }表示用目前shell。例如 $ ( VAR='testvar'; ) $ echo $VAR $ { VAR='testvar'; } $ echo $VAR testvar
Linux指令練習-網頁版 http://bellard.org/jslinux/
在命令的前面加上反斜線「\」即可使它在alias裡的設定失效. $\ls $\rm 等等...
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_02.html
$ typeset -i i=8#100 $ echo $i 8#100 $ i=50 $ echo $i 8#62 $ (( i = 16#a5 + 16#120 )) $ echo $i 8#705 Because the first value assigned to i in this example is an octal number (8#100), all further substitutions of i will be in octal. When the base 10 value of 50 is next assigned to i and then i is subsequently displayed, we get the value 8#62, which is the octal equivalent of 50 in base 10. In the preceding example, the ((...)) construct is used to add together the two hexadecimal values a5 and 120. The result is then displayed, once again in octal. Bash uses both the base#number syntax for arbitrary bases and the C language syntax for octal and hexadecimal numbers—octal numbers are preceded by 0 (zero), and hexadecimal numbers are preceded by 0x: $ typeset -i i=0100 $ echo $i 64 $ i=0x80 $ echo $i 128 $ i=2#1101001 $ echo $i 105 $ (( i = 16#a5 + 16#120 )) $ echo $i 453
原來在linux下還有一種叫hash table的東西.. 用type command命令會出現.. -- http://www.cyberciti.biz/tips/an-example-how-shell-understand-which-program-to-run-part-ii.html -- 1.Before a command is executed REDIRECTION is done. Then following sequence used by SHELL 2.ALIASES 3.Parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable 4.Shell FUNCTION 5.BUILTIN command 6.HASH tables 7.PATH variable 8.If everything fails, you see command not found error message.
補充一下 第二順位的alias 當你的指令搭配xargs使用時,也會暫時失去alias的設定 譬如說,你要使用xargs刪除目前目錄下的jpg檔 ls -1 *.jpg | xargs rm 這時候會直接刪除,不會詢問你. 這是最近在使用xargs搭配rm時遇到的問題,還好沒有發生指令打錯誤砍的情況. 當然這工作你可以直接rm *.jpg啦,不過這只是舉例XD
補充:在alias跟function之間,應該要多出一個keyword,像是 if , for ...