为什么在尝试捕获 SIGINT 信号时会收到错误消息?

fly*_*ace 17 shell trap

我正在运行下面的脚本以使用 lftp 从服务器自动下载文件。它可以工作,但在运行时我收到错误消息

trap: SIGINT: bad trap
Run Code Online (Sandbox Code Playgroud)

如果我用 INT 和 TERM 替换 SIGINT 和 SIGTERM 那么它确实有效,但我不知道它是否达到了同样的目的。这是在 Linux Debian 4.9.2-10 上。

#!/bin/sh
login="login"
pass="password"
host="server.server.com"
remote_dir='~/remote/dir'
local_dir="/local/dir"

base_name="$(basename "$0")"
lock_file="/tmp/$base_name.lock"
trap "rm -f $lock_file" SIGINT SIGTERM
if [ -e "$lock_file" ]
then
    echo "$base_name is running already."
    exit
else
    touch "$lock_file"
    /usr/bin/lftp -p 22 -u "$login","$pass" sftp://"$host" << EOF
    set sftp:auto-confirm yes
    set mirror:use-pget-n 5
    mirror -c -P5 "$remote_dir" "$local_dir"
    quit
EOF
    rm -f "$lock_file"
    trap - SIGINT SIGTERM
    exit
fi
Run Code Online (Sandbox Code Playgroud)

hee*_*ayl 25

去掉SIG前缀,输入信号名即可:

trap "rm -f -- "$lock_file"" INT TERM
Run Code Online (Sandbox Code Playgroud)

并非所有 shell 都理解/接受带有SIG前缀的输入,sh(大概您正在使用dash)就是其中之一。

另一方面,更多功能丰富的外壳,如ksh, bash,zsh允许SIG在信号名称前面添加前缀。