遇到星號會亂掉,這啥鬼啊 - Linux

By Ophelia
at 2015-09-14T10:34
at 2015-09-14T10:34
Table of Contents
這是我的 shell script
#!/bin/bash
routeStr2=$(route )
echo "================="
echo $(echo $routeStr2 | grep -c "default")
echo $routeStr2
exit 0
因為我想判斷route指令跑出來的東東有多少default指令, 迴圈就跑多少次...
問題來了
如果我把route執行結果直接dump string,
他會是一個字串,中間沒有斷行,
例如
routeStr2
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use
Iface default 172.11.23.1 0.0.0.0 UG 0 0 0 eth0 default 192.168.42.129
0.0.0.0 UG 10 0 0 usb0 10.2.0.0 = download dwhelper examples.desktop font
fontconfig hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img
index.html magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh
VirtualBox VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂
255.255.0.0 U 1 0 0 eth1 140.112.172.0 192.168.42.129 255.255.255.0 UG 0 0 0
usb0 172.11.23.0 = download dwhelper examples.desktop font fontconfig
hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img index.html
magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh VirtualBox
VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂 255.255.254.0 U
1 0 0 eth0 192.168.42.0 = download dwhelper examples.desktop font fontconfig
hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img index.html
magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh VirtualBox
VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂 255.255.255.0 U
1 0 0 usb0
很亂吧, 只要routeStr2裡頭有星字號, bash會自動帶入當下目錄的內容
變成一沱怪東西
後來改為
#!/bin/bash
routeStr2=$(route )
echo "================="
echo $(echo "$routeStr2" | grep -c "default")
echo "$routeStr2"
星字號就不會被bash亂轉譯
請問這是為啥呢?
謝謝~
--
#!/bin/bash
routeStr2=$(route )
echo "================="
echo $(echo $routeStr2 | grep -c "default")
echo $routeStr2
exit 0
因為我想判斷route指令跑出來的東東有多少default指令, 迴圈就跑多少次...
問題來了
如果我把route執行結果直接dump string,
他會是一個字串,中間沒有斷行,
例如
routeStr2
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use
Iface default 172.11.23.1 0.0.0.0 UG 0 0 0 eth0 default 192.168.42.129
0.0.0.0 UG 10 0 0 usb0 10.2.0.0 = download dwhelper examples.desktop font
fontconfig hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img
index.html magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh
VirtualBox VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂
255.255.0.0 U 1 0 0 eth1 140.112.172.0 192.168.42.129 255.255.255.0 UG 0 0 0
usb0 172.11.23.0 = download dwhelper examples.desktop font fontconfig
hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img index.html
magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh VirtualBox
VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂 255.255.254.0 U
1 0 0 eth0 192.168.42.0 = download dwhelper examples.desktop font fontconfig
hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img index.html
magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh VirtualBox
VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂 255.255.255.0 U
1 0 0 usb0
很亂吧, 只要routeStr2裡頭有星字號, bash會自動帶入當下目錄的內容
變成一沱怪東西
後來改為
#!/bin/bash
routeStr2=$(route )
echo "================="
echo $(echo "$routeStr2" | grep -c "default")
echo "$routeStr2"
星字號就不會被bash亂轉譯
請問這是為啥呢?
謝謝~
--
Tags:
Linux
All Comments

By Jake
at 2015-09-17T10:11
at 2015-09-17T10:11

By Bennie
at 2015-09-20T12:31
at 2015-09-20T12:31

By Elvira
at 2015-09-23T22:00
at 2015-09-23T22:00

By Thomas
at 2015-09-24T03:27
at 2015-09-24T03:27

By Adele
at 2015-09-24T13:28
at 2015-09-24T13:28

By Catherine
at 2015-09-24T19:20
at 2015-09-24T19:20

By Ina
at 2015-09-26T19:31
at 2015-09-26T19:31
Related Posts
HOSTAPD說找不到nl80211

By Faithe
at 2015-09-13T21:10
at 2015-09-13T21:10
buffer overflow detected ***: multiget

By Enid
at 2015-09-10T19:17
at 2015-09-10T19:17
等寬字體不等寬

By Noah
at 2015-09-10T18:27
at 2015-09-10T18:27
更新後的倉頡3輸入法問題

By Yuri
at 2015-09-10T14:51
at 2015-09-10T14:51
centos

By Quintina
at 2015-09-10T14:49
at 2015-09-10T14:49