Bash/xdotool: Commands work, but not in a script

6,000

When you source the file with ., the commands will run just as if you had entered them in the command line. Thus exit will exit your currently running shell.

To exit the shell from which the script was executed when forking, you need to get the process id of the parent process. You can try running

kill ${PPID}

in the script instead of exit to kill the parent shell (tip: try just echoing the pid first and check which process it corresponds to so you don't kill your WM or something).

If ${PPID} doesn't do it for you, you can also try sending the pid as a parameter to the script, but it depends on how and where it's called.


You said you used urxtvd/urxvtc. With that combination, this script kills the terminal from which it was started:

#!/bin/sh
echo kill in 3
sleep 3
kill -1 ${PPID}

so you should be able to use kill -1 in this way to kill a single urxvtc instance.

Note that if you run this by sourcing, then the urxvtd instance will be $PPID for the currently running terminal, and all terminals will die. You don't want that.

Share:
6,000

Related videos on Youtube

Fruckubus Crunt
Author by

Fruckubus Crunt

Updated on September 18, 2022

Comments

  • Fruckubus Crunt
    Fruckubus Crunt over 1 year

    I am making a bash script for Linux which closes the terminal window if the window loses focus.

    On the command line, I was able to do this:

    termwin=$(xdotool getactivewindow)
    
    while : 
    do 
      if [[ $(xdotool getactivewindow) != $termwin ]]
      then 
        exit 
      fi 
    done
    

    It works typed manually into a terminal, but, if I put it into a script, instead of closing the window when focus is lost, the script simply stops. No error or anything, just back to a prompt.

    I feel like I'm missing something basic.

    EDIT

    After reading this...: See here

    I tried running this as ". test.sh" rather than "./test.sh" and it worked. The link describes the difference in these methods as running the script as a subprocess or as part of the main process, respectively. Can anyone just explain this, and/or modify the script to run successfully with "./" instead of ". ", in the latter creates issues?

    • uzsolt
      uzsolt about 12 years
      My idea is a bit off-topic: do you know about "exotic" window managers? E.g. awesome (awesome.naquadah.org) - you can do this or similar functions easily.
    • Rony
      Rony about 12 years
      @uzsolt, you are to? if me, I am mainly on awesome wm quite some years and now and all.
    • uzsolt
      uzsolt about 12 years
      Yes :) In awesome this is only about 3 lines :) In bash...
    • Rony
      Rony about 12 years
      @uzsolt dear, awesome wm has lua, homebrew widgets are candies :), bashlets is another playground :)
    • Fruckubus Crunt
      Fruckubus Crunt about 12 years
      Please see edit.
    • Daniel Andersson
      Daniel Andersson about 12 years
      Does ./test.sh && exit work for you? Just exit or exit 0 when the condition is met in the script, and exit will be run in the terminal afterwards.
    • Fruckubus Crunt
      Fruckubus Crunt about 12 years
      Oh bloody hell, that's exactly what I had to do. I was too absorbed in the script itself to consider running it like that.
    • Rony
      Rony about 12 years
      scripting is somehow like building pieces as according to nix philosophy. xdotool is so useful. btw, hug@uzolt
    • Rony
      Rony about 12 years
      @uzsolt 2 lines now ;) cheers.
    • uzsolt
      uzsolt about 12 years
      @Rony if you calculate the parts of while it will about five lines. In awesome: you call a signal function to unfocus, call c:kill(), type end and that's all. It's three lines. And you shouldn't run a plus bash script which eats your CPU :) So awesome is the winner ;)
  • Fruckubus Crunt
    Fruckubus Crunt about 12 years
    This yields the sme results for me, it works if typed in manually, and fails
  • Fruckubus Crunt
    Fruckubus Crunt about 12 years
    I am now wondering if "exit" is the problem. Typed into a terminal, 'exit' closes the terminal, but is it possible that "exit" has a different meaning in a script, that stops the script rather than closing the terminal? Or is this even possible?
  • Fruckubus Crunt
    Fruckubus Crunt about 12 years
    Please see edit.
  • Fruckubus Crunt
    Fruckubus Crunt about 12 years
    Dan, the kill PPID method looks good, but fails with my particular terminal. Im using urxvt in a client/daemon setup (urxvtd and urxvtc). It seems that each terminal in this case has the same ppid, and s =o this method doesn't work. What is different is the pty, as found with the tty command. You can directly tell a pty to do something, right? Something like (not working) "exit >>/dev/pts/0"
  • Daniel Andersson
    Daniel Andersson about 12 years
    Refrain from using kill -9. A single kill is enough, and much nicer. kill -9 does nothing nice.
  • Daniel Andersson
    Daniel Andersson about 12 years
    @FruckubusCrunt: Each terminal has the same PPID (parent pid), but when you launch a script from a terminal, inside the script the launching terminal's pid will be PPID, so it sends the signal to the correct pid. However, -1 seems to be needed with urxvtc. I'll update my answer.
  • Rony
    Rony about 12 years
    @DanielAndersson thank you very much for your refreshment and very useful link. I use the -9 because my kill does not kill. I shall look into the matter.
  • Fruckubus Crunt
    Fruckubus Crunt about 12 years
    This is great Dan, thank you. A good learning experience for me. And I certainly could not accept a situation where more terminals would die, as that would defeat the purpose of what I'm doing. So thanks for the solid explanation.