繁体   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