简体   繁体   English

如何避免每个循环中出现不可打印的字符(shell脚本)

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

I wrote a shell script as follows 我写了一个shell脚本如下

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

The following are the details of corresponding directories: 以下是相应目录的详细信息:

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


the script output: 脚本输出:

 /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 

The problem is that last directory is left out.. I was expecting that the last entry is combined with a non printable character. 问题是最后一个目录被遗漏了。我期望最后一个条目与不可打印的字符组合在一起。 Permissions are all okay with the directories. 目录的权限都可以。 Please help me out of this mess. 请帮助我摆脱困境。 Thanks in advance 提前致谢

The problem is more apparent than it looks. 问题比看起来更明显。 There is no non-printable character. 没有不可打印的字符。

Let's take a look a cd error message from dash , the shell you're using instead of bash : 让我们看一下dash (您正在使用的shell而不是bashcd错误消息:

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

Now look at your error message: 现在查看您的错误消息:

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

And where does this colon come from? 结肠从哪里来?

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

Delete it, and it'll work. 删除它,它将起作用。

(Note that this is not canonical or entirely correct code, and people's suggestions about for f in rec* loops and subshells still apply.) (请注意,这不是规范或完全正确的代码,人们for f in rec*循环和子shell中for f in rec*的建议仍然适用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM