2016-07-11 46 views
0

我运行以下脚本,在水珠的环路扩展到一个非常大的文件集。由于某些原因,由于按键拒绝退出......脚本打印出“检测到按键,退出”但只是继续前进。Cygwin的庆典拒绝退出

我怀疑有子shell被莫名其妙地产生了被吸干退出通话,但我难倒就如何解决它。脚本根本不会退出。

#!/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 

回答

1

刚抬起头,你不使用bash,您正在使用shsh缺少bash所具有的一些功能。这可能会造成麻烦。改变你的朋友到#!/bin/bash#!/usr/bin/env bash。在一些操作系统上,/ bin/sh指向/ bin/bash,但是这个改变了一阵子。欲了解更多信息阅读Difference between sh and bash

如果孩子的将不会退出,您可以通过

kill -- -$PGID默认的信号杀死所有儿童和grandchildrens(TERM = 15)

kill -9 -$PGID SIGKILL(9)

您可以从同一进程树的任何进程ID(PID)中检索PGID kill -- -$(ps -o pgid= $PID | grep -o '[0-9]*')(信号TERM)

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

我还通过https://www.shellcheck.net/运行脚本,并返回以下:

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. 

编辑

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

为什么continue在那里?它什么都不会做。该脚本将在到达之前终止。

的情况也是如此diecygwin=0

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

确定脚本继续运行?它应该在退出时终止。如果它没有通过出口后获得什么产出?

+0

/bin/sh的是/斌/ bash-它的cygwin的。继续和diecygwin在那里,因为出口不退出。 – CoderBrien

+0

该脚本产生后退出的输出是什么? – Zuzzuc

+0

它保持循环,diecygwin没有设置,但它跳过了一堆文件。 cygwin的错误? – CoderBrien