skip to Main Content

Good day.

I am testing shell script over my own debian notebook

android-verify.sh: line 14: syntax error near unexpected token `then'
android-verify.sh: line 14: `if [[ ! -z $iopt ]] then { wd=$(pwd) basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptname'

I am still new here. Please advised.

When attempt to include ;

In the following instruction

if [[ ! -z $iopt ]]**;** then { wd=$(pwd) basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptname

scriptname=$(echo -e -n $wd/ && cat /tmp/scriptname); su -c "cp $scriptname /usr/bin/monitor" root && echo "Congratulations! Script Installed, now run monitor Command" || echo$
fi 

This bash terminal return the following messages

android-verify.sh: line 17: syntax error near unexpected token `fi'
android-verify.sh: line 17: `fi '

2

Answers


  1. It appears as though you’re attempting to compose a shell script, but there are a few syntax errors in your code. In particular, you’re absent a semicolon and a line break before the then keyword, and there’s a mispositioned fi at the conclusion. Here’s the rectified version of your code:

    #!/bin/bash
    
    iopt="certain_value"  # Illustrative value for testing
    
    if [[ ! -z $iopt ]]; then
        wd=$(pwd)
        basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptname
        scriptname=$(echo -e -n $wd/ && cat /tmp/scriptname)
        su -c "cp $scriptname /usr/bin/monitor" root && echo "Felicitations! Script Deployed, now execute 'monitor' command" || echo "Installation unsuccessful"
    fi
    

    Modifications applied:

    1. Inserted #!/bin/bash at the outset to specify the shell interpreter.
    2. Added an illustrative value for iopt to ensure the condition is satisfied in the illustration.
    3. Rectified the if clause structure by including a semicolon and a line break before then.
    4. Modified the formatting for improved legibility.
    5. Omitted the mispositioned fi at the conclusion.

    By incorporating these alterations, your script should function as intended. Just substitute "certain_value" with your real condition and confirm that you are executing this script within a bash shell.

    Login or Signup to reply.
  2. Here is the code rewritten with the changes to solve the problem and make it more readable:

    if [[ ! -z $iopt ]] ; then
        wd=$(pwd)
        basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptname
        scriptname=$(echo -e -n $wd/ && cat /tmp/scriptname)
        su -c "cp $scriptname /usr/bin/monitor" root && echo "Congratulations! Script Installed, now run monitor Command" || echo
    fi
    
    • I reformatted the code to make it more obvious what is happening and also be able to discuss line numbers with you. It also helps to allow the shell to show line numbers in case other errors come up.
    • On the first line, you guessed needing a semi-colon before the then which was correct.
    • On the 2nd line with the wd variable, there was no semi-colon before base so the wd variable would have effectively only been set only for the base command and not any later commands.
    • On the 5th line with the three echo’s, the last one had a ‘$’ that was out of place and would have resulted in an error about the command not existing.

    So in summary:

    • Consider re-formatting the code to line breaks to help with readability.
    • Add the ‘;’ between the if test and the then statement
    • Add a ‘;’ or line break between wd and base
    • Change echo$ to echo

    Hope that helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search