[ale] Bash scripting q
JK
jknapka at kneuro.net
Thu May 8 18:42:08 EDT 2008
J. D. wrote:
> Is the desired effect for it to expand to /home/callinguser/user?
No, the desired effect is for it to expand to the home
directory of the user name supplied to the script as
arg $1. So if I say:
./myscript joe
I need a way within myscript to refer to "joe"'s home, which on
my system happens to be /home/joe. I cannot make any such
assumptions about the target system, though (that user home
directories live under /home, I mean). Which is why I
want to use the tilde.
-- JK
> Usually the tilda should expand to /home/user right? I'm not sure why
> you are using the tilda. $USER is a bash builtin but I guess there is no
> harm in resetting it.
>
> for me this works:
>
> #!/bin/bash
>
> USER=$1
> cp foo /home/$USER
> chown $USER /home/$USER/foo
>
>
> On Thu, May 8, 2008 at 3:38 PM, JK <jknapka at kneuro.net
> <mailto:jknapka at kneuro.net>> wrote:
>
> Hi folks,
>
> I am trying to do a very simple thing, and getting screwed by bash's
> evaluation
> rules. Basically, I need to write a script that accepts a user name,
> and then
> copies some files to that user's home dir, and also chowns them to that
> user.
> So I tried the obvious:
>
> #!/bin/bash
> USER=$1
> cp foo ~$USER
> chown $USER ~$USER/foo
>
> This totally fails to work as I expect, because tilde expansion happens
> before variable expansion. So if I supply 'joe' as the first arg,
> "~$USER"
> expands to ~joe (instead of the desired /home/joe), and the cp
> effectively
> evaluates to "cp foo '~joe'", which ends up creating a regular file
> named
> ./~joe
>
> Is there a clean workaround? I'd rather not force the user to
> supply both
> the user name and the user's home directory as script arguments. And
> doing a grep'n'cut on /etc/passwd is not too appealing either. I've
> checked
> a couple of books ("bash Cookbook", "Linux Shell Scripting", and an
> online tutorial or two), but the most any of them say is "Don't do
> that";
> they provide no workaround.
>
> An explicit command to force shell expansion of an arbitrary string
> would
> do the trick... I'm goofing around with echo and eval right now, with
> interesting, but not useful, results...
>
> Oops, just stumbled on an answer:
>
> cp foo $(eval echo $(echo ~$USER))
>
> works, although it makes my eyes water a bit. Is there a better way?
> (This post has devolved to the "Linux trivia quiz" level...)
>
> Thanks,
>
> -- Joe
>
> --
> I do not particularly want to go where the money is -
> it usually does not smell nice there. -- A. Stepanov
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org <mailto:Ale at ale.org>
> http://mail.ale.org/mailman/listinfo/ale
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
--
I do not particularly want to go where the money is -
it usually does not smell nice there. -- A. Stepanov
More information about the Ale
mailing list