[ale] Bash scripting q
Brian Pitts
brian at polibyte.com
Thu May 8 16:26:18 EDT 2008
JK 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
>
That is interesting. Eval is the best I've got.
$ FOO=~brian
$ echo $FOO
/home/brian
$ ls $FOO
[lists my home directory]
$ FOO="~brian"
$ echo $FOO
~brian
$ ls $FOO
ls: cannot access ~brian
$ ls $(eval echo $FOO)
[lists my home directory]
$ FOO=~$USER
$ echo $FOO
~brian
$ ls $FOO
ls: cannot access ~brian
$ ls $(eval echo $FOO)
[lists my home directory]
-Brian
More information about the Ale
mailing list