linux shell腳本
大量重複執行的工作
shell (Linux殼), 一類程序的名稱
文本文件-------> shell命令, /bin/bash提供邏輯控制語句,
重定向符號的使用
/dev/stdin 標準輸入設備(鍵盤) 0
/dev/stdout 標準輸出設備(顯示器) 1
/dev/stderr 標準錯誤輸出設備(顯示器) 2
輸出重定向符號
> >> 用於重定向標準輸出
[root@shell ~]# ls -ldh /etc/ > /tmp/1.txt
[root@shell ~]# ls -ldh /tmp/ >> /tmp/1.txt
2> 2>> 用於重定向標準錯誤輸出
[root@shell ~]# ls -ldh /asjdfklasjdlfasd 2> /tmp/1.txt
&> 同時重定向標準輸出及標準錯誤輸出
特殊設備文件: /dev/null
[root@shell ~]# ls -ldh /etc/ &> /dev/null
輸入重定向符號
[root@shell ~]# tr 'a-z' 'A-Z' < /tmp/1.txt
ABCDEF
[root@shell ~]#
[root@localhost ~]# mail -s "test fstab" root@localhost < /etc/fstab
輸出信息:
1 echo
[root@shell ~]# echo "請輸入你的選擇:" 默認會打印換行符
請輸入你的選擇:
[root@shell ~]# echo -n "請輸入你的選擇:"
請輸入你的選擇:[root@shell ~]#
[root@shell ~]# echo -e "a\nbb\nccc" \n:回車 或者\r
a
bb
ccc
[root@shell ~]# echo -e "a\tbb\tccc" \t tab鍵
a bb ccc
[root@shell ~]#
2 printf
[root@shell ~]# printf "hello world"
hello world[root@shell ~]#
3 HERE DOCUMENT -----> 輸出多行內容
[root@shell ~]# cat << eof
> 1. 安裝KVM
> 2. 重置所有虛擬機
> eof
1. 安裝KVM
2. 重置所有虛擬機
[root@shell ~]#
雙引號和單引號的區別:
單引號: 所有字符會失雲原有的含義
雙引號: 特殊字符會被轉義
如何處理交互式命令:
[root@shell ~]# echo "redhat" | passwd --stdin tom &> /dev/null
[root@shell ~]# echo -e "n\np\n1\n\n+500M\nw\n" | fdisk /dev/vdb &> /dev/null