LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-19-2006, 04:25 AM   #1
KM3
LQ Newbie
 
Registered: Oct 2006
Location: UK
Distribution: 2.6.16-gentoo-r7
Posts: 3

Rep: Reputation: 0
running a command with args from a shell script


Hi,
I'm sure this is easy, but it's been baffling me for an hour.

myscript.sh:
Code:
#!/bin/sh
cmd1="ls -l /home/me/ > /home/me/outfile"
$cmd1
but, cmd1 doesn't work as I want, which to behave exactly as if I had done this:

Code:
#!/bin/sh
ls -l /home/me/ > /home/me/outfile
thanks in advance!
 
Old 10-19-2006, 04:46 AM   #2
greeklegend
Member
 
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Rep: Reputation: 15
Damn this one has me baffled. Note how echo $cmd1 yeilds exactly what it should...
Is this a bug or some expected behaviour?
EDIT: This works...
Code:
#!/bin/bash
cmd1="ls -l /home/me/"
$cmd1 > /home/me/outfile

Last edited by greeklegend; 10-19-2006 at 04:48 AM.
 
Old 10-19-2006, 05:28 AM   #3
KM3
LQ Newbie
 
Registered: Oct 2006
Location: UK
Distribution: 2.6.16-gentoo-r7
Posts: 3

Original Poster
Rep: Reputation: 0
found the answer afterall:

Code:
#!/bin/sh
cmd1="ls -l /home/me/ > /home/me/outfile"
eval $cmd1
 
Old 10-19-2006, 01:46 PM   #4
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
You could also use an array, e.g.

Code:
#!/bin/sh
cmd1=(ls -l /home/me/ > /home/me/outfile)
${cmd1[*]}
This ${cmd1[*]} is slightly cumbersome, since bash only expands the first element given $cmd1. I use zsh, which expands $cmd1 to the entire array, which is a little more convenient.
 
  


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
running shell script: command not found tripat Red Hat 3 04-14-2010 01:21 PM
Running a shell script remotely via 'ssh <hostname> <command>' davee Linux - General 4 10-09-2005 01:38 AM
Running script as user that does not have shell sschueller Linux - General 1 04-24-2004 03:06 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM
c/c++ running a shell script? thermalpants Programming 3 05-12-2002 11:41 AM


All times are GMT -5. The time now is 01:34 AM.

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