如果指令不起作用,则使用Bash编程

时间:2018-05-19 11:04:46

标签: bash

我试图制作一个小bash程序,以便通过433Mhz发射器控制我的光线,但我的程序中的if子句不起作用。

PROGRAMM:

    #!/bin/sh -x

    if [ "$1"="A" ]; then
            if [ "$2"="off" ] || [ "$2"="0" ] || [ "$2"="OFF" ] ;then
                    /opt/433Utils/RPi_utils/codesend 1364
            else
                    /opt/433Utils/RPi_utils/codesend 1361
            fi
    fi

输出:

    + [ B=A ]
    + [ Lol=off ]
    + /opt/433Utils/RPi_utils/codesend 1364
    sending code[1364]

有人有建议吗?

2 个答案:

答案 0 :(得分:1)

此行似乎不正确。

if [ "$2"="off" ] || [ "$2"="0" ] || [ "$2"="OFF" ] ;then

以下是正确的。

if [ "$2" = "off" ] || [ "$2" = "0" ] || [ "$2" = "OFF" ] ;then

(与if [ "$1"="A" ]; then相同)

[ "$2" = "off" ]相当于test "$2" = "off",它为test命令提供3个参数。

但是test "off"="on"为真,它为test命令提供了1个参数。

答案 1 :(得分:1)

作为set0gut1 points out,问题是测试中public void renderFrame(State state, float[] projectionMatrix) { mSampleAppRenderer.renderVideoBackground(); GLES20.glEnable(GLES20.GL_DEPTH_TEST); isTracking = false; for (int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++) { TrackableResult trackableResult = state.getTrackableResult(tIdx); ImageTarget imageTarget = (ImageTarget) trackableResult .getTrackable(); imageTarget.startExtendedTracking(); isTracking = true; float[] modelViewMatrixVideo = Tool.convertPose2GLMatrix( trackableResult.getPose()).getData(); float[] modelViewProjectionVideo = new float[16]; Matrix44F invTranspMV = SampleMath.Matrix44FTranspose(SampleMath.Matrix44FInverse(Tool.convertPose2GLMatrix(trackableResult.getPose()))); Matrix.translateM(modelViewMatrixVideo, 0, 0f, 0f, 1f); Matrix.rotateM(modelViewMatrixVideo, 0, (float) Math.toDegrees(Math.asin(-invTranspMV.getData()[6])), 0.0f, 0.f, 1.0f); Matrix.multiplyMM(modelViewProjectionVideo, 0, projectionMatrix, 0, modelViewMatrixVideo, 0); GLES20.glEnable(GLES20.GL_BLEND); GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); GLES20.glUseProgram(videoPlaybackShaderID); GLES20.glVertexAttribPointer(videoPlaybackVertexHandle, 3, GLES20.GL_FLOAT, false, 0, quadVertices); GLES20.glVertexAttribPointer(videoPlaybackTexCoordHandle, 2, GLES20.GL_FLOAT, false, 0, fillBuffer(videoQuadTextureCoordsTransformedStones)); GLES20.glEnableVertexAttribArray(videoPlaybackVertexHandle); GLES20.glEnableVertexAttribArray(videoPlaybackTexCoordHandle); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, videoPlaybackTextureID); GLES20.glUniformMatrix4fv(videoPlaybackMVPMatrixHandle, 1, false, modelViewProjectionVideo, 0); GLES20.glDrawElements(GLES20.GL_TRIANGLES, NUM_QUAD_INDEX, GLES20.GL_UNSIGNED_SHORT, quadIndices); GLES20.glDisableVertexAttribArray(videoPlaybackVertexHandle); GLES20.glDisableVertexAttribArray(videoPlaybackTexCoordHandle); GLES20.glUseProgram(0); GLES20.glDisable(GLES20.GL_BLEND); SampleUtils.checkGLError("VideoPlayback renderFrame"); } GLES20.glDisable(GLES20.GL_DEPTH_TEST); Renderer.getInstance().end(); } 周围缺少空格。无论=的值如何,"$1"="A"之类的字符串始终为 true

建议更整洁的代码:

$1

或者,对于DRY变体,

#!/bin/sh -x

if [ "$1" = "A" ]; then
    case "$2" in
        off|OFF|0) /opt/433Utils/RPi_utils/codesend 1364 ;;
        *)         /opt/433Utils/RPi_utils/codesend 1361 ;;
    esac
fi

,或者

#!/bin/sh -x

if [ "$1" = "A" ]; then
    case "$2" in
        off|OFF|0) arg=1364 ;;
        *)         arg=1361 ;;
    esac
    /opt/433Utils/RPi_utils/codesend "$arg"
fi

...除非它以某种方式干扰脚本中的其他实用程序。