0
votes

I want to plant multiple Bash scripts inside my "main" tcl program - so no external .sh scripts - I want it all in one place. I already found out how to plant one script into tcl:

set printScript { echo $HOME }

proc printProc {printScript} {
    puts [exec /bin/bash << $printScript]
}

printProc $printScript

My question is now: How can I use this concept to implement scripts that call other scripts without hardcoding the called script into the calling script? Let's say I have something like this:

script1.sh

script2="$PWD/script2.sh"
#do some stuff
if [something]
then
    $script2
fi
#do some more stuff

Can the above mentioned concept be used to solve my problem? How can this be done?

1
Bash has functions, use them. - n. 1.8e9-where's-my-share m.
I have many scripts that call two scripts. I could use functions in bash, but I still would have to define that function in every single script, which would result in repeating code... - harrisonfooord
Don't have many scripts. Have one script with many functions instead. - n. 1.8e9-where's-my-share m.
You cannot mix syntax for two different languages like that. The only way to do that "reliably" (i.e., horribly complex and extremely difficult to maintain) would be to keep scripts as strings in the TCL script, and pass them to standard input of bash. @n.m. has the right idea. - l0b0
Write all of your functions in one script function.sh and use the functions of that script by mentioning it in your other scripts . ..../function.sh - Aman

1 Answers

0
votes

Every script is a string, so, yes, you can use string manipulation to build scripts out of script primitives, as it were. It's not the best solution, but it's possible. If you choose to build scripts by string manipulation, substitution by string map is probably better than substitution by variable. Something along the lines of the following:

 set script1 {
      #do some stuff
      if [something]
      then
          %X
      fi
      #do some more stuff
 }
 set maplist {%% %}
 lappend maplist %X {$PWD/script2.sh}
 set executable_script [string map $maplist $script1]

Other solutions include

  1. Writing everything in Tcl
  2. Writing everything in bash script, if possible
  3. Writing a master bash script with functions and calling those functions from Tcl