You are not logged in.

#1 2010-04-15 11:01:38

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Terminate shell function with ctrl-c

Hi everyone,

I'm having a problem with exiting a shell function. Maybe you can help.
What i want is the function to exit when i hit Ctrl-C.
Right now, if i press ^c it terminates only the current command and jumps to the next one.
I thought this would work but it doesn't:

trap '{ echo "Hey, you pressed Ctrl-C.  Time to quit." ; exit 1; }' INT
respectively
trap 'exit' INT

The whole function:

setup () {
echo -n "intial-setup: user1 or user2? "
read answer
case "$answer" in
    user1|u1|"") echo "Proceeding with setup for user1"
    echo "Creating user demian, directories and password"
    useradd -m -G many,groups -s /bin/bash user1 ; passwd user1 ; mkdir /a/few/user1/ /specific/folders
    echo "Downloading and installing packages"
    pacman -Syy many programs ; yaourt -Sy --aur a few more programs
        ;;
    user2|u2|"") echo "Proceeding with setup for user2"
    echo "Creating user user2, directories and password"
    useradd -m -G many,groups -s /bin/bash user2 ; passwd user2 ; mkdir /a/few/user2/ /specific/folders
    echo "Downloading and installing packages"
    pacman -Syy many different programs ; yaourt -Sy --aur a few more programs
        ;;
        *) echo "Unknown parameter. Exiting." 
        ;;
esac
}

Any ideas?

Greets,
demian

Last edited by demian (2010-04-15 13:51:24)


no place like /home
github

Offline

#2 2010-04-16 01:11:58

ldvx
Member
Registered: 2007-01-02
Posts: 33

Re: Terminate shell function with ctrl-c

Why don't you just use the end of transmission character to finalize the function?
EOT = ctrl + d

Last edited by ldvx (2010-04-16 01:12:40)

Offline

#3 2010-04-16 09:01:52

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: Terminate shell function with ctrl-c

Thanks. If I've placed the EOT right in the following example it doesn't work though - meaning it doesn't exit the script.

            ;;
esac
EOT
}

Regards,
demian

Last edited by demian (2010-04-16 09:03:13)


no place like /home
github

Offline

Board footer

Powered by FluxBB