作者aeolus0829 (archer)
看板Linux
標題[問題] shell scripts 中,遇到空白怎麼處理?
時間Fri Jan 16 08:17:42 2009
我想寫個 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
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: 220.130.203.236
→ psboy:試試看"${A}" 01/16 10:51
→ psboy:順便看看$IFS是不是空白 如果是 把IFS改成換行試試看 01/16 10:57
→ aeolus0829:a .. 樓上指的 $IFS 是什麼我不了解耶~ 01/16 11:32
※ 編輯: aeolus0829 來自: 220.130.203.236 (01/16 11:42)
→ aeolus0829:1f的方法確定不行,$A 和 "${A}" 的結果是一樣的 01/16 11:43
→ mathfeel:雙引號 01/16 13:37
→ mathfeel:我今天才寫了個跑convert的script用雙引號就沒問題。 01/16 13:39
→ aeolus0829:可以請樓上貼一下你的嗎? 01/16 14:40
→ psboy:man bash 找IFS 就知道IFS是什麼了 01/16 15:22