1. 程式人生 > >shell 復習筆記

shell 復習筆記

增加 test name 例子 -c tle 一行 基礎 begin

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
循環判斷類
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

for((i=1;i<=10;i++));
do
echo $(expr $i \* 3 + 1);
done

條件 雙括號 用分號分開,分別是 初始值;範圍;變化 for do done


if [ -f "$FILE" ];then
echo "OK"
else
echo "error $FILE" > error.log
mail -s "$FILE backup fail" [email protected] <error.log
fi

開頭結尾 用if --- fi 條件後; if ;then else fi

while [ "$var" != "by" ]

do

echo "you input a char $var "

read var

done


-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
命令類
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
sort -t : -n -k 1 new.txt (-t 分隔符 -n 以純數字 -k 第幾個域 -u 去重 -r 逆排序,-f 忽略字母大小寫 -o file)
1:hello
2:hehe
8:exit
10:haha
11:nihao
12:hello

cut -d‘ ‘ -f 1 filename (-d 分隔符 -f 第幾個域 -c 字符 -b 字節 utf-8 3個字節一個漢字)


sed -n "1p" file (-n p搭檔使用 顯示第幾行 a下一行增加 i 上一行加 s 替換 "s/^s/title/g" d 刪除)


date -d "19700102 08:00:00" +%Y-%m-%d:%H:%M:%S
1970-01-02:08:00:00
date -s "20171111 11:02:00"


uniq [ -c | -d | -u ] [ -f Fields ] [ -s Characters ] [ -Fields ] [ +Characters ] [ InFile [ OutFile ] ]

-c 在輸出行前面加上每行在輸入文件中出現的次數。
-d 僅顯示重復行。
-u 僅顯示不重復的行。
-f Fields 忽略由 Fields 變量指定的字段數目。
-s Characters 忽略由 Characters 變量指定的字符的數目
cut -d‘:‘ -f 2 new.txt |sort -r |uniq -c -d
2 nihao
2 hello
2 hehe

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
正則類
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
基礎的 * , \<\> , \{n,m\} ,擴展的+, ?, 都是其前面的字符重復次數 《<<<重點《<<<<

#例子 new.txt
12:hello
2:hehe
10:haha
11:nihao
1:hello
20:nihao
19:hehe
8:exit
23.bay

$grep -e ‘^[1-9]:.*[t]$‘ new.txt <<<<<例子<<<<
8:exit
grep -e "\<ni\>" new.txt <<<<<例子<<<< \<ni\> 要完整的詞!!!
結果找不到
$ sed ‘s/ni/\*/g‘ new.txt |grep -e ‘\<hao\>‘ <<<<<例子<<<< 單詞前面是*這樣可以
11:*hao
20:*hao

$sed "s/[:.]/\+/g" new.txt <<<<<例子<<<<
12+hello
2+hehe
.....
23+bay

$ sed "s/[l]\{2\}/\*\*/g" new.txt <<<<<例子<<<<
12:he**o
2:hehe
$ sed ‘s/ni/\*/g‘ new.txt <<<<<例子<<<<
...
11:*hao
...

$ awk -F[:] ‘{ print $1,‘\t‘,$2 }‘ new.txt |$awk -F: ‘{ print $1"\t"$2 }‘ new.txt <<<例子的區別<<<<<<
12 hello |12 hello
2 hehe |2 hehe
.... |....

cat /etc/passwd |awk -F ‘:‘ ‘BEGIN {print "name,shell"} {print $1","$7} END {print "blue,/bin/nosh"}‘
awk -F: ‘/root/{print $7}‘ /etc/passwd
awk ‘/root/‘ /etc/passwd == grep -e ‘root‘ /etc/passwd

$awk -f awk.sc new.txt
#awk.sc
BEGIN{ FS=":";print "begin" }
{ print $1 }
END { print "end" }


sort cut awk 的域符號!!!!
sort -t‘:‘ -k 1
cut -d‘:‘ -f 1
awk -F[:.] { print $1 } <<<<<例子<<<< [正則 : 或 .] 如果用‘’ 不支持 sort或cut也不支持

shell 復習筆記