1833582 Members
3472 Online
110061 Solutions
New Discussion

Re: for loop and cd ~

 
SOLVED
Go to solution
Barbara Flynn_1
Frequent Advisor

for loop and cd ~

Hi

I am trying to write a script to cd to a list of users home directories using a for in i and cd ~$i but it keeps complaining....

script is
for i in `cat userlist`
do
echo $i
cd ~$i
done

It echoes the $i but gives me an error then for the cd

barbara (successful echo $i)
ksh[4]: ~barbara: not found (failed cd ~$i)

I am sure it is something simple.....I have tried quotes adn brackets all over the place but nothing seems to work.....

Anyone got any ideas??

Cheers Barbara


5 REPLIES 5
G. Vrijhoeven
Honored Contributor
Solution

Re: for loop and cd ~

Hi B.

try putting the eval command in front of the for i in `cat userlist`
do
echo $i
eval cd ~$i
done



Anupam Anshu_1
Valued Contributor

Re: for loop and cd ~

Hi Barbara,

Please use sh-posix, that will solve your problem. I mean you need to add "#!/usr/bin/sh" in the first line of your program and then your script should work fine.

So your script should look like following:

----------------------
#!/usr/bin/sh

for i in `cat userlist`
do
echo $i
cd ~$i
done
-----------------------

This should solve your problem.

Cheers,

Anshu
KapilRaj
Honored Contributor

Re: for loop and cd ~

I hope the users are existing on the node where u r trying to run this !

Kaps
Nothing is impossible
Barbara Flynn_1
Frequent Advisor

Re: for loop and cd ~

Thanks guys, both those solutions worked!
Tapas Jha
Valued Contributor

Re: for loop and cd ~

Hi,

You can not run cd ~$i until you are using sha-bang(#!/usr/bin/sh) or eval command.
The first one will tell that this is a shell script(not comment).

Rgds
Tapas
Tapas Jha