Find the answer to your Linux question:
Results 1 to 4 of 4
Hello again, I had a second question, but I figured it should be a separate post. I am running a command pretty similar to the one below: Code: sudo -u ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6

    [SOLVED] bash: propogate exit status

    Hello again,

    I had a second question, but I figured it should be a separate post. I am running a command pretty similar to the one below:

    Code:
    sudo -u <user> sh -c "export status=0; source my_job.sh |tee my_log.txt 2>&1; exit \$status"
    In order to get the SUDO to exit the same as my_job.sh, I need to capture the exit status of that part of the command. What I am trying to do is set a variable that can be modified within my_job.sh, and used to exit. However, no matter what I set "status" to in my_job.sh, it exits with "0".

    What can I do differently? Any help would be appreciated!

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Quoting bash man pages concerning source command:
    The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.
    I have not tested through ssh but just in a local shell. If the return status of the last command is assigned to $status, its value can be echoed simply, whether 0 or any other value.

    It seems you should assign the return status of every command in your script to $status and exit after the first failure (This seems quite huge a task if it's a big script.)
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Problem is you're using the exit status of the tee command and not your script. When a pipe is used enviroment variable PIPESTATUS is set. PIPESTATUS is an array. The exit status of the 1st command is in element zero so try:

    exit \$PIPESTATUS[0]

  4. #4
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6
    Exiting with the pipestatus worked! I just had to add braces:

    exit \${PIPESTATUS[0]}

    Thanks for the help!

Posting Permissions

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