简体   繁体   English

在Linux shell脚本中有条件地执行命令

[英]Conditionally execute command in Linux shell script

I use Cordova CLI to create Android APKs on my Ubuntu 16.04 VPS server. 我使用Cordova CLI在我的Ubuntu 16.04 VPS服务器上创建Android APK。 Once the APK has been built I copy it to Dropbox on local machine and then install the APK on my Android test devices. 构建APK后,我将其复制到本地计算机上的Dropbox,然后在我的Android测试设备上安装APK。 I want to use the Dropbox API to upload the APK directly so I avoid the unnecessary 3 way transfer: 我想使用Dropbox API直接上传APK,以避免不必要的3路转移:

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

The sequence of operations would go like this 操作顺序就是这样的

  • Shell scripts (already written) on the server cleanup the Android source and rebuild the APK 服务器上的Shell脚本(已编写)清理Android源并重建APK
  • This is done with Phonegap/Cordova verbose output on which ensures that a successful build emits the following text at the end 这是通过Phonegap / Cordova详细输出完成的,该输出可确保成功构建在结尾处发出以下文本

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. 最后一步 - 只要在Cordova / Phonegap调试输出中找到BUILD SUCCESSFUL,就应该将android apk上传到我的Dropbox。 I have got everything in place but I am not sure how I should check for BUILD SUCCESSFUL 我已经掌握了一切,但我不确定如何检查BUILD SUCCESSFUL

Here is the pseudocode in the shell script 这是shell脚本中的伪代码

!# /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. 这是我对bash脚本的了解到达缓冲区的地方。 What I would like to do next: 我接下来想做什么:

  • Test debug.txt, above, to ensure that the build is successful 测试上面的debug.txt,以确保构建成功
  • If so call my final shell script 如果这样,请调用我的最终shell脚本

    moveapktodropbox $1 moveapktodropbox $ 1

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. 其中$ 1是我传递给当前shell脚本的参数,以提供APK应存储在Dropbox中的名称。

With POSIX every program should exit with a status code: 0 means success , 1 warning , 2 and more error . 使用POSIX,每个程序都应以状态代码退出: 0表示成功1表示 警告2表示更多错误

You could test if the process build exit with status code 0. 您可以测试进程构建是否以状态代码0退出。

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

$? $? means last status code 表示最后的状态代码

or more concise: 或者更简洁:

buildprocess && otherscript

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM