21

I am running the script below to automatically download files from a server using lftp. It works except that when it runs I get the error message

trap: SIGINT: bad trap

If I replace SIGINT and SIGTERM with INT and TERM then it does work, but I don't know if it then achieves the same purpose. This is on 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
flyingace
  • 211
  • 1
  • 2
  • 4
  • 1
    What user does this code run as? What happens if someone naughty creates `ln -s /etc/passwd /tmp/$base_name.lock` or equivalent? – thrig Oct 05 '16 at 18:49

1 Answers1

38

Drop the SIG prefix, just input the signal name:

trap "rm -f -- "$lock_file"" INT TERM

Not all shells understand/take the input with the SIG prefix, sh (presumably you are using dash) is one of those.

On the other hand, more feature rich shells like ksh, bash, zsh allow SIG prefix in front of the signal name.

heemayl
  • 54,820
  • 8
  • 124
  • 141