簡體   English   中英

如何避免每個循環中出現不可打印的字符(shell腳本)

[英]how to avoid non printable characters in for each loops (shell scripting)

我寫了一個shell腳本如下

for i in `readlink -f rec*`:
do
    cd $i
    pwd
    cd ..
    pwd
done

以下是相應目錄的詳細信息:

readlink -f rec*

/home/sandilya/part655gib/recup_dir.1
/home/sandilya/part655gib/recup_dir.2
/home/sandilya/part655gib/recup_dir.3
/home/sandilya/part655gib/recup_dir.4
/home/sandilya/part655gib/recup_dir.5


腳本輸出:

 /home/sandilya/part655gib/recup_dir.1 /home/sandilya/part655gib /home/sandilya/part655gib/recup_dir.2 /home/sandilya/part655gib /home/sandilya/part655gib/recup_dir.3 /home/sandilya/part655gib /home/sandilya/part655gib/recup_dir.4 /home/sandilya/part655gib mvrec.sh: 5: cd: can't cd to /home/sandilya/part655gib/recup_dir.5: /home/sandilya/part655gib /home/sandilya 

問題是最后一個目錄被遺漏了。我期望最后一個條目與不可打印的字符組合在一起。 目錄的權限都可以。 請幫助我擺脫困境。 提前致謝

問題比看起來更明顯。 沒有不可打印的字符。

讓我們看一下dash (您正在使用的shell而不是bashcd錯誤消息:

$ cd somewhere
dash: 5: cd: can't cd to somewhere
                                  ^-- Nothing follows the path

現在查看您的錯誤消息:

mvrec.sh: 5: cd: can't cd to /home/sandilya/part655gib/recup_dir.5:
                                                Spurious colon ---^

結腸從哪里來?

for i in `readlink -f rec*`:
                           ^--- Right here

刪除它,它將起作用。

(請注意,這不是規范或完全正確的代碼,人們for f in rec*循環和子shell中for f in rec*的建議仍然適用。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM