shell 腳本學習筆記
阿新 • • 發佈:2018-12-17
.cn pri 教程 follow 與或 name source 學習 basic 前言
基於bash 並參考 菜鳥教程的shell 教程 http://www.runoob.com/linux/linux-shell.html
### shell 的執行方法
chmod +x ./test.sh
bash test.sh
輸入輸出
調試輸出
#!/bin/bash
set -x
外部參數
#!/bin/bash # author:菜鳥教程 # url:www.runoob.com echo "Shell 傳遞參數實例!"; ## echo 為輸出方法 echo "執行的文件名:$0"; echo "第一個參數為:$1"; echo "第二個參數為:$2"; echo "第三個參數為:$3"; $ chmod +x test.sh $ ./test.sh 1 2 3 Shell 傳遞參數實例! 執行的文件名:./test.sh 第一個參數為:1 第二個參數為:2 第三個參數為:3
printf 格式化輸出
#!/bin/bash
# author:菜鳥教程
# url:www.runoob.com
printf "%-10s %-8s %-4s\n" 姓名 性別 體重kg
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234
printf "%-10s %-8s %-4.2f\n" 楊過 男 48.6543
printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876
詳細見 http://www.runoob.com/linux/linux-shell-printf.html
重定向
標準輸入及標準輸出,默認均為當前用戶操作的終端。 command > file command < file command >> file 需要註意的是文件描述符 0 通常是標準輸入(STDIN),1 是標準輸出(STDOUT),2 是標準錯誤輸出(STDERR)。 command 2 > file ##重定向錯誤到file command > file 2>&1 ## 標準輸出及錯誤都重定向到file command > /dev/null 2>&1 ## 不顯示任何輸出
外部文件
. filename # 註意點號(.)和文件名中間有一空格
或
source filename
變量及賦值
參見 http://www.runoob.com/linux/linux-shell-variable.html
運算
算數、關系(大小)、布爾(與或非)、邏輯(與或)、字符串、文件測試
http://www.runoob.com/linux/linux-shell-basic-operators.html
正則
https://www.cnblogs.com/hanxiaoyu/p/5759477.html
數組
參見:http://www.runoob.com/linux/linux-shell-array.html
流程控制
參見:http://www.runoob.com/linux/linux-shell-process-control.html
函數
參見:http://www.runoob.com/linux/linux-shell-func.html
常用系統命令
http://www.runoob.com/linux/linux-command-manual.html
sed、tail、grep、wc、awk、more、cat
應用進階
用戶交互 read
http://www.runoob.com/linux/linux-comm-read.html
圖形交互 dialog
http://www.ttlsa.com/linux-command/linux-dialog-shell/
自動交互 expect
http://www.cnblogs.com/lixigang/articles/4849527.html
shell 腳本學習筆記