23

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
1
  • 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 5, 2016 at 18:49

1 Answer 1

43

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.

3
  • Thank you! that is what I tried; I just wanted to make sure it did the same thing.
    – flyingace
    Oct 5, 2016 at 18:53
  • @flyingace you could also consider changing your #! line to reference bash instead of sh. Oct 5, 2016 at 19:12
  • Using dash, huh? I haven't heard of that shell before... Feb 28, 2018 at 17:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .