LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Executing a command in Shell Script (http://www.linuxquestions.org/questions/programming-9/executing-a-command-in-shell-script-591850/)

gjagadish 10-15-2007 01:51 AM

Executing a command in Shell Script
 
Hi,

I have a command in a variable. How to execute that command,

Something like,

a="date|awk '{print \$1\"\n\"\$2}'"

now i need to execute the command stored in $a

please help me

kWzTa 10-15-2007 02:48 AM

Hello,

you may try something like:
Code:

a="ls -l"
echo `$a`

HTH,

gjagadish 10-15-2007 03:00 AM

Hi,

thanks for ur reply.

i already know that,

we can execute command like, echo `$a`

but i try to execute my command in the same way,

i.e.

a="date|awk '{print \$1\"\n\"\$2}'"

echo `$a`

i got this error: -bash: date|awk: command not found

ghostdog74 10-15-2007 03:22 AM

some simple debugging may help you to get through

first, echo out the value of a, and see if the value is assigned and constructed properly. If its not constructed properly, no matter how you do, you will always get error...

unSpawn 10-15-2007 03:25 AM

a=$(date|awk '{print \$1\"\n\"\$2}') ?

jschiwal 10-15-2007 03:46 AM

Doing a single command like:
a=date
$a

would work as you are expecting. Maybe you want to create a function instead of a variable:
Code:

b()
> {
> date | awk '{ print $1 "\n" $2 }'
> }
jschiwal@hpamd64:~> b
Mon
Oct


radoulov 10-15-2007 04:32 AM

... and you don't need awk:

Code:

% dt="$(date +%a\\n%b)"
% echo "$dt"
Mon
Oct


gjagadish 10-15-2007 05:23 AM

Hi jschiwal,

thanx for ur reply

that's what, what i was looking for

now my way is clear


All times are GMT -5. The time now is 09:42 PM.