0

I have a script (say Task.sh) (with executable permission):

Pseducode:

if [[ Not called from inside a script ]]; then echo 'Not called from inside a script' else echo 'Called from a script' fi 

I have a script say Main.sh (with executable permission):

Value=$(Task.sh) echo "${Value}" 

Expected Outcome

$ Task.sh Not called from inside a script $ Main.sh Called from a script 

Help Request

Please suggest what conditional to put in the pseducode for Task.sh

7
  • Does running bash -c Task.sh count as running inside a script? If not, why not?
    – muru
    CommentedOct 3, 2023 at 20:30
  • 2
    Do you want to catch just something like Value=$(Task.sh), or also a script that just runs Task.sh? Also, is there some particular reason for Task.sh to behave differently in these situations? Knowing the background might help in finding a suitable solution.
    – ilkkachu
    CommentedOct 3, 2023 at 20:46
  • @ilkkachu The Task.sh copies to clipboard if it's not called from inside the script else it just prints it.
    – Porcupine
    CommentedOct 3, 2023 at 22:48
  • 1
    Why not just import the script as a function into the main.sh? Another way to do it is to have both script source different variables. Have main.sh override the variables from task.sh. This way you would know if the script was run by anther script or not.CommentedOct 4, 2023 at 3:17
  • 1
    Apart from just checking if the output is a terminal, a straightforward solution would be to just look at a command-line argument to decide if to use the clipboard or to print something. I would likely make the default action printing, and then the user could use an alias to turn Task.sh into Task.sh --clipboard, or so. But you could do it the other way around too, since at least the argument needs to be added to the script only once.
    – ilkkachu
    CommentedOct 4, 2023 at 6:30

2 Answers 2

2

One option is to change the requirements slightly from "am I running in a script?" to "am I connected to a terminal or a pipe/file?". This would allow for the case of Task.sh >/tmp/file: it's not called from a script, but it seems it should write to the file rather than the clipboard.

If that's acceptable then you can use a simple test for stdout connected to a terminal:

if [ -t 1 ] then echo "stdout is a terminal (tty)" else echo "not a terminal (tty)" fi 

Tools like ls and tty operate differently depending on their usage situation using a very similar approach. For example, in a directory with several entries contrast ls and ls | cat

    1

    Having a tool copy something to clipboard seems rare enough that it might be better to be explicit about it and only do it when asked.

    So one option is to the script check for a command line argument asking to use the clipboard, and otherwise default to printing to stdout.

    If you don't use command line arguments in the script otherwise, it's as easy as:

    if [ "$1" = --clipboard ]; then copy_to_clipboard "$data" # or whatever else printf "%s\n" "$data" # print to stdout fi 

    or include it in your getopt(s) loop if you are using command line arguments.

    That means an interactive user will need to remember to add the option, but they can just install an alias in their .bashrc or equivalent to do that:

    alias Task.sh="Task.sh --clipboard" 

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.