目錄 i(9/314)python
[echo.sh]linux
#!/bin/sh cd /tmp echo "hello world!"
1.2.1 選婿:位於第一行的#!正則表達式
whereis bash
查。自刪除腳本shell
#!/bin/rm # 運行這個腳本時,什麼也不會發生,只是刪自已 WHATEVER=65 echo "This line will never print!" exit $WHATEVER #腳本不會在這退出
1.2.2 找茬:程序執行的差別編程
source echo.sh
時,改變了目錄。1.2.3 shell的命令種類vim
source
執行的差別,不會建立子進程,直接在父進程中執行。1.3.一、變量數組
測試全局變量和局部變量的適用範圍安全
#!/bin/sh num=123 func1() { num=321 echo $num } Func2() { local num=456 #局部變量 echo $num } echo $num func1 echo $num func2 echo $num
1.3.二、用echo輸出變量bash
echo
掌握各類轉義字符 1.3.三、環境變量的相關操做編輯器
export
命令bash的啓動文件/登出文件
/etc/profile
/etc/bashrc
$HOME/.bash_profile
$HOME/.bashrc
$HOME/.bash_logout
unset
命令env
命令2.1.一、Shell腳本的參數
Shell編程中的函數
testfunc() { echo "$# parameters"; echo "$@"; }
shell編程中參數引用
0,1,2...
*
@
#
$
!
?
-
2.1.二、參數的用途
[ps.sh]
#! /bin/sh ps -eLf | grep $1
mv
和mkdir
命令2.2.一、標準輸入、標準輸出與標準錯誤
cat
命令後面的主要參數 2.2.二、管道與重定向
>
、<
、>>
、|
head
命令2.2.四、特殊文件的妙用
/dev/null
/dev/zero
/dev/tty
read
命令,我基本沒在命令中用過,最多編程中有用 grep
相關命令2.4.一、一切皆文件
ls
命令shell中有3種變量:
3.1.二、位置變量
${10}
[Process2.sh]
#!/bin/sh if[ $# -ne 2 ]; then echo "Usage: $0 string file"; exit 1; fi grep $1 $2; if[ $? -ne 0 ]; then echo "Not Found \"$1\" in $2"; exit 1; fi echo "found \"$1\" in $2";
0
3.2.一、函數定義
unset -f funcname
刪除函數[user_login.sh]
#!/bin/bash function user_login() { if who | grep $1 >/dev/null then echo "User $1 is on." else echo "User $1 is off." fi }
3.2.二、函數的參數和返回值
[演示函數的返回值]
#! /bin/bash add() { let "sum=$1+$2" return $sum }
3.3.二、退出狀態
$?
返回上一條語句的退出狀態退出狀態值
>0
:在重定向或單詞展開期間()失敗>128
:命令因收到信號而死亡3.4.二、while/until循環
[遍歷PATH路徑]
path = $PATH while [ -n $path ]; do ls -ld ${path%%:*} #ls -ld顯示path的第一個目錄 path = ${path#*:} #截去path中第一個目錄和冒號 done
3.4.三、跳出循環
break
3.4.四、循環實例
[例3.18 getopt的使用]
author=false list=false file="" while getopt alf: opt do case $opt in f) file=$OPTARG #將-f參數的下一個參數截取至file變量 ;; l) list = true ;; a) author = true ;; esac done shift ${{OPTIND - 1}} #刪除選項,留下參數
getopt
分析命令行標誌和參數0
grep、sed、awk、more、vi
4.1.2 如何學習正則表達式
4.1.3 如何實踐正則表達式
4.2.1 元字符
都支持的meta字符
^
:錨定行或字符串的開始$
:錨定行或字符串的結束.
*
[...]
\
4.2.2 單個字符
4.2.6 更多差別
4.3.一、擴展
4.3.2 案例研究:羅馬數字
0
sort、uniq、cut、join、head、tail、grep、wc、fmt、fold、pr、tr
5.1.一、sort命令的行排序
sort file.txt
sort -d file.txt
sort -d -f file.txt
5.1.二、sort命令的字段排序
sort -t: -k3 -n /etc/group
5.1.三、sort小結
uniq
wc
head
與tail
5.6.四、其餘字段處理方法
awk
5.7.二、其餘選擇
sed、awk、perl、python
0
ls、cat、chown、chgrp、chmod、umask、mkdir、touch、find、xargs、comm、diff、vimdiff、fdisk、mkfs、df、mount
6.1.三、文件的權限
chown
和chgrp
,以及chmod
6.1.四、文件的修改時間
touch
更新文件的訪問和修改時間6.2.二、遍歷文件
[例6.10 xargs使用]
find /tmp -type f -print | xargs file
6.3.一、使用comm比較排序後文件
comm file1 fiel2
6.3.三、其餘文本比較方法
vimdiff
7.1.1 挑選編輯器
7.1.2 sed的版本
sed --version
7.2.1 sed的工做方式
sed -e 'd' /tmp/passwd.bak
# d是刪除,所有刪除sed -e '5d' /tmp/passwd.bak
# 刪除第5行7.3.1 替換
sed -e 's/power/jiechen/g' /tmp/a.c
#s是替換,g是全局7.4.1 組合多條命令
sed -n -e '=;p' /tmp/passwd.piece
sed -e 's/$/\r/' myunix.txt > mydos.txt
awk '{print $0}' /etc/fstab
# print $0是打印整行,print和print $0效果是同樣的8.2.2 使用其餘字段分隔符
awk -F ":" '{print "USER: "$1 "\tSHELL: "$7}' /tmp/passwd.piece
20190314過了一遍chap1和2,仍是有一些知識點不知道的。
20190315過了一遍chap3-5。
20190316過了一遍chap6
20190318過了一遍chap七、08