bash and return codes

let’s start… the aim is too see the behaviour of the bash when we deal with return/exit codes

spawn-2:trials zeph$ vi pointer.c

#include 

int main(){
        printf("Hello world!!!n");
}

spawn-2:trials zeph$ make pointer
cc pointer.c -o pointer

wow… it compiles, I still remember how to code!🙂

well… I found this online course to refresh my memory

spawn-2:trials zeph$ ./pointer
Hello world!!!

and runs, also, amazing!

spawn-2:trials zeph$ ./pointer && echo ciao
Hello world!!!

&& interconnects the future of both the processes… the second one is executed only if the first one exits correctly (it is usefull when you have a long run and you want to go away for a beer)

spawn-2:trials zeph$ ./pointer || echo ciao
Hello world!!!
ciao

or if you want to be sure that it fails… use the || (logical OR)

let’s modify again the file… and add the proper return code

spawn-2:trials zeph$ vi pointer.c

#include 

int main(){
        printf("Hello world!!!n");
        return 0;
}

spawn-2:trials zeph$ make pointer
cc pointer.c -o pointer
spawn-2:trials zeph$ ./pointer && echo ciao
Hello world!!!
ciao
spawn-2:trials zeph$ ./pointer || echo ciao
Hello world!!!

as you see, this time the behaviour is inverted

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s