Stupid Bash Trick: Make StackScripts work from a shell

NEVERMIND, THIS DOESN'T WORK!

Some of the functions I wrote up in my StackScripts were so cool, I wanted to use them after the machine was installed. However, on a StackScript where there are tags, it choked up bash.

So, we include the tag in a quote that escapes the <> chars, then wrap the whole thing in a case statement that detects whether the script is being run from an interactive shell, or from a non-interactive one (first boot as a StackScript invokation):

case "$-" in
        *i*)
                PATH=$(cd ${0%/*} && pwd -P)
                source "${PATH}/bash_lib_rh.sh"
                source "${PATH}/drupal_lib_rh.sh"
        ;;
        *)
                source '<ssinclude stackscriptid="154">'
                source '<ssinclude stackscriptid="162">'
        ;;
esac</ssinclude></ssinclude> 

The only thing you have to do after using this code is to make sure you export your UDF variables before you run anything.

2 Replies

You can use the tty command in a script to test if it's being run in an interactive shell. Just do tty > /dev/null and check the exit status:
> 0

Standard input is a terminal.

1

Standard input is not a terminal.

1

An error occurred.

Or use the "-t" test

if [ -t 0 ]
then
  echo STDIN is connected to a terminal
else
  echo No terminal
fi

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct