Dynamically-created shell commands

Reply

Join Date: Sep 2008
Posts: 2
Reputation: Jaoqua is an unknown quantity at this point 
Solved Threads: 0
Jaoqua Jaoqua is offline Offline
Newbie Poster

Dynamically-created shell commands

 
0
  #1
Sep 29th, 2008
I am trying to dynamically create some shell script and run it (but don't know how!).

I want to read in a string from a file (e.g. ' echo $myvar ') then run that string (and, yes, myvar IS an existing variable).

Can it be done? I have thought about writing it off to a .sh file and executing the file, but I can't get that to work either.

To put the problem into context, I want to read in a bunch of commands from a file and run them, and they may contain names of existing variables.

Incidentally, I'm using bash, but a general solution will be fine.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: Dynamically-created shell commands

 
0
  #2
Sep 29th, 2008
Hi Jaoqua!

How have you tried so far? Depending on how the file is formatted (are there spaces in some of the commands you want to run?) it should be as simple as:

for i in $(cat /path/to/file); do $i; done

If there are spaces in the command lines that you want to run, it'll get a little more complicated. You'd have to do something like this to change the Internal Field Separator temporarily, or else each line will be broken into a new command at every space:

Shell Scripting Syntax (Toggle Plain Text)
  1. cat /path/to/file |while IFS= read command; do
  2. $command
  3. done


I hope this helps!
-G
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2
Reputation: Jaoqua is an unknown quantity at this point 
Solved Threads: 0
Jaoqua Jaoqua is offline Offline
Newbie Poster

Re: Dynamically-created shell commands

 
0
  #3
Sep 30th, 2008
G,

Thanks for your answer. It gets part of the job done, but unfortunately not all of it. An example input file to my script might be...

Shell Scripting Syntax (Toggle Plain Text)
  1. echo Started
  2. aVar="Worked"
  3. echo $aVar
  4. echo Stopped

If you give this a go with your code you'll see that the echo s are fine, but the variables aren't processed properly.

I'm stumped! Help!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: Dynamically-created shell commands

 
0
  #4
Sep 30th, 2008
Okay, I think I'm confused now. It sounds like you just need to run that file as a shell script.

# sh ./file

Unless there's a reason to run it line-by-line... If you can't run it in place like the above example, you might want to copy it off to another file and execute it there. To do it using our original example:

[code]
cat /path/to/file |while IFS= read command; do
echo "$command"
done > /tmp/script.sh

sh /tmp/script.sh
[code]

I'm not sure if you can run it line by line. I was originally thinking you were using stand-alone commands, instead of running multiple commands together and setting variables. Hope this helps!

-G
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Dynamically-created shell commands

 
0
  #5
Sep 30th, 2008
Yes,

It would make more sense to just run it in a tmp script. The reason only the echo's work in the "while read" loop is that you're processing one line at a time, and not maintaining state between them, so the lines

aVar="Worked"
echo $aVar
are getting processed, but, by the time you get to your echo $aVar statement, aVar has no value if you run sh against each line.

if you just "run" each line, your

aVar="Worked"
command will produce an error that will also leave $aVar without a value:

bash: aVar="Worked": command not found
If you need to do this, try it this way. It will probably work, but I can't guarantee your system works the same as mine:

$ bash `echo $myvar|sed 's/\n/;/g'`
Started
Worked
Stopped
of course, if the script were more complicated (looping constructs, if/then statements, etc), your sed statement would become increasingly more bizarre and convoluted

I'd say go with Gromit's suggestion above to encounter minimum hassle

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC