[bash] Executing script that is held in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] Executing script that is held in a variable
# 1  
Old 01-14-2010
[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 ?

A.
# 2  
Old 01-14-2010
Yes it is, usually with using the eval statement.
# 3  
Old 01-15-2010
Fantastic!!!
Thanks for reply.

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
-------------------------------------------
Code:
#!/bin/bash
declare str
if $str
    then do something
    else otherwise
    fi

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.

Any help would be appreciated.

A.

Last edited by ASGR; 01-15-2010 at 06:05 PM..
# 4  
Old 01-15-2010
I'm unsure about what you mean with vertical and horizontal scripts (are the latter oneliners?) but in any case you can write:
Code:
STRING="#!/bin/bash
cmd args
cmd args"
eval "$STRING"

Note that the shebang directive will be ignored as eval is using the current shell anyway.

Last edited by jlliagre; 01-15-2010 at 11:54 AM..
# 5  
Old 01-15-2010
Sry, should have explained.

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:

array[0]="#!/bin/bash"
array[1]="cmd args"
array[2]="cmd args"

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.

A.
# 6  
Old 01-15-2010
This just works:
Code:
#!/bin/bash
array[0]="#!/bin/bash" # This line is a no op
array[1]="echo one"
array[2]="date"
array[3]="pwd"

IFS="
 "
script="$(echo "${array[*]}")"

eval "$script"

# 7  
Old 01-16-2010
Outstanding work my European friend.

I just tried a more complex script with a for loop
and that also seems to work,

Code:
#!/bin/bash
declare IFS=$'\n'
ARRAY[0]="#!/bin/bash"
ARRAY[1]="declare -i	INT=10"
ARRAY[2]="for (( i=0 ; i<INT ; i++ )); do"
ARRAY[3]="echo "hello""
ARRAY[4]="done"
eval "$(echo "${ARRAY[*]}")"

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,
Code:
#!/usr/bin/perl -w
use strict;
print "Hello, Perl!\n";

#!/usr/bin/python
print "Hello, Python!"

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,
Code:
#!/bin/bash
ARRAY[0]="#!/usr/bin/perl -w"
ARRAY[1]="use strict;"
ARRAY[2]="print "Hello, Perl!\n";"

IFS="
 "
script="$(echo "${array[*]}")"
eval "$script"

I get the following error.
Code:
Perl!n;: command not found

The same with the python script,
Code:
#!/bin/bash
ARRAY[0]="#!/usr/bin/python"
ARRAY[1]="print "Hello, Python!""

IFS="
 "
script="$(echo "${array[*]}")"
eval "$script"

produces this error.
Code:
Python!: command not found

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.

Any further help would be appreciated.

A.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing sed command inside a bash script

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)
Discussion started by: Kangol
3 Replies

2. Shell Programming and Scripting

Bash script if condition not executing

issue is with .txt files (7 Replies)
Discussion started by: anil529
7 Replies

3. Shell Programming and Scripting

Acces Variable from expect-Script in bash-Script

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)
Discussion started by: gandalfthepink
3 Replies

4. UNIX for Dummies Questions & Answers

Write pid and command name to a txt file while executing a bash script

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)
Discussion started by: rahulkalra9
2 Replies

5. Shell Programming and Scripting

Bash script errors when executing

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)
Discussion started by: rayne127
2 Replies

6. Shell Programming and Scripting

variable issue in a bash in a shell script

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)
Discussion started by: bioBob
2 Replies

7. Shell Programming and Scripting

passing variable from bash to perl from bash script

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)
Discussion started by: arsidh
10 Replies

8. Shell Programming and Scripting

How do you parse a variable in a bash script?

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)
Discussion started by: vertical98
3 Replies

9. Shell Programming and Scripting

Trying to create variable, value not being held

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)
Discussion started by: cstovall
2 Replies

10. Shell Programming and Scripting

Finding out the length of a string held within a variable

:confused: Does anyone know which command I can use to find out the length of a string held within a variable? (5 Replies)
Discussion started by: dbrundrett
5 Replies
Login or Register to Ask a Question