문제

I wrote a shell script as follows

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:

$ 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.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top