作者pttnews (PTT新聞)
看板Linux
標題[問題] 遇到星號會亂掉,這啥鬼啊
時間Mon Sep 14 10:34:08 2015
這是我的 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亂轉譯
請問這是為啥呢?
謝謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.13.2.228
※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1442198050.A.1B1.html
→ kerwinhui: * 在 echo 內變成是 glob pattern… 09/14 10:45
所以glob pattern 就是.... 當下目錄的內容?
※ 編輯: pttnews (101.13.2.228), 09/14/2015 10:47:30
→ kerwinhui: script內最好的解應該是設GLOBIGNORE了,除非你會要用 09/14 10:49
→ kerwinhui: 到 * 來 match 09/14 10:49
→ kerwinhui: 是的,* 就是萬用字元,除了 . 開頭的全部都 match 09/14 10:51
→ kerwinhui: 當然,如果是 a* 會 match a.out 09/14 10:52
→ HamalAri: 某些沒有 ls 的系統,ls 其實 echo * (busybox) 09/14 22:33
→ kenduest: 解決方式是把 echo $routeStr2 改成 echo "$routeStr2" 09/15 09:54
→ kenduest: pttnews 這個是官方帳號還是? 09/16 11:03