CoderBrien
CoderBrien

Reputation: 693

Cygwin bash refuses to exit

I'm running the following script where the glob in the for loop expands to a very large set of files. For some reason the exit due to key press refuses to take... the script prints "detected keypress, exiting" but just keeps going.

I suspect there are subshells being spawned somehow which are sucking up the exit calls but I'm stumped as to how to fix it. The script simply doesn't exit.

#!/bin/sh -e
bindir=`dirname $0`

shopt -s nullglob

dir="$1"

diecygwin=
for complete in "${dir}"/*#complete; do
    if [ ! -z "$diecygwin" ]; then
        exit "$diecygwin"
        continue
    fi
    seq=${complete//#complete/}
    echo -n "${seq} ... "
    rc=0
    $bindir/other.sh -d "$seq" || rc=$?
    if [ $rc -eq 0 ]; then
        echo ok
        read -t 0.5 -n 1 -s holder && key="$holder"
        if [ ! -z "$key" ]; then
            echo detected keypress, exiting
            exit 0
            diecygwin=0
        fi
    elif [ $rc -ne 100 ]; then
        echo fatal error $rc
        exit 1
        diecygwin=1
    fi
done

Upvotes: 0

Views: 1301

Answers (1)

Zuzzuc
Zuzzuc

Reputation: 148

Just a heads up, you are not using bash, you are using sh. sh lacks some of the features bash have. This might be causing troubles. Change your shebang to #!/bin/bash or #!/usr/bin/env bash. On some operating systems /bin/sh points to /bin/bash, but this changed a while back. For more info read Difference between sh and bash

If the childs won't exit, you can kill all children and grandchildrens by

kill -- -$PGID The default signal (TERM = 15)

kill -9 -$PGID SIGKILL (9)

You can retrieve the PGID from any Process-ID (PID) of the same process tree kill -- -$(ps -o pgid= $PID | grep -o '[0-9]*') (signal TERM)

kill -9 -$(ps -o pgid= $PID | grep -o '[0-9]*') (signal KILL)

I also ran your script through https://www.shellcheck.net/ and it returned the following:

Line 2:
bindir=`dirname $0`
       ^-- SC2006: Use $(..) instead of legacy `..`.
                ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 4:
shopt -s nullglob
^-- SC2039: In POSIX sh, 'shopt' is undefined.

Line 14:
    seq=${complete//#complete/}
        ^-- SC2039: In POSIX sh, string replacement is undefined.

Line 15:
    echo -n "${seq} ... "
         ^-- SC2039: In POSIX sh, echo flags are undefined.

Line 17:
    $bindir/other.sh -d "$seq" || rc=$?
    ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 20:
        read -t 0.5 -n 1 -s holder && key="$holder"
        ^-- SC2162: read without -r will mangle backslashes.
             ^-- SC2039: In POSIX sh, read -t is undefined.

EDIT

 if [ ! -z "$diecygwin" ]; then
        exit "$diecygwin"
        continue
 fi

Why is continue in there? It will do nothing. The script will terminate before reaching it.

The same is true for diecygwin=0 in

if [ ! -z "$key" ]; then
    echo detected keypress, exiting
    exit 0
    diecygwin=0
fi

Are you sure the script keeps running? It should terminate once reaching exit. If it doesn't what output does you get once it has passed the exit?

Upvotes: 1

Related Questions