Quantcast

Return value of a command in shell script != 0 stops build

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Return value of a command in shell script != 0 stops build

natalie_public
Hi there,

I'm using Jenkins to build a C++ project on Solaris 10.

To set up the environment I'm calling various shell scripts, of which one contains the following code:
--
for P in /usr/contrib/bin /usr/local/bin
do
        cd $P > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
                // XYZ
                echo $P
        fi
done
-- 

The directory "/usr/contrib/bin" does not exist. This makes the return value in line 3 (cd $P > /dev/null 2>&1) negative and Jenkins stops the build because it appears to be "failed".

In the job configuration, section "Build shell" it already says that the build is considered failed if one of the commands returns <> 0.

This script has been used for compilation by many other projects for long time, so I don't really want to change it.

Is there any workaround or solution to this behaviour (except to create this directory)?

Regards,
Natalie
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Return value of a command in shell script != 0 stops build

Sami Tikka
If you begin the build step with a line that starts with #!, Jenkins will assume you want to specify the interpreter instead of using /bin/sh -xe, which is the default. It is the -e switch of the shell which is responsible for aborting the script when a command exits with error.

You could place "#!/bin/sh -x" on the first line (without the quotes). But remember to watch for errors yourself, then.

-- Sami

[hidden email] kirjoitti 14.8.2012 kello 18.49:

> Hi there,
>
> I'm using Jenkins to build a C++ project on Solaris 10.
>
> To set up the environment I'm calling various shell scripts, of which one contains the following code:
> --
> for P in /usr/contrib/bin /usr/local/bin
> do
>        cd $P > /dev/null 2>&1
>        if [ $? -eq 0 ]
>        then
>                // XYZ
>                echo $P
>        fi
> done
> --
>
> The directory "/usr/contrib/bin" does not exist. This makes the return value in line 3 (cd $P > /dev/null 2>&1) negative and Jenkins stops the build because it appears to be "failed".
>
> In the job configuration, section "Build shell" it already says that the build is considered failed if one of the commands returns <> 0.
>
> This script has been used for compilation by many other projects for long time, so I don't really want to change it.
>
> Is there any workaround or solution to this behaviour (except to create this directory)?
>
> Regards,
> Natalie
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Return value of a command in shell script != 0 stops build

Qazwart-2
In reply to this post by natalie_public
Add in a test to see if the director exists before doing the copy:

for P in /usr/contrib/bin /usr/local/bin
do
       [ -d $P ] || continue
       cd $P > /dev/null 2>&1
       if [ $? -eq 0 ]
       then
               // XYZ
               echo $P
       fi
done

This will skip the iteration and go to the top of the for loop if this directory doesn't exist.

--
David Weintraub
[hidden email]

On Aug 14, 2012, at 11:49 AM, [hidden email] wrote:

> Hi there,
>
> I'm using Jenkins to build a C++ project on Solaris 10.
>
> To set up the environment I'm calling various shell scripts, of which one contains the following code:
> --
> for P in /usr/contrib/bin /usr/local/bin
> do
>        cd $P > /dev/null 2>&1
>        if [ $? -eq 0 ]
>        then
>                // XYZ
>                echo $P
>        fi
> done
> --
>
> The directory "/usr/contrib/bin" does not exist. This makes the return value in line 3 (cd $P > /dev/null 2>&1) negative and Jenkins stops the build because it appears to be "failed".
>
> In the job configuration, section "Build shell" it already says that the build is considered failed if one of the commands returns <> 0.
>
> This script has been used for compilation by many other projects for long time, so I don't really want to change it.
>
> Is there any workaround or solution to this behaviour (except to create this directory)?
>
> Regards,
> Natalie
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Return value of a command in shell script != 0 stops build

jenkins wilde
In reply to this post by Sami Tikka
Or you can turn off error checking for a specific command by doing:

set +e
command
set -e

Andy.

On 14 August 2012 17:45, Sami Tikka <[hidden email]> wrote:
If you begin the build step with a line that starts with #!, Jenkins will assume you want to specify the interpreter instead of using /bin/sh -xe, which is the default. It is the -e switch of the shell which is responsible for aborting the script when a command exits with error.

You could place "#!/bin/sh -x" on the first line (without the quotes). But remember to watch for errors yourself, then.

-- Sami

[hidden email] kirjoitti 14.8.2012 kello 18.49:

> Hi there,
>
> I'm using Jenkins to build a C++ project on Solaris 10.
>
> To set up the environment I'm calling various shell scripts, of which one contains the following code:
> --
> for P in /usr/contrib/bin /usr/local/bin
> do
>        cd $P > /dev/null 2>&1
>        if [ $? -eq 0 ]
>        then
>                // XYZ
>                echo $P
>        fi
> done
> --
>
> The directory "/usr/contrib/bin" does not exist. This makes the return value in line 3 (cd $P > /dev/null 2>&1) negative and Jenkins stops the build because it appears to be "failed".
>
> In the job configuration, section "Build shell" it already says that the build is considered failed if one of the commands returns <> 0.
>
> This script has been used for compilation by many other projects for long time, so I don't really want to change it.
>
> Is there any workaround or solution to this behaviour (except to create this directory)?
>
> Regards,
> Natalie

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Return value of a command in shell script != 0 stops build

natalie_public
In reply to this post by natalie_public
Thanks for the many helpful replies!

I decided to use set the option "set +e" before executing the script for the environment variables and to unset it afterward. It works perfectly!

Natalie

 

----- Ursprüngliche Nachricht -----

Von: jenkins wilde

Gesendet: 15.08.12 10:53 Uhr

An: [hidden email]

Betreff: Re: Return value of a command in shell script != 0 stops build


Or you can turn off error checking for a specific command by doing:

 
set +e
command
set -e
 
Andy.

On 14 August 2012 17:45, Sami Tikka <[hidden email]> wrote:
If you begin the build step with a line that starts with #!, Jenkins will assume you want to specify the interpreter instead of using /bin/sh -xe, which is the default. It is the -e switch of the shell which is responsible for aborting the script when a command exits with error.

You could place "#!/bin/sh -x" on the first line (without the quotes). But remember to watch for errors yourself, then.

-- Sami

[hidden email] kirjoitti 14.8.2012 kello 18.49:

> Hi there,
>
> I'm using Jenkins to build a C++ project on Solaris 10.
>
> To set up the environment I'm calling various shell scripts, of which one contains the following code:
> --
> for P in /usr/contrib/bin /usr/local/bin
> do
>        cd $P > /dev/null 2>&1
>        if [ $? -eq 0 ]
>        then
>                // XYZ
>                echo $P
>        fi
> done
> --
>
> The directory "/usr/contrib/bin" does not exist. This makes the return value in line 3 (cd $P > /dev/null 2>&1) negative and Jenkins stops the build because it appears to be "failed".
>
> In the job configuration, section "Build shell" it already says that the build is considered failed if one of the commands returns <> 0.
>
> This script has been used for compilation by many other projects for long time, so I don't really want to change it.
>
> Is there any workaround or solution to this behaviour (except to create this directory)?
>
> Regards,
> Natalie

 

 


 
Loading...