Sponsored Content
Top Forums Shell Programming and Scripting [bash] Executing script that is held in a variable Post 302387562 by Scrutinizer on Sunday 17th of January 2010 08:42:34 AM
Old 01-17-2010
Maybe we are making this too complicated. If I have an input file like so:

Code:
# cat infile
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
[script-nameofscript]
#!/usr/bin/perl -w
use strict;
print "Hello, Perl!\n";
[/script-nameofscript]
This is line 10
This is line 11
This is line 12
This is line 13
This is line 14

And if I use a script like this:
Code:
#!/bin/bash
script=""
shebang=""
reading_script=0
while IFS= read -r line
do
  if [[ $reading_script -eq 1 ]]; then
    if [[ $line == "[/script-"* ]]; then
      reading_script=0
      eval "${shebang#\#!}" <(printf "$script")
      script=""
      shebang=""
    else
      script="$script$line"
    fi
  else
    if [[ $line == "[script-"* ]]; then
      reading_script=1
      IFS= read -r shebang
    else
      printf "$line\n"
    fi
  fi
done < infile

Then the output is:
Code:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
Hello, Perl!
This is line 10
This is line 11
This is line 12
This is line 13
This is line 14

Or is that not what you had in mind?

---------- Post updated at 14:39 ---------- Previous update was at 14:16 ----------

Slightly optimized script:
Code:
#!/bin/bash
reading_script=false
while IFS= read -r line
do
  case $line in
    "[/script-"*)
       reading_script=false
       eval "${shebang#\#!}" <<< $script
       script=""; shebang="" ;;
    "[script-"*)
       reading_script=true
       IFS= read -r shebang ;;
    *)
       if $reading_script; then
         script="$script$line"
       else
         printf "$line\n"
       fi ;;
  esac
done < infile



---------- Post updated at 14:42 ---------- Previous update was at 14:39 ----------

Posix compliant version:
Code:
#!/bin/sh
reading_script=false
while IFS= read -r line
do
  case $line in
    "[/script-"*)
       reading_script=false
       printf "$script" | eval "${shebang#\#!}"
       script=""; shebang="" ;;
    "[script-"*)
       reading_script=true
       IFS= read -r shebang ;;
    *)
       if $reading_script; then
         script="$script$line"
       else
         printf "$line\n"
       fi ;;
  esac
done < infile

 

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 09:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy