Operating System - HP-UX
1844192 Members
2393 Online
110229 Solutions
New Discussion

Using variable to execute command...

 
SOLVED
Go to solution
Sébastien N
Advisor

Using variable to execute command...

Hi,

I want to do something like that :
CMD="cat /etc/inittab > /tmp/inittab"
$CMD

The pb is that the ">" is not correctly parsed ! :-(
The $CMD is send to a function executing it and testing its return code.

Does anybody know the answer ?
6 REPLIES 6
Christopher McCray_1
Honored Contributor

Re: Using variable to execute command...

Hello,

Try escaping it with
CMD="cat /etc/inittab \> /tmp/inittab"
$CMD

Hope this helps

Chris
It wasn't me!!!!
A. Clay Stephenson
Acclaimed Contributor

Re: Using variable to execute command...

CMD="cat /etc/inittab > /tmp/inittab"

eval "${CMD}"

That should fix you.
If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: Using variable to execute command...

This should work .. use $() as command substitution.

CMD=$(cat /etc/inittab > /tmp/inittab)
$CMD

Sean OB_1
Honored Contributor

Re: Using variable to execute command...

Does it need to be as a variable?

Why not create them as functions.

cat_inittab ()
{
cat /etc/inittab > /tmp/inittab
}


then simply call the function.

# call function.
cat_inittab


Sridhar Bhaskarla
Honored Contributor
Solution

Re: Using variable to execute command...

Hi Sebastian,

A "3" will show your respect towards others for their time if they couldn't solve your problem. All the answers you got so far are "something" like what you said.

What you are executing is a command and it is to be treated with the same respect. So, you will need to do any of the following.

1. Declare it as a function and call the function.
2. Do exactly what you want to do but execute the variable with "eval" call.
3. Put it in command substitution syntax so that the shell will understand that it is a command.
4. Make it as an alias and run the alias.

alias CMD="cat /etc/inittab > /tmp/iniitab"
CMD

The solution closest to what you need is command substitution. If you don't want to use $ substitution, I guess you can replace double quotes with backticks (the key associated with ~).

CMD=`cat /etc/inittab > /tmp/inittab`
$CMD

-Sri



You may be disappointed if you fail, but you are doomed if you don't try
Sébastien N
Advisor

Re: Using variable to execute command...

Hi everybody,

It was not a lack of respect, sorry if it has been missunderstood.

I also want to apologize A. Clay Stephenson because his solution works properly (I did a mistake when writing the command).

Thanx for all.

Sebastien.