Sponsored Content
Top Forums Shell Programming and Scripting [bash] Executing script that is held in a variable Post 302387585 by ASGR on Sunday 17th of January 2010 11:03:41 AM
Old 01-17-2010
This is an extract from the project,
Code:

#!/bin/bash
################################################################################
function array_append	{ eval "$1=( \"\${$1[@]}\" \"\${$2[@]}\" )"; }
function array_assign	{ eval "$1=( \"\${$2[@]}\" )"; }
function array_rm_front { eval "$1=( \"\${$1[@]##$2}\" )"; }
function array_loads	{ local IFS=$'\n'; eval "$1=( \$( < \"$2\" ) )"; }
function array_script	{ local IFS=$'\n'; eval "\${$1[0]#\#!} <(printf \${$1[*]:1} )"; }
function string_assigns	{ eval "$1=( \"\$2\" )"; }
################################################################################
function tag_filters
{
	local -a	_FLTR_SRC_;	array_assign	_FLTR_SRC_	$2
	local		_FLTR_TAG_;	string_assigns	_FLTR_TAG_	$3
	local		_FLTR_APP_;	string_assigns	_FLTR_APP_	"false"
	local -a	_FLTR_RTN_

	for _LINE_ in "${_FLTR_SRC_[@]}"; do

	case $_LINE_ in
	\[$_FLTR_TAG_\]		)	_FLTR_APP_=true;;
	\[*\]			)	_FLTR_APP_=false;;
	*			)	;;
	esac

	case $_FLTR_APP_ in
	true			)	array_append	_FLTR_RTN_	_LINE_;;
	*			)	continue;;
	esac

	done
	array_assign $1 _FLTR_RTN_
}
################################################################################
function array_defrag
{
	local -a	_DFRG_SRC_;	array_assign	_DFRG_SRC_	$1
	local -a	_DFRG_RTN_
	for _ITEM_ in "${_DFRG_SRC_[@]}"; do
	if [ -n "$_ITEM_" ]
		then	array_append _DFRG_RTN_ _ITEM_
		else	continue
		fi
	done
	array_assign	$1	_DFRG_RTN_
}
################################################################################
# MAIN
################################################################################
	declare	-a	_file_cfg_ _file_tmp_

	array_loads	_file_cfg_	"/where/script/file/is"
	tag_filters	_file_tmp_	_file_cfg_ "script-python"
	array_rm_front	_file_tmp_	'##'*			# remove remarks
	array_rm_front	_file_tmp_	'['*			# remove tags
	array_defrag	_file_tmp_				# defrag array
	array_script	_file_tmp_				# execute script

And this is an example of a data file that would contain tags including
scripts as well as others. The location of which has to be specified in
the variable above.
Code:

[different-tags]
################################################################################
## LOCAL SCRIPTS
################################################################################
[script-bash]
#!/bin/bash
declare -i INT=10
for (( i=0 ; i<INT ; i++ )); do
	echo "Hello, Bash!"
	done
################################################################################
[script-perl]
#!/usr/bin/perl -w
use strict;
print "Hello, Perl!\n";
################################################################################
[script-python]
#!/usr/bin/python
print "Hello, Python!"
################################################################################
[more-different-tags]

The sections don't have a corresponding closing tag. They are terminated
with either another tag or end of file, handled by the 'for' loop in the
'tag_filters' function. It was designed to take wildcards so that similar
tags would be filtered and then the contents of the array could be
refined and/or appended to each other with another function.
Conveniently, it also takes absolutes.

Getting back to the problem... I'm still experiencing problems with
incomplete commands, due to the lack of single quotes around the
elements of the array. The whole process is programatically applied
and I have no opportunity to manually insert single quotes around the
elements of the array.

To solve the problem, I was wondering if it was possible to somehow
incorporate the single quote solution into the 'array_script' command
so that whenever a script is invoked, it is processed there.

Alternatively, an acceptable solution would be a function that converts
an array so that each element is surround by single quotes just before
the 'array_script' function is called.

I hope the above is more informative.

A.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

Bash script if condition not executing

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

10. 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
All times are GMT -4. The time now is 06:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy