[bash] Executing script that is held in a variable
Hi,
I'm building an installation system that uses separate data
files that contain the individual characteristics of each package.
Within the data file, I would like to incorporate a section that
contains a bash script that is loaded into an array which is
then redirected to bash to be executed.
Before I go into detail, I'd like to ascertain if it is possible ?
The data file would contain a section with the following tag
that is filtered from the other tags and loaded into an array.
The section is terminated either by another tag or end of file.
declare -a ARRAY
[script-nameofscript]
do bash stuff
[script-otherscripts]
more bash stuff
The tags are then removed leaving the raw bash code to be
redirected to bash to execute.
Would I need to include the bash header shebang (I think it's
called) at the beginning or does bash assume it's a bash script?
Having the option to use perl, python or ruby would be convenient.
Lastly, I'm not that familiar with the 'eval' command regarding
redirecting arrays that contain scripts to be executed in bash.
Therefore, an example would be greatly appreciated.
A.
---------- Post updated 15-01-10 at 08:59 AM ---------- Previous update was 14-01-10 at 10:48 PM ----------
Quick update.
I've found a number of examples that use strings that
contain commands to be executed with eval,
STRING="cmd args; cmd args"
eval "$STRING"
The way the install system works, will be to contain
a script in the vertical style in an array as opposed to the
horizontal scripting style associated with strings.
script example to be loaded into array
-------------------------------------------
Although it is possible to convert the vertical script into a
horizontal script, I would like to preserve the first line
that identifies the script type keeping the system flexible
and hence my preference for the vertical scripting style.
This may seem obvious, but from experience I get the
feeling this will not work,
eval "${CMD[@]}"
I've also considered the possibility, but not tried due to
unfamiliarity, of using IFS=$'\n' and possibly echo or
printf.
I'm unsure about what you mean with vertical and horizontal scripts (are the latter oneliners?) but in any case you can write:
Note that the shebang directive will be ignored as eval is using the current shell anyway.
Horizontal scripting style does refer to the appending
of commands one after the other separated with semi-
colons otherwise referred to as 'one liners' ;-). As
opposed to using new lines for each command.
With reference to your example, when the code is loaded
into the array it will look something like this:
However, the array is most likely to contain complex
bash scripts with for loops, if's and case statements as
well as variables of all types.
Bash will have to be able to evaluate all the normal things
it does but also take into account that it is a script that is
held in an array.
What I don't know is if Bash will recognise the whole script
in the array when it is evaluated so that I don't get syntax
errors when it tries to recognise the different elements of
each command e.g. the 'do' and 'done' part of a 'for' loop.
I just tried a more complex script with a for loop
and that also seems to work,
Getting back to the shebang, if I wanted to run a
perl script, would this work from within bash,
#!/usr/bin/perl
Thanks for all your help.
A.
---------- Post updated 16-01-10 at 09:30 AM ---------- Previous update was 15-01-10 at 02:23 PM ----------
Additional.
Found some perl and python scripts,
but the method that worked for the bash scripts
doesn't seem to work for the other scripts.
Using the previous method with the perl script,
I get the following error.
The same with the python script,
produces this error.
I tested the perl and python scripts in terminal and the
output was as expected.
Although I have *no* idea what I'm talking about, as an
alternative, I'm experimenting with redirection to bash
so that the script is executed from stdin if it is possible.
I want to run commands inside a bash script.
An example is
I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable
sed.sh regexp
sed.sh "-i \"s/<p>//g\""
then call
sed "$regexp" $fl (3 Replies)
Hi all,
I have a little problem with a expect in a bash Script.
The hull of my script:
#!/bin/sh
( expect -c '
set a \"eee\"; # the variable a
' )
echo $a; # using the variable out of the expect script
I would like to use the variable out of the expect script(in bash),... (3 Replies)
Hi All,
Just have a requirement, I am executing a bash shell script, my requirement is to catch the pid and job name to a txt file in the same directory, is there anyway to do it? please help me out.
Regards
Rahul
---------- Post updated at 08:42 AM ---------- Previous update was at... (2 Replies)
I'm working on a script that will search through a directory for MKV files and remux them to MP4. I found a few other scripts around the net that do this, but they all have limitations that don't always get the job done. The main limitation I've found is that most scripts look for the video track... (2 Replies)
Hi,
I am trying to write a PBS shell script that launches a bash process. The issue is that the bash process needs a variable in it and the shell script is interpreting the variable. How do I pass this as a literal string? Here is my code snippit:
TMP=".fasta"
FILEOUT=$FILE$TMP
cd... (2 Replies)
Hi All,
I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl
FROM_DATE="06/05/2008"
TO_DATE="07/05/2008"
"perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename"
filename has... (10 Replies)
I have a script I use on my web server (Apache2). I am changing to Lighttpd and need to make a few changes.
This is what I use on my apache server
#!/bin/bash
# accepts 3 parameters: <domain name> <user name> <XXXXXXXX>
# domain name is without www (just domain.com)
# username would be... (3 Replies)
I tried creating a variable with the awk command. The correct value is being printed to the screen; however, the variable max is not being set. The command "echo $max" is null. Any help would be greatly appreciated. Thank you.
cat test.txt:
-rw-r--r-- 1 root root 0 2005-01-12 20:51... (2 Replies)