#!/bin/bash<br>#best to not tinker with USER or HOME variables<br>user=$1<br>home_dir=$(grep $user /etc/passwd | cut -f 6 -d ":")<br>cp foo $home_dir<br>chown $user $homedir/foo<br><br><br>--OR--<br><br>#!/bin/bash<br>
<br>su - $1<br>cd ~<br>cp <path to foo> .<br>exit<br><br><div class="gmail_quote">On Thu, May 8, 2008 at 3:38 PM, JK <<a href="mailto:jknapka@kneuro.net">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;">
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, "~$USER"<br>
expands to ~joe (instead of the desired /home/joe), and the cp effectively<br>
evaluates to "cp foo '~joe'", which ends up creating a regular file named<br>
./~joe<br>
<br>
Is there a clean workaround? I'd rather not force the user to 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 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 that";<br>
they provide no workaround.<br>
<br>
An explicit command to force shell expansion of an arbitrary string 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>
<a href="mailto:Ale@ale.org">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>
</blockquote></div><br><br clear="all"><br>-- <br>-- <br>James P. Kinney III <br>