0

I want to use a shell script to check if it is interactive. If so, it should spawn a fish and continue the rest of the script in the background. Otherwise, it should not run fish and continue running the rest of the script.

How I check if the shell is interactive:

#!/bin/sh

case $- in
    *i*) echo "interactive" ;;
    *) echo "non-interactive" ;;
esac

Now, I want to spawn a different shell in the foreground if it is interactive, and continue to run the rest of the script in the background.

In this example, it will wait until I exit fish to run echo "continue....

#!/bin/sh

case $- in
    *i*) fish ;;
    *) ;;
esac

echo "continue..."

Things I tried:

  • If I use fish &, the rest of the script runs in the foreground and fish runs in the background.
  • If I use fish, the script runs fish in the foreground, but only continues the rest of the script once the fish shell is exits.
  • If I use exec fish, the script runs fish in the foreground, but does not run the rest of the script, because fish replaces the parent.

Note that I am using dash and not bash for the script, so it should not contain any bashisms.

John Doe
  • 1
  • 1
  • You have to just change the order. Just put the `echo continue...` before `fish` and add a `&`. – KamilCuk Dec 10 '18 at 00:42
  • @KamilCuk in my scenario, `echo "continue..."` represents a command that must be executed *after* the `case` test. – John Doe Dec 10 '18 at 01:32
  • You want to use `&` so it will run in the background so it will run _simultaneously_ (in any order). If you wan t to execute it _after_ the case test, place it before case test. It doesn't work that way, you cannot schedule a background job to run after something. You can synchronize using sleep `( sleep 5; echo continue; ) & case ....` which is the worst way to synchronize. – KamilCuk Dec 10 '18 at 07:36

0 Answers0