Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

can not parser ~$user

SOLVED
Go to Solution
Regular Advisor

can not parser ~$user

user=`whoami`
in my script, I have the following lines

for i in `ls ~$user/.*table`
do
echo "Modifying $i now....."
echo
done

when I run it, it always put the ' ' around $user and gave the following errors

++ ls '~anderson/.*table'
ls: ~anderson/.*table: No such file or directory

If I do ls ~anderson/.*table, it gave desired output.

how do I get around the problem :-(


thak you

but, if I run manually like "ls ~yanga
>
7 REPLIES
Honored Contributor [Founder]

Re: can not parser ~$user

First problem is that you are apparently using the Korn shell. This doesn't happen in the POSIX shell, /usr/bin/sh, which is the standard HP-UX shell. :)
--
Jeff Traigle
Honored Contributor [Founder]

Re: can not parser ~$user

Jeff is right. It works in the POSIX shell but not the Korn shell. The man pages for both shells seem to have the same description for tilde substitution, so my guess is that you've found a bug in HP-UX's version of the Korn shell.

JP
Acclaimed Contributor [Founder]

Re: can not parser ~$user

PHCO_33169 corrects some tidle related bugs in ksh for 11.11; since you didn't bother to identify your OS version, this may or may not be applicable. In any event, the use of ksh on HP-UX is somewhat discouraged as sh (the POSIX shell) is the default and I suspect gets much more rigorous testing than does ksh.
If it ain't broke, I can fix that.
Honored Contributor [Founder]

Re: can not parser ~$user

Since the only workaround to your problem seems to be injecting the "eval" command in the for loop line, it looks like the shell isn't doing variable expansion before running the command. You can force it do so using eval...

for i in `eval ls ~$user/.*table`
do
echo "Modifying $i now....."
echo
done

hope it helps!
Regular Advisor

Re: can not parser ~$user

yes, "eval" did the trick!

thank you
Honored Contributor [Founder]

Re: can not parser ~$user

Minor correction to my last post, the shell is doing variable expansion but not doing the filename metacharacter expansion. Since ~$user is the home directory of user "anderson" on your system, hence the shell needs to be forced to re-evaluate the command line before executing it. Otherwise the command tries to list all files named ".*table" under the sub-directory "~anderson".

hope it helps!
Highlighted
Regular Advisor

Re: can not parser ~$user

excellent explanation!!

really appreciate!