DroidOS
DroidOS

Reputation: 8910

Conditionally execute command in Linux shell script

I use Cordova CLI to create Android APKs on my Ubuntu 16.04 VPS server. Once the APK has been built I copy it to Dropbox on local machine and then install the APK on my Android test devices. I want to use the Dropbox API to upload the APK directly so I avoid the unnecessary 3 way transfer:

Server -> Local Machine -> Dropbox -> Android test device.

The sequence of operations would go like this

BUILD SUCCESSFUL

 Total time: 5.495 secs
 Built the following apk(s): 
 /path/to/app/source/platforms/android/build/outputs/apk/android-debug.apk


No scripts found for hook "after_compile".


No scripts found for hook "after_build".


[36m[phonegap][39m completed 'cordova build android -d --no-telemetry'

The final step - uploading the android apk to my Dropbox should only be done if BUILD SUCCESSFUL is found in the Cordova/Phonegap debug output. I have got everything in place but I am not sure how I should check for BUILD SUCCESSFUL

Here is the pseudocode in the shell script

!# /bin/bash
pgclean;
# pgclean is another shell script that cleans up the Phonegap project in the 
# current folder
pgbuild;
# this rebuilds the APK and saves the detailed debug output to
# /path/to/my/project/debug.txt
# it is debug.txt which would contain BUILD SUCCESSFUL etc

Here is where my knowledge of bash scripts hits the buffers. What I would like to do next:

where $1 is the parameter I pass to the current shell script to provide the name under which the APK should be stored in Dropbox.

Upvotes: 1

Views: 2272

Answers (2)

m47730
m47730

Reputation: 2261

With POSIX every program should exit with a status code: 0 means success, 1 warning, 2 and more error.

You could test if the process build exit with status code 0.

buildprocess
if [ $? -eq 0 ] ; then otherscript ; fi

$? means last status code

or more concise:

buildprocess && otherscript

Upvotes: 3

DroidOS
DroidOS

Reputation: 8910

I eventually ended up doing this

x=$(grep -c "BUILD SUCCESSFUL" /path/to/my/app/debug.txt);
if [ $x -eq 1 ]; then
 moveit $1;
 echo "Good Build";
 exit;
fi;

Upvotes: 0

Related Questions