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