Find the answer to your Linux question:
Results 1 to 4 of 4
I'm a beginner at shell scripts and I need to learn a few more tricks to compete a task I have. I have created a very simple nautilus script to ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Location
    Tukwila, WA
    Posts
    7

    Error traping in nautilus scripts

    I'm a beginner at shell scripts and I need to learn a few more tricks to compete a task I have. I have created a very simple nautilus script to archive some files onto my server. It is simply an SCP command. It does not run in a terminal and it provides no feedback (other than asking for a password). I would like to know that the process finished successfully (or more importantly, if it didn't). How do I trap errors and display them in a gnome dialog box?

    Thanks

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    When you say "Nautilus script", I assume that you mean "Bash script"? Which is to say, it's a shell script, just you're running it from Nautilus.

    Now then: the variable $? holds the return code of the last executed program. So if you check the value of $? just after you run scp, you'll get its return code. From scp's man page:
    Code:
    The scp utility exits 0 on success, and >0 if an error occurs.
    As far as popping up a dialog box, I'm coming up a bit dry. One option would be to write a Tcl/Tk script to do this, but it may be overkill. There's also the option of using dialog: you could end your script with:
    Code:
    exitstat = $?
    if [ $exitstat -eq 0 ]; then
        message=All Good
    else
        message=Error in Running
    fi
    
    xterm -e "dialog --title \"scp Exit Status\" --msgbox \"$message\" 0 0"
    dialog creates an ncurses-based notification, but since we're popping up a new xterm, it should be alright.

    Check "man dialog" for more information.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Location
    Tukwila, WA
    Posts
    7
    I found some other scripts use zenity for handling dialog boxes. I wrote this code:

    Code:
    #!/bin/bash
    <scp command>
    
    exitstat = $?
    
    if [ $exitstat -eq 0 ] then
        zenity --info --text="Archive Successful"
    else
        zenity --info --text="Archive Failed"
    fi
    However, I always get the "Archive Failed" result. I have confirmed that the scp was successful and that the command returned 0:

    Code:
    <scp command>
    exitstat = $?
    zenity --info --text=$exitstat
    All the references I've looked at indicate the if statement is well formed. Could I be missing anything else?

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Code:
    exitstat = $?
    Remove the spaces around the "=" like this:

    Code:
    exitstat=$?
    Regards

Posting Permissions

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