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

Ophelia avatar
By Ophelia
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亂轉譯
請問這是為啥呢?

謝謝~




--
Tags: Linux

All Comments

Jake avatar
By Jake
at 2015-09-17T10:11
* 在 echo 內變成是 glob pattern…
Bennie avatar
By Bennie
at 2015-09-20T12:31
script內最好的解應該是設GLOBIGNORE了,除非你會要用
到 * 來 match
Elvira avatar
By Elvira
at 2015-09-23T22:00
是的,* 就是萬用字元,除了 . 開頭的全部都 match
Thomas avatar
By Thomas
at 2015-09-24T03:27
當然,如果是 a* 會 match a.out
Adele avatar
By Adele
at 2015-09-24T13:28
某些沒有 ls 的系統,ls 其實 echo * (busybox)
Catherine avatar
By Catherine
at 2015-09-24T19:20
解決方式是把 echo $routeStr2 改成 echo "$routeStr2"
Ina avatar
By Ina
at 2015-09-26T19:31
pttnews 這個是官方帳號還是?

HOSTAPD說找不到nl80211

Faithe avatar
By Faithe
at 2015-09-13T21:10
我按照 http://tinyurl.com/oo3yxwz 想設定我的EPC,我先用CentOS 6.7 32bit LiveDVD啟動,用YUM安裝epel和hostapd 後照網頁設定 [rootatlivedvd ~]# lspci -k | grep -A 3 -i and#34;networ ...

buffer overflow detected ***: multiget

Enid avatar
By Enid
at 2015-09-10T19:17
When i run multiget it show me and#34;buffer overflow detected ***: multiget terminatedand#34; How to fix buffer overflow? ubuntuatubuntu:/$ multiget *** ...

等寬字體不等寬

Noah avatar
By Noah
at 2015-09-10T18:27
先上圖, 預期結果對照組: http://i.imgur.com/N13pRdP.png (稱為 A 系統) 實際結果對照組: http://i.imgur.com/pMDIIIK.png (稱為 B 系統) A 系統是裝在 B 系統內的虛擬電腦,設定值幾乎和 B 系統一模一樣, 佈景主題與字體設定也 ...

更新後的倉頡3輸入法問題

Yuri avatar
By Yuri
at 2015-09-10T14:51
倉頡 三更新 後 變成如果輸 入的是五碼 就 不用再按空格 而如果少於五碼 的話 就 要再多按個空格 但打字習慣後會在每個字輸 入完時多加一個空格 導制文章會像這篇一樣.... 請 問一下有沒有方法可以解 法這問題 ? 或者設 定?或者回到上一版? 我是用ubuntu安裝 完後的預 設 輸 入法 ...

centos

Quintina avatar
By Quintina
at 2015-09-10T14:49
遇到問題如下: 有什麼方式 能解決?? CENTOS 7 可以 PING One of the configured repositories failed (Unknown), and yum doesnand#39;t have enough cached data to continue ...