introduce to linux command line
DESCRIPTION
2015/01/07 修正ctrl + ins複製錯誤,應該是shift + ins,感謝Jack Chen告知 2014/08/05 修正control + z描述錯誤,感謝En-Hao Hu的告知TRANSCRIPT
參考資料
2013 8 月份 SA@Tainan 從 shell 開始 8/10教材 https://sites.google.com/a/study-area.org/sa-
activity/home/2013-08-tn 鳥哥的 Linux 私房菜
http://linux.vbird.org/ man bash
wen:/tmp/$ echo -n test1 test2
test1 test2wen:/tmp/$
第一提示符號(PS1)
Command Option 或Parameter
Argument: 要處理的對象
Command line 的組成
wen:/tmp/$ echo "
> test1
> test2
> "
test1
test2
第一提示符號(PS1) Command
Argument: 要處理的對象
Argument: 要處理的對象
Option 或Parameter 可以是空的
第二提示符號(PS2)
Command line 的組成
-- :後面的都是 Argument
$ touch -- -al
$ ls -al -- -al
-rw-rw-r-- 1 test test 0 Sep 12 15:31 -al
$ rm -al
rm: invalid option -- 'a'
Try `rm ./-al' to remove the file `-al'.
Try `rm --help' for more information.
$ rm -- -al
$$ command
command
fork() + exec() exit() 或是 kill
Foreground mode
Command 可以取得鍵盤輸入的資料
Shell 須要等 command結束才能執行下一個
command
$$ command &
command
fork() + exec()
Background mode
鍵盤輸入的資料傳給 shell而不是 command
Shell 不須等 command結束就能執行下一個
command
撒尿牛丸 (2)
Shell
鍵盤傳送 Process 對象
Command Shell
執行 command &CTRL + Z 暫停
Command
Shell
執行 fg 把 command轉回 foreground
Command 遺言
正常結束 0
有錯誤 非 0
用 $? 取得
$ ls testtest
$ echo $?0
$ ls --badls: unrecognized option '--bad'Try `ls --help' for more information.
$ echo $?2
Quote
Hard Quoting: 成對的 ' ,第一個 ' 右邊所有的特殊字元都會視為一般字元直到第二個 ' 出現
Soft Quoting: 成對的 " ,第一個 " 右邊大部份的特殊字元都會視為一般字元直到第二個 " 出現 例外仍然有特殊意義的字元 $ ` \ ! ( 有 enable history 功能 )
跳脫字元 \
File Descriptor
簡稱 fd Linux command 開檔案都會有對應的數字代號
/proc/[process id]/fd 關鍵字 :Process, process ID, file descriptor
每個 command 初始都會有 3 個開啟的檔案 0: stdin 1: stdout 2: stderr
Redirect
輸出 Redirect Fd> 檔案
預設 1> ls > file ls -no_such_option 2> error_file
複製輸出 fd Fd1>&Fd2
make 2>&1 | tee build.log
Shortcuts
CTRL + R: 搜尋打過的指令 CTRL + A: 切到 command 第一個字元 CTRL + E: 切到 command 最後一個字元 CTRL + Z: 暫停目前 command CTRL + S: 停止螢幕捲動 CTRL + Q: 恢復螢幕捲動
Gnome-terminal shortcuts
CTRL+SHIFT+C: COPY CTRL+SHIFT+V: PASTE CTRL+SHIFT+T: New TAB SHIFT+INS: PASTE 滑鼠選的字 CTRL+SHIFT+F: 搜尋
環境變數
所有的 process在 fork 都會繼承的變數 在單一 shell 下更改環境變數後,該 shell之後
fork 的 process環境變數都會繼承改變的環境變數
範例 $PATH $HOME
可以用 env檢查環境變數
Regular expression
一個描述字串 pattern 的語法,如 Lib 開頭 .a 結尾的字串 變數 my_var被 assgin 的字串
注意 = 前後可能有 0 到多個空白 ...
範例 grep
找出檔案裏面變數 my_var被 assign 的行數 grep * -r my_var.*=
找出目錄下面所有 lib 開頭 .a 結尾的檔案 find | grep ^./lib.*\\.a$
請打下面的指令
echo hello world (enter)
滑鼠游標到顯示的 hello world那行中的 world
Double click ( 左鍵 )
Click ( 左鍵 )
echo 後 click 中鍵echo 後按 shift + ins
請打下面的指令
echo hello world (enter)
滑鼠游標到顯示的 hello world那行中的 world
Double click ( 左鍵 )
Ctrl + shift + c
echo 後 Ctrl + shift + v 再按 enter
ctrl + l
ctrl + shift + t
變數 , IFS 以及 quote
VAR1=my test
echo $VAR1
VAR1="my test"
echo $VAR1
for i in $VAR1; do echo $i ; done
Job control
while [ true ] ; do echo test ; sleep 5 ; done & Enter
vi (enter)
Ctrl + z
jobs (enter)
fg 1 (enter)
Ctrl + c
fg 2 (enter)
Quit vi
Return value and conditional
ls (enter)
if [ $? = 0 ] ; then echo ok; else echo bad; fi (enter)
ls --bad (enter)
if [ $? = 0 ] ; then echo ok; else echo bad; fi (enter)
Redirect and fd
rm /tmp/dct (enter)
ls / --bad > /tmp/dct (enter)
cat /tmp/dct (enter)
ls / --bad 2> /tmp/dct (enter)
cat /tmp/dct (enter)
pipeline
ls /lib | grep lib.*\\.so$ (enter)
rm -f /tmp/dct (enter)
ls / | tee /tmp/dct (enter)
cat /tmp/dct (enter)