| | |
Dynamically-created shell commands
![]() |
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
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. '
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.
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.
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:
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:
I hope this helps!
-G
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)
cat /path/to/file |while IFS= read command; do $command done
I hope this helps!
-G
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
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...
If you give this a go with your code you'll see that the
I'm stumped! Help!
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)
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!
Okay, I think I'm confused now. It sounds like you just need to run that file as a shell script.
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
# 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
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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
command will produce an error that will also leave $aVar without a value:
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:
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
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
if you just "run" each line, your
•
•
•
•
aVar="Worked"
•
•
•
•
bash: aVar="Worked": command not found
•
•
•
•
$ bash `echo $myvar|sed 's/\n/;/g'`
Started
Worked
Stopped

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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: Hex conversion problem
- Next Thread: How can i set column width in unix?
| Thread Tools | Search this Thread |





