Posted on 2007-04-18 15:58
天衣有縫 閱讀(1273)
評論(0) 編輯 收藏 引用 所屬分類:
linux shell/kernel
1.whereis 和
which 的區別
which: 只在PATH環境變量中尋找文件
whereis: 在系統定義的目錄中尋找
2.根據關鍵字查找man頁
舉例:apropos split 或者 man -k split
3.sed示例:
sed -e s/root/toor/g /etc/passwd >
~/test.out 替換 /etc/passwd中的root為toor輸出到~/test.out
sed -e 's/root/toor/g; s/ftp/ptf/g'
/etc/passwd 使用 -e 指定多個命令
sed -e 's/root/toor/g' -e ' s/ftp/ptf/g'
/etc/passwd 同上
使用命令文件:
/* test.sed 開始,不包含本行 */
s/root/toor/g
s/ftp/ptf/g
/* test.sed 結束,不包含本行 */
指令:sed -f
test.sed /etc/passwd
4.awk示例:
awk '{print $0}'
/etc/passwd $0表示完整的輸入記錄
awk -F":" '{print $1}'
/etc/passwd 打印第一列,以:為分隔符
awk -F":" '{print "username:
"$1 "\t\t\t user id: "$3}'
/etc/passwd 格式化并打印
使用命令文件:
/* test.awk 開始,不包含本行 */
BEGIN{
FS=":"
}
{printf "username: "$1 "\t\t\t user id:
"$3"\n"}
END{
printf "all done processing
/etc/passwd\n"
}
/* test.awk 結束,不包含本行 */
指令:awk -f
test.awk /etc/passwd
5.shell腳本
1)特殊變量:
?前一個命令輸出狀態
0當前腳本名
1~9參數
2)范例1,使用if語句
#!/bin/bash
echo "guest
the select color"
read COLOR
if [ $COLOR =
"yellow" ]
then
echo
"you are correct"
elif [ $COLOR = "blue" ]
then
echo
"you are correct also"
fi
3)范例2,使用case語句
#!/bin/bash
case "$1"
in
start)
echo "start......"
;;
stop)
echo "stop......"
;;
status)
echo "status......"
;;
*)
echo "usage: $0 {start | stop | status}"
;;
esac
4)范例3,使用迭代流程
#!/bin/bash
echo "guest
color: red, blue or orange\n"
read COLOR
while [ $COLOR !=
"orange" ]
do
echo
"incorrect, try again"
read
COLOR
done
echo
"correct"
5)使用雙引號進行命令替換
lines="$(wc -l 3.sh)"
echo $lines
6)測試文件
-d
file 目錄
-e
file 存在
-r
file 可讀
-w
file 可寫
-x
file 可執行