我需要两种方法来终止 bash 脚本的一部分。
计数器达到预定义的数字,或者用户手动强制脚本继续使用计数器当前的值。
具体来说 - 我列出了 USB 驱动器。如果有 15 个,则对它们进行计数的函数将退出并且脚本可以继续。
我的代码看起来有点像这样:
scannew(){
NEW=0
OLD=$NEW
while [ true ]; do
# count the new drives
lsblk -r > drives.new
diff drives.old drives.new | grep disk | cut -d' ' -f 2 | sort > drives.all
NEW=$(wc -l drives.all | cut -d' ' -f1)
echo -en " Detected drives: $NEW \r"
sleep 0.01
if [ "$NEW" -eq "15" ]; then # exit if we reach the limit
break
fi
done
}
# SOME CODE...
lsblk -r > drives.old
scannew & # start live device counter in the background
SCAN_PID=$! # remember it's PID
wait $SCAN_PID 2>/dev/null # wait until it dies
echo "It's on!"
# REST OF THE CODE...
我用该read
命令尝试了各种东西,但结果是,脚本将始终等待读取退出(按 ENTER 后),并且我无法使“15 限制”条件覆盖它。
例如我尝试在函数中使用read -t
而不是:sleep
scannew()
scannew(){
NEW=0
OLD=$NEW
while [ true ]; do
# count the new drives
lsblk -r > drives.new
diff drives.old drives.new | grep disk | cut -d' ' -f 2 | sort > drives.all
NEW=$(wc -l drives.all | cut -d' ' -f1)
echo -en " Detected drives: $NEW \r"
read -t 0.01 -n 1 && break # read instead of sleep
if [ "$NEW" -eq "15" ]; then
break
fi
done
}
但是 - 函数子进程似乎无法访问标准输入,并且使用read -t 0.01 -n 1 < /dev/stdin && break
它也不起作用。
我怎样才能做到这一点?
答案1
首先我要说的是,你可以只是内联您在 中拥有的所有内容scannew
,因为wait
无论如何您都会 ing,除非您打算在脚本中的其他位置再次扫描。这实际上是您担心的呼叫wc
可能会花费太长时间,如果确实如此,您可以终止它。这是一种简单的设置方法,trap
它允许您捕获发送到进程的信号并为其设置自己的处理程序:
#! /usr/bin/env bash
# print a line just before we run our subshell, so we know when that happens
printf "Lets do something foolish...\n"
# trap SIGINT since it will be sent to the entire process group and we only
# want the subshell killed
trap "" SIGINT
# run something that takes ages to complete
BAD_IDEA=$( trap "exit 1" SIGINT; ls -laR / )
# remove the trap because we might want to actually terminate the script
# after this point
trap - SIGINT
# if the script gets here, we know only `ls` got killed
printf "Got here! Only 'ls' got killed.\n"
exit 0
但是,如果您想保留做事的方式,并将scannew
函数作为后台作业运行,则需要做更多的工作。
由于您需要用户输入,因此正确的方法是使用read
,但我们仍然需要脚本在scannew
完成时继续运行,而不仅仅是永远等待用户输入。read
使得这有点棘手,因为在允许s 处理信号bash
之前等待当前命令完成。trap
据我所知,在不重构整个脚本的情况下,唯一的解决方案是放入read
一个while true
循环并使用read -t 1
.这样,该过程总是需要至少一秒钟才能完成,但在像您这样的情况下,您本质上想要运行列出 USB 设备的轮询守护程序,这可能是可以接受的。
#! /usr/bin/env bash
function slow_background_work {
# condition can be anything of course
# for testing purposes, we're just checking if the variable has anything in it
while [[ -z $BAD_IDEA ]]
do
BAD_IDEA=$( ls -laR / 2>&1 | wc )
done
# `$$` normally gives us our own PID
# but in a subshell, it is inherited and thus
# gives the parent's PID
printf "\nI'm done!\n"
kill -s SIGUSR1 -- $$
return 0
}
# trap SIGUSR1, which we're expecting from the background job
# once it's done with the work we gave it
trap "break" SIGUSR1
slow_background_work &
while true
do
# rewinding the line with printf instead of the prompt string because
# read doesn't understand backslash escapes in the prompt string
printf "\r"
# must check return value instead of the variable
# because a return value of 0 always means there was
# input of _some_ sort, including <enter> and <space>
# otherwise, it's really tricky to test the empty variable
# since read apparently defines it even if it doesn't get input
read -st1 -n1 -p "prompt: " useless_variable && {
printf "Keypress! Quick, kill the background job w/ fire!\n"
# make sure we don't die as we kill our only child
trap "" SIGINT
kill -s SIGINT -- "$!"
trap - SIGINT
break
}
done
trap - SIGUSR1
printf "Welcome to the start of the rest of your script.\n"
exit 0
当然,如果您真正想要的是一个监视 USB 设备数量变化的守护进程或其他东西,您应该考虑systemd
哪个可以提供更优雅的东西。
答案2
一个(某种程度上)通用的解决方案,用于运行给定的命令并在用户输入时终止它,否则退出。要点是在某种原始终端模式下执行读取,寻找“any”键,并通过子进程退出时应发送的信号来处理运行程序退出情况SIGCHLD
(假设没有有趣的事情)子进程)。代码和文档(以及最终的测试)。
#ifdef __linux__
#define _POSIX_SOURCE
#include <sys/types.h>
#endif
#include <err.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <termios.h>
#include <unistd.h>
int Flag_UserOkay; // -U
struct termios Original_Termios;
pid_t Child_Pid;
void child_signal(int unused);
void emit_help(void);
void reset_term(void);
int main(int argc, char *argv[])
{
int ch, status;
char anykey;
struct termios terminfo;
while ((ch = getopt(argc, argv, "h?U")) != -1) {
switch (ch) {
case 'U':
Flag_UserOkay = 1;
break;
case 'h':
case '?':
default:
emit_help();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc == 0)
emit_help();
if (!isatty(STDIN_FILENO))
errx(1, "must have tty to read from");
if (tcgetattr(STDIN_FILENO, &terminfo) < 0)
err(EX_OSERR, "could not tcgetattr() on stdin");
Original_Termios = terminfo;
// cfmakeraw(3) is a tad too raw and influences output from child;
// per termios(5) use "Case B" for quick "any" key reads with
// canonical mode (line-based processing) and echo turned off.
terminfo.c_cc[VMIN] = 1;
terminfo.c_cc[VTIME] = 0;
terminfo.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &terminfo);
atexit(reset_term);
signal(SIGCHLD, child_signal);
Child_Pid = fork();
if (Child_Pid == 0) { // child
close(STDIN_FILENO);
signal(SIGCHLD, SIG_DFL);
status = execvp(*argv, argv);
warn("could not exec '%s' (%d)", *argv, status);
_exit(EX_OSERR);
} else if (Child_Pid > 0) { // parent
if ((status = read(STDIN_FILENO, &anykey, 1)) < 0)
err(EX_IOERR, "read() failed??");
kill(Child_Pid, SIGTERM);
} else {
err(EX_OSERR, "could not fork");
}
exit(Flag_UserOkay ? 0 : 1);
}
void child_signal(int unused)
{
// might try to pass along the exit status of the child, but that's
// extra work and complication...
exit(0);
}
void emit_help(void)
{
fprintf(stderr, "Usage: waitornot [-U] command [args ..]\n");
exit(EX_USAGE);
}
void reset_term(void)
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &Original_Termios);
}