shell腳本輸入



shell 開頭

以sh後綴開頭#註釋,表明採用何種解釋器

<code> #!/bin/bash /<code>添加作者等說明

<code> #/bin/bash 文件權限

<code>$ ll total 4 -rw-r--r-- 1 root root 88 Apr 24 22:15 demo.sh/<code>其中可以看到,demo.sh沒有可執行權限;但是 bash demo.sh可以運行

<code> $ ./demo.sh -bash: ./demo.sh: Permission denied $ bash ./demo.sh shell demo/<code>也可以通過chmod 修改權限

<code>$ chmod 755 ./demo.sh # or $ chmod u+x ./demo.sh # 給當前擁有者添加可執行權限 $ ll -rwxr--r-- 1 root root 88 Apr 24 22:15 demo.sh/<code>

shell 輸出 - echo

echo 輸出特殊字符

<code> $ echo -e "hello\tworld" $ echo -e "hello\nworld" $ echo -e "hello\fworld"/<code>輸出標色字符串

<code> # \e[35m開啟,\e[0m關閉 $ echo -e "\e[35mok\e[0m" $ echo -e "\e[32mOK\e[0m"/<code>

shell - 輸出 printf

<code>$ printf "%5d" 12 # 十進制右對齊輸出,但是默認不換行 $ printf "%-5d" 12 # 十進制左對齊輸出/<code>

shell - 輸入 read

<code>$ read name #把輸入的值,賦值給name $ echo $name $ read name age address # 讀入3組 $ echo $name $age $address $ read -p "請輸入用戶名" user $ echo $user $ read -t -10 -p "請輸入用戶名" user # 3s後自動退出等待輸入狀態 $ echo $user $ read -s -p "請輸入密碼" pass # 不顯示方式輸入密碼/<code>

shell - 管道|

<code> * who查看最近登錄用戶 * wc 統計輸入數據的行數-l,單詞數 -w, 字節-c,字符-m $ who | wc -l * ss 可查看系統服務監聽的端口,grep 具備過濾功能 $ ss -nutlp | grep sshd/<code>