2

I am looking to run a script that is separate from my parent script in another terminal window while keeping the current window usable.

The reasoning behind this is I wish to allow the user to be able to run a watch script that will monitor directory changes as they are made.

this is my function for this

function watchFunction ()
{
  ./watch.sh &
}    

However this only continues to run in the current window in the background.

I cannot use or install any of the following due to my linux distribution: genone-terminal ; xterm ; screen ; konsole ; terminal or any other installable tools!

Any advice would be great as I am just starting out with bash scripting!

6
  • This is something that is specific to your terminal emulator. For example, xterm -e /path/to/script. Which terminal emulator are you using? (And what distro?)
    – Sparhawk
    Commented Oct 25, 2017 at 22:54
  • @Sparhawk when I echo $TERM it says xterm is currently not installed and to install it. The distro is: Linux Mint 17.3 Rosa Commented Oct 25, 2017 at 23:06
  • $TERM is not your terminal emulator. (e.g. I get xterm-256color even though I'm running Terminator.) What application are you actually launching to start the emulator? Also, you should be able to install any of those alternatives in Mint.
    – Sparhawk
    Commented Oct 25, 2017 at 23:16
  • @Sparhawk the terminal I am using is the /bin/bash terminal but I also have Xfce Terminal. To start the terminal I press ctrl alt + T which brings up the /bin/bash terminal. Also the installing is not the issue unfortunately, its other constraints that are restricting me from installing them. Commented Oct 25, 2017 at 23:30
  • 1
    /bin/bash is the shell, not the terminal. I've posted an answer based on xfce4-terminal.
    – Sparhawk
    Commented Oct 26, 2017 at 1:33

3 Answers 3

4

If you just want to watch the output from the script, you can redirect the output from your script to a file and then watch that file in another window:

# Run the script and log all output to a file
./watch.sh &> /var/log/watch.log &

# Watch the file, possibly in another terminal window
tail -f /var/log/watch.log

In my experience, this behavior (writing to a log file) is pretty typical. I don't recall ever having used a command-line application that started spawning other terminal windows.

That said, if you really want to open a new terminal window from the command-line then that will depend on the terminal application. There is a good post about this on the AskUbuntu StackExchange site:

In particular see this answer. For example, for the Gnome terminal you might use a command such as the following:

gnome-terminal -x sh -c "./watch.sh; bash"

If you want to programmatically determine which terminal application is being used, you might want to refer to the following AskUbuntu post:

The accepted solution there defines the following function:

which_term(){
    term=$(perl -lpe 's/\0/ /g' \
           /proc/$(xdotool getwindowpid $(xdotool getactivewindow))/cmdline)

    ## Enable extended globbing patterns
    shopt -s extglob
    case $term in
        ## If this terminal is a python or perl program,
        ## then the emulator's name is likely the second 
        ## part of it
        */python*|*/perl*    )
         term=$(basename "$(readlink -f $(echo "$term" | cut -d ' ' -f 2))")
         version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
         ;;
        ## The special case of gnome-terminal
        *gnome-terminal-server* )
          term="gnome-terminal"
        ;;
        ## For other cases, just take the 1st
        ## field of $term
        * )
          term=${term/% */}
        ;;
     esac
     version=$(dpkg -l "$term" | awk '/^ii/{print $3}')
     echo "$term  $version"
}
1

As per your comments, you are using xfce4-terminal. From its man page, you can see the following option

−x, −−execute
Execute the remainder of the command line inside the terminal

Hence, you could simply prepend this to ./watch.sh, i.e.

function watchFunction ()
{
  xfce4-terminal -x ./watch.sh &
}  
0

Well, I made that in a script I did. You need to launch a terminal window before executing the command.

xterm -e sh -c path/yo/your/script &;

In the script I wrote, I use something like this:

terminal="xterm"

run () {
    cmd="$terminal -e sh -c $1"
    [ -n "$1" ] && (eval "$cmd") > /dev/null 2>&1 &
}

run $VISUAL example/file/name

That will launch a terminal that will execute your program on it. It will also prevent the first terminal to get filled with stderr/stdout messages from the new window.

Just replace the terminal variable to the one you use/like.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .