1. for loop
for var in item1 item2 ... itemN
do
donelinux
for var in list-of-values
do
doneshell
for var in file1 file2 ... fileN
do
donebash
for var in $filename
do
doneoop
for var in $(command-in-linux)
do
done.net
for var in ${arrayname[@]}
do
doneorm
for ((exp1;exp2;exp3))
do
done進程
2. command substitution
a shell command output stored in a string or a variable
$(...)
`command`ip
3. e.g. --- print out a chess board
#!/bin/bash get
###print out a chess board
for ((i=1; i<=8; i++)); do
for ((j=1; j<=8; j++)); do
total=$(($i+$j))
tmp=$((total%2))
if [ $tmp -eq 0 ]; then
echo -e -n "\033[47m \033[0m"
else
echo -e -n "\033[40m \033[0m"
fi
done
echo ""
doneinput
4. while loop
while
do
done
eg
while [ $n -le 5 ]
do
...
done
while (($n <= 5))
do
...
done
5. 無限循環(infinite loop)
while :
do
done
while true
do
done
6. until toop
until [ condition ]
do
done
7. select loop
root@localhost :/home/James/mypro/shell# select var in $list; do echo $var; done
1) option1
2) option2
3) option3
#? 2
option2
#?
8. break和break N,continue
跳出循環;跳出N重循環;繼續下次迭代
9. create empty file
touch newfile
> newfile
10. 利用/dev/null忽略不想要的輸出
command > /dev/null
command 2>/dev/null (注意2和>之間不能有空格)
command &>/dev/null
command >/dev/null 2>&1
11. Redirection
command < inputfile > outputfile
12. User defined file descriptor
exec fd> filename
command >&fd (>和&間布恩那個有空格)
exec fd<filename
command <&fd
exec fd<&- (關閉該文件描述符)
exec fd<>filename (read and write)
13. 獲得目前進程的pid
$$
root@localhost :/home/James/mypro/shell# ls -l /proc/$$/fd
總用量 0
lrwx------ 1 root root 64 2012-05-12 10:54 0 -> /dev/pts/2
lrwx------ 1 root root 64 2012-05-12 10:54 1 -> /dev/pts/2
lrwx------ 1 root root 64 2012-05-12 10:54 2 -> /dev/pts/2
lrwx------ 1 root root 64 2012-05-12 10:54 255 -> /dev/pts/2
lr-x------ 1 root root 64 2012-05-12 10:54 3 -> /home/James/mypro/shell/output
root@localhost :/home/James/mypro/shell# exec 3<&-
root@localhost :/home/James/mypro/shell# ls -l /proc/$$/fd
總用量 0
lrwx------ 1 root root 64 2012-05-12 10:54 0 -> /dev/pts/2
lrwx------ 1 root root 64 2012-05-12 10:54 1 -> /dev/pts/2
lrwx------ 1 root root 64 2012-05-12 10:54 2 -> /dev/pts/2
lrwx------ 1 root root 64 2012-05-12 10:54 255 -> /dev/pts/2
14. pipes
command1 | command2
command1 | command2 | commandN
command1 arg1 | command2 arg1 arg2
get_data_command | verify_data_command | process_data_command |
format_data_command > output.data.file
get_data_command < input.data.file | verify_data_command |
process_data_command | format_data_command > output.data.file
(評論:Linux的IPC pipe機制和shell相結合,其威力絕對是1+1 > 2的。
實際上command1 | command2是將command1的輸出重定向到管道write端,將command2的輸入重定向到管道read端,而後利用管道進行數據傳輸。我能夠猜想到,其實現大概會是這樣的:
主進程關閉標準輸入輸出,建立一個pipe[2],而後fork出兩個子進程,command1子進程從新關閉pipe的輸入端,從新打開stdin,command2子進程不安比pipe的輸出端,從新打開stdout.
這裏有個很重要的小技巧,進程必定會把0當成標準輸入,把1當成標準輸出。能夠參考Beginning Linux Programming中關於pipe IPC機制的講述,其中有個例子就是在不改變原來程序的基礎
上,利用主程序+pipe+重定向+fork+exec的技巧,實現了主程序能夠對子程序進行輸入和輸出控制。這就至關於能夠和現有的程序通信了,而他們之間不用特殊的約定。注:實際上是有的,就
是程序認爲0是輸入,1是輸出。)
pipe的做用:
=> 避免建立臨時文件。
=> 進行數據過濾。(fiter out data)
15. 顯示系統支持的信號
root@localhost :/home/James/proto3-247# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
kill -9 pid <==> kill -KILL pid <==> kill -SIGKILL pid
killall -s SIGKILL prog-name
16. trap (用以捕捉信號)