DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   Dynamically-created shell commands (http://www.daniweb.com/forums/thread148173.html)

Jaoqua Sep 29th, 2008 10:47 am
Dynamically-created shell commands
 
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.

Gromit Sep 29th, 2008 5:37 pm
Re: Dynamically-created shell commands
 
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:

cat /path/to/file |while IFS= read command; do
  $command
done


I hope this helps!
-G

Jaoqua Sep 30th, 2008 4:42 am
Re: Dynamically-created shell commands
 
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...

echo Started
aVar="Worked"
echo $aVar
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!

Gromit Sep 30th, 2008 12:14 pm
Re: Dynamically-created shell commands
 
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

eggi Sep 30th, 2008 9:36 pm
Re: Dynamically-created shell commands
 
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

Quote:

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

Quote:

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

Quote:

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:

Quote:

$ 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


All times are GMT -4. The time now is 1:47 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC