Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top