shell的输入和输出
1.echo ? ?echo [option] string
? ? ? ? ? ?-e 解析轉義字符
? ? ? ? ? ?-n 回車不換行,linux系統默認回車換行
? ? ? ? ? ? 轉移字符 \c \t \f \n
#!/bin/bash
#echo
echo -e "this echo's 3 newlne\n\n\n"
echo "OK"
echo
echo "this is echo's 3 ewline\n\n\n"
echo "this log file have all beendone">mylogfile.txt
[test@szbirdora ~]$ sh echod.sh
this echo's 3 newlne
?
OK
this is echo's 3 ewline\n\n\n
上面可以看到有-e則可以解析轉移字符,沒有不能解析。echo空輸出為空
2.read 可以從鍵盤或文件的某一行文本中讀入信息,并將其賦給一個變量
read variable1 variable2
eg.
#!/bin/bash
#readname
echo -n "first name:"
read firstname
echo -n "last name:"
read lastname
echo "this name is $firstname $lastname"
| toto@toto-virtual-machine:~/c$ sh readname.sh first name:toto last name:toto this name is toto toto |
?
3.cat 顯示文件的內容,創建內容,還可以顯示控制字符
?cat [options]filename1 filename2
? ? -v ? 顯示控制字符(Windows文件)
?cat命令不會分頁顯示,要分頁可以采用more、less
4.管道|
5.tee 把輸出的一個副本輸送到標準輸出,另一個副本拷貝到相應的文件中,一般與管道合用?tee [options] files
? ? ? ? ?-a 在文件中追加
eg.
[test@szbirdora 1]$ echo |tee myfile
[test@szbirdora 1]$ cat myfile
?
將myfile文件置空
6.文件重定向
command>filename ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ?---覆蓋輸出?
command>>filename ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ?---追加輸出
command>filename>&1 ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ---把標準輸出和標準錯誤重定向
command<<delimiter ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ---輸入直到delimiter分解符
command<filename ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ----輸入文件內容到命令
command<- ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? --- 關閉標準輸入
>nullfile.txt ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?---創建字節為0的文件
command1<filename>command3 ? ? ?? ? ? ? ---按從左到右順序執行
eg.
說明:myfile為空間
[test@szbirdora 1]$ df -lh>myfile
[test@szbirdora 1]$ cat myfile
Filesystem ? ? ? ? ? ?SizeUsed Avail Use% Mounted on
/dev/sda1 ? ? ? ? ? ??20G 3.3G ? 16G 18% /
none ? ? ? ? ? ? ?? ?2.0G ? ? 0 2.0G ? 0% /dev/shm
/dev/sda2 ? ? ? ? ? ??79G ? 17G ? 59G 23% /u01
/dev/sda4 ? ? ? ? ? ??28G 3.9G ? 22G 15% /u02
[test@szbirdora 1]$ df -lh>myfile
[test@szbirdora 1]$ cat myfile
Filesystem ? ? ? ? ? ?SizeUsed Avail Use% Mounted on
/dev/sda1 ? ? ? ? ? ??20G 3.3G ? 16G 18% /
none ? ? ? ? ? ? ?? ?2.0G ? ? 0 2.0G ? 0% /dev/shm
/dev/sda2 ? ? ? ? ? ??79G ? 17G ? 59G 23% /u01
/dev/sda4 ? ? ? ? ? ??28G 3.9G ? 22G 15% /u02
[test@szbirdora 1]$ df -lh>>myfile
[test@szbirdora 1]$ cat myfile
Filesystem ? ? ? ? ? ?SizeUsed Avail Use% Mounted on
/dev/sda1 ? ? ? ? ? ??20G 3.3G ? 16G 18% /
none ? ? ? ? ? ? ?? ?2.0G ? ? 0 2.0G ? 0% /dev/shm
/dev/sda2 ? ? ? ? ? ??79G ? 17G ? 59G 23% /u01
/dev/sda4 ? ? ? ? ? ??28G 3.9G ? 22G 15% /u02
Filesystem ? ? ? ? ? ?SizeUsed Avail Use% Mounted on
/dev/sda1 ? ? ? ? ? ??20G 3.3G ? 16G 18% /
none ? ? ? ? ? ? ?? ?2.0G ? ? 0 2.0G ? 0% /dev/shm
/dev/sda2 ? ? ? ? ? ??79G ? 17G ? 59G 23% /u01
/dev/sda4 ? ? ? ? ? ??28G 3.9G ? 22G 15% /u02
[test@szbirdora 1]$ cat >>myfile<<exit
> China
> Hubei
> Suizhou
> exit
[test@szbirdora 1]$ cat myfile
China
Hubei
Suizhou
7.exec 可以用來替代當前shell。現有任何環境變量都會清除
總結
以上是生活随笔為你收集整理的shell的输入和输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做事考虑后果,遵纪守法,会不会很痛苦?
- 下一篇: 4.录屏软件录屏端和接收端程序