Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: bash control-c trap

  1. #11
    Join Date
    Sep 2012
    Posts
    5,230

    Default Re: bash control-c trap

    Quote Originally Posted by lord_valarian View Post
    So, I'm not fully understanding what is going on when I press control-c with a trap and child-process is running.
    SIGINT is delivered to shell that runs the script as well as to child process.

    Otherwise I completely lost track of what you are trying to do, why you post those random code snippets and what we are supposed to do with them, sorry.

  2. #12

    Default Re: bash control-c trap

    The code snips are helpful code that shows similar problems and solutions from a website, job control.

    What is wrong with this code?


    Code:
    #!/bin/bash
    trap_key=false
    
    # capture the interrupt control-c
    trap 'trap_key=controlc' 2
    
    clamscan -r /
    
    if [ trap_key = 'controlc' ]; then
        printf "clamscan interrupted\n"
    else
        printf "clamscan completed
    fi

  3. #13
    Join Date
    Sep 2012
    Posts
    5,230

    Default Re: bash control-c trap

    Quote Originally Posted by lord_valarian View Post
    What is wrong with this code?
    Nothing until you explain what you expect this code to do, what this code actually does and why you think it is wrong.

  4. #14

    Default Re: bash control-c trap

    I want control-c to cancel clamscan and return to the script. The variable controls the print. Prints results if control-c was pressed or not.

    The code snip I included looks like it does that. I have not had chance to test it yet.

  5. #15
    Join Date
    Sep 2012
    Posts
    5,230

    Default Re: bash control-c trap

    Quote Originally Posted by lord_valarian View Post
    I want control-c to cancel clamscan and return to the script.
    This is entirely up to clamscan and shell has no control over it. If clamscan ignores or handles SIGINT, it will continue running. Shell won't get control until clamscan is terminated.

  6. #16

    Default Re: bash control-c trap

    Quote Originally Posted by arvidjaar View Post
    This is entirely up to clamscan and shell has no control over it. If clamscan ignores or handles SIGINT, it will continue running. Shell won't get control until clamscan is terminated.
    I think I know what is happening, thanks to help from here. Control-c is working, but it's causing an error "unexpected EOF" which causes the script to drop to the command line. I'm using a loop which I didn't add into writing my simple script. I forgot to add a control-c flag inside the a loop and need go back over the scanvirus script.


    There are two ways I'v found to catch this error.

    http://redsymbol.net/articles/bash-exit-traps/

    Using signal exit to unmount the drive if the script exits for any reason. The link shows how to use the exit signal to restart a script if it crashes.


    http://stackoverflow.com/questions/6...-in-bash#69808

    Using sig error to trap this error.

    Thanks for the help all.

    I'll have to come back to this problem later. A new error appeared on my SED stream filter line on scanvirus script.

  7. #17

    Default Re: bash control-c trap

    I found an alternative method to fix the problem, using the 'trap function exit'.

    Thanks to all for the help....

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •