Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8606157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:01:35+00:00 2026-06-12T03:01:35+00:00

I would like to get an opinion on checking the error of a shell

  • 0

I would like to get an opinion on checking the error of a shell script. The system that I am supporting contains a simple shell script that is run multiple times by another application (a java). The java calls a shell script that performs ftp connection open, moving the local directory, mget command and finally a bye command. It then checks the $? after the bye command.

I know that in the first place this code is suspicious since it should only check the bye command always but it intermittently causes an error. I already ask the UNIX admin to see if there are log error in the system, but they said there is non.

Basically the script general structure is:

sftp -oPort=<port forwarding port> <ip and user> <<==
lcd <some variable>
mget <some variable>
bye

rtn=$?
if [ ${rtn} != 0 ]
then
        exit 1
fi

exit 0

any ideas why the $? returns a non zero at times?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:01:36+00:00Added an answer on June 12, 2026 at 3:01 am

    Reasons for a non-zero exit code:

    1. The lcd command failed and you’re in batch mode
    2. The mget command failed (the glob did not work) and you’re in batch mode
    3. The bye command failed (low probability) and you’re in batch mode
    4. There was no sftp server on the remote end (exit code == 255)

    If you want to deliberately ignore errors from commands being used, then you can prefix the command with - e.g. mget becomes -mget

    An important thing to do when trying to debug this is to capture the output of the command and dump it when the command fails e.g.

    trap 'rm -f /tmp/logfile.$$' EXIT HUP
    sftp -oPort=<port forwarding port> <ip and user> >&/tmp/logfile.$$ <<EOM
    lcd <some variable>
    mget <some variable>
    bye
    EOM
    rtn=$?
    if ((rtn != 0)); then
        cat /tmp/logfile.$$
        exit 1
    fi
    exit 0
    

    The trap deletes the file when the script terminates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get your opinion on as how far to go with
I have a little experience with WCF and would like to get your opinion/suggestion
I would like to get View instance that is used to display specific Preference
I would like to get the stackoverflow community's opinion on the following three design
I would like to get started on a simple project using the Kohana 3
I would like to get an opinion what language is better to use with
I would like to get some opinion regarding these two JMS providers in terms
I have a small problem and I would like to get your opinion. I'm
I would like to get a second opinion on how to handle Exceptions within
I would like to get some opinion about best practice in iOS5 and higher

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.