LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-15-2007, 12:51 AM   #1
gjagadish
Member
 
Registered: Oct 2005
Location: India
Distribution: Fedora Core
Posts: 88

Rep: Reputation: 15
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
 
Old 10-15-2007, 01:48 AM   #2
kWzTa
LQ Newbie
 
Registered: Sep 2005
Distribution: Slackware
Posts: 9

Rep: Reputation: 0
Hello,

you may try something like:
Code:
a="ls -l"
echo `$a`
HTH,
 
Old 10-15-2007, 02:00 AM   #3
gjagadish
Member
 
Registered: Oct 2005
Location: India
Distribution: Fedora Core
Posts: 88

Original Poster
Rep: Reputation: 15
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
 
Old 10-15-2007, 02:22 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
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...
 
Old 10-15-2007, 02:25 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,984
Blog Entries: 54

Rep: Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742Reputation: 2742
a=$(date|awk '{print \$1\"\n\"\$2}') ?
 
Old 10-15-2007, 02:46 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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

Last edited by jschiwal; 10-15-2007 at 02:52 AM.
 
Old 10-15-2007, 03:32 AM   #7
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
... and you don't need awk:

Code:
% dt="$(date +%a\\n%b)"
% echo "$dt"
Mon
Oct
 
Old 10-15-2007, 04:23 AM   #8
gjagadish
Member
 
Registered: Oct 2005
Location: India
Distribution: Fedora Core
Posts: 88

Original Poster
Rep: Reputation: 15
Hi jschiwal,

thanx for ur reply

that's what, what i was looking for

now my way is clear
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 12:21 PM
executing konsole from shell script Meriadoc Linux - Newbie 10 07-10-2009 08:08 AM
Executing a shell script command!! vishamr2000 Programming 4 08-15-2005 12:33 AM
executing shell script on apache nooodles Linux - General 1 06-29-2004 01:51 AM
executing a shell script from C llama_meme Programming 2 04-01-2002 09:10 PM


All times are GMT -5. The time now is 02:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration