shell輸出的儲存

2021-06-19 10:15:47 字數 1578 閱讀 5598

想在shell中把乙個命令的輸出儲存在乙個變數中,這樣就可以一次呼叫,多次使用結果。但出現麻煩了。

比如想統計某一時刻網口資料報收發的情況,用ifconfig+grep過濾需要分別兩次,但此時rxtx都已經改變。如果用outbuf=`ifconfig`儲存你會發現echo $outbuf變成這樣了

eth0 link encap:ethernet hwaddr 00:0c:29:66:1a:2d inet addr:192.168.95.128 bcast:192.168.95.255 mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe66:1a2d/64 scope:link up broadcast running multicast mtu:1500 metric:1 rx packets:728045 errors:0 dropped:0 overruns:0 frame:0 tx packets:754529 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 rx bytes:135205752 (135.2 mb) tx bytes:251721983 (251.7 mb) interrupt:19 base address:0x2024 lo link encap:local loopback inet addr:127.0.0.1 mask:255.0.0.0 inet6 addr: ::1/128 scope:host up loopback running mtu:16436 metric:1 rx packets:104 errors:0 dropped:0 overruns:0 frame:0 tx packets:104 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 rx bytes:8900 (8.9 kb) tx bytes:8900 (8.9 kb)

這可不是我想要的。

通過反覆試驗,可以這樣來儲存:

outbuf="`ifconfig eth0`"

echo "$outbuf"

注意上面的"號哦。得到結果為:

eth0      link encap:ethernet  hwaddr 00:0c:29:66:1a:2d 

inet addr:192.168.95.128  bcast:192.168.95.255  mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fe66:1a2d/64 scope:link

up broadcast running multicast  mtu:1500  metric:1

rx packets:728023 errors:0 dropped:0 overruns:0 frame:0

tx packets:754513 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

rx bytes:135203758 (135.2 mb)  tx bytes:251719335 (251.7 mb)

interrupt:19 base address:0x2024

Shell 輸出幫助

blog 個人 目錄日常執行指令碼的時候,時間久了不知道指令碼的作用和實行了哪些功能,需要重新看指令碼原始碼。因此,需要對指令碼做一下輸出幫助。格式參考 my script does one thing well usage my script options input file to read....

shell將命令輸出結果儲存到變數中的3種常用方式

shell將命令輸出結果儲存到變數中的幾種形式及相應的輸出結果,使用時須注意對應關係,記錄在此以便使用時查詢比較 bin bash abc echo 66666666666666666 只用乙個 包住了echo語句 def echo 777777777777777 用 包住了echo語句,並用 包住...

shell的輸入與輸出

echo string echo命令輸出轉義符以及變數,如 home,並且可以讓系統執行tty命令 echo 007your home dictionary is home you are connected on tty c不換行 在linux中要使用 n來禁止換行 echo n f進紙 t跳格 ...