作者garyzyg (garyzyg)
看板Linux
標題Re: [問題] shell scripts 中,遇到空白怎麼處理?
時間Fri Jan 16 14:28:48 2009
※ 引述《aeolus0829 (archer)》之銘言:
: 我想寫個 scripts ,依照名稱搬移到適當的目錄
: 若目錄不存在,則以名稱命名($F),建立一個新目錄後(/home/user/doc/$A),
: 再搬進去
: shell scripts 如下
: /var/www/html
: drwxr-xr-x 13 root root 4096 2008-06-02 09:06 manual
: drwxr-xr-x 2 root root 4096 2008-06-02 09:06 nut cgi bin
: drwxr-xr-x 5 root root 4096 2009-01-15 13:10 ora doc
: F=`find /var/www/html -type d -print`
: for A in $F; do
上面兩行改成下面兩行
find /var/www/html -type d -print |
while read A; do
: if [ -d /home/user/doc/$A ]; then
: mv $A /home/user/doc
: else
: mkdir /home/user/doc/$A
: mv $A /home/user/doc/
: done
: 問題來了,當這支 scripts 遇到 "nut cgi bin" 時,會把它當成
: nut
: cgi
: bin
: 三個字串
: 我嘗試過在 scripts 中把 $A 用 "$A" 包起來,但沒有作用
: 請問空白該怎麼處理?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.228.79.227
推 aeolus0829:神奇!改while 的確可以耶! 非常感謝 .. ^ ^ 01/16 14:47
→ aeolus0829:以前沒用過 while ,看來該來研究一下了 01/16 14:47
推 CrBoy:重點應該是read 不是while吧....@@" 我不會shell script.. 01/16 15:08
→ psboy:好像是因為read一次讀取一行 所以不會有被IFS中斷的問題? 01/16 15:25
推 aeolus0829:如樓上所說,的確是 read 的功勞 .. 囧 01/16 15:41
→ aeolus0829:原來是我想的太複雜了 :P 01/16 15:41