Command Substitution within Parameter Substitution?

Command Substitution within Parameter Substitution?

Post by Gazz » Sun, 12 Jun 2005 15:17:52



Hi,

(GNU bash, version 2.05b.0(1)-release (i586-mandrake-linux-gnu).)

Is it possible, in the above version of BASH, to have (a) command
substitution(s) within (a) parameter substitution(s)?  I tried the
following in a BASH script:

gqview ${$( cat $HOME/mydisplay_return )#*\ }

and it didn't work.  The above code line was to execute GQview (an image
viewer) with the name of an image file that was stored, on it's own, in a
file called 'mydisplay_return', hence the 'cat' command to get at the image
file name.  The file name stored in the 'mydisplay_return' file was
prefixed with an identifying number followed by a space, hence the '#*\ '
parameter substitution to just leave the file name.  But it doesn't work.
What's wrong?

Yours,
Gary Hayward.

 
 
 

Command Substitution within Parameter Substitution?

Post by Icarus Sparr » Sun, 12 Jun 2005 15:50:49



> Hi,

> (GNU bash, version 2.05b.0(1)-release (i586-mandrake-linux-gnu).)

> Is it possible, in the above version of BASH, to have (a) command
> substitution(s) within (a) parameter substitution(s)?  I tried the
> following in a BASH script:

No.

Quote:> gqview ${$( cat $HOME/mydisplay_return )#*\ }

> and it didn't work.  The above code line was to execute GQview (an image
> viewer) with the name of an image file that was stored, on it's own, in a
> file called 'mydisplay_return', hence the 'cat' command to get at the image
> file name.  The file name stored in the 'mydisplay_return' file was
> prefixed with an identifying number followed by a space, hence the '#*\ '
> parameter substitution to just leave the file name.  But it doesn't work.
> What's wrong?

If this was going to do anything, I would expect it to run the 'cat'
first, then use the output of that as the variable name, and then edit the
value of that variable. However it doesn't even do that.

Do what you want in 2 steps.

x=$(car $HOME/mydisplay_return)
gqview "${x#*\ }"

or, since you already are running an external program

gqview $( sed 's/.* //'  $HOME/mydisplay_return)

(Of course there is

read x < $HOME/mydisplay_return
gqview "${x#*\ }"

as well)

 
 
 

Command Substitution within Parameter Substitution?

Post by Chris F.A. Johnso » Sun, 12 Jun 2005 21:10:40



> Hi,

> (GNU bash, version 2.05b.0(1)-release (i586-mandrake-linux-gnu).)

> Is it possible, in the above version of BASH, to have (a) command
> substitution(s) within (a) parameter substitution(s)?  I tried the
> following in a BASH script:

> gqview ${$( cat $HOME/mydisplay_return )#*\ }

> and it didn't work.  The above code line was to execute GQview (an image
> viewer) with the name of an image file that was stored, on it's own, in a
> file called 'mydisplay_return', hence the 'cat' command to get at the image
> file name.  The file name stored in the 'mydisplay_return' file was
> prefixed with an identifying number followed by a space, hence the '#*\ '
> parameter substitution to just leave the file name.  But it doesn't work.
> What's wrong?

   You are trying to use parameter expansion on something that is not
   the name of a variable. (If the result of $(cat ...) were the name
   of a variable, you could do it with eval; but it's not.)

   When a complex command is causing problems, break it up:

read var < "$HOME/mydisplay_return"    ## no need for cat
gqview "${var#* }"

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

1. Substitution within substitution- Is it possible?

I have two  variables
jbc3=8
i=3

i now have the following loop

for i in 3
do
  echo $jbc$i
done

What i actually want to print is the value 8.
i.e. substutute the $i for 3 , then evaluate $jbc3.
Is there some way to combine these two substitutions.

Thanks in advance.
--
Paul Porcelli
Dept of Aerospace Engineering
University of Glasgow

2. Can't get Sony CUA-55E to work w/ 2nd controller

3. parameter substitution within backquotes

4. Motorola 56K Modemsurfr

5. Command substitution in rsh-command

6. Fooling Disk Suite

7. Variable substitution within a variable name

8. Large SCSI drive will not boot

9. Twolevel parameter substitution

10. Stuck in parameter substitution hell

11. Shell script question... parameter substitution

12. parameter substitution in cron

13. `at' and Bourne shell parameter substitution