Re: can not parser ~$user (60 Views)
Reply
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 1 of 8 (60 Views)
Accepted Solution

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
>
Please use plain text.
Honored Contributor
Jeff_Traigle
Posts: 1,354
Registered: ‎03-04-2004
Message 2 of 8 (60 Views)

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
Please use plain text.
Honored Contributor
John Poff
Posts: 2,448
Registered: ‎05-22-2001
Message 3 of 8 (60 Views)

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
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 4 of 8 (60 Views)

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.
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 5 of 8 (60 Views)

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!
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 6 of 8 (60 Views)

Re: can not parser ~$user

yes, "eval" did the trick!

thank you
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 7 of 8 (60 Views)

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!
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 8 of 8 (60 Views)

Re: can not parser ~$user

excellent explanation!!

really appreciate!

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation