0

So i recently asked about the unexpected token fi and fixed those errors. Now I am getting different errors. can some one please read through my script and point out or fix all the errors you see -I am new to bash and not getting half of these errors. For example:

$ composerrun.sh
./composerrun.sh: line 2: checkForComposerJson: command not found
./composerrun.sh: line 27: syntax error near unexpected token `)'
./composerrun.sh: line 27: `           [Nn]*) '

When I run:

#!/bin/bash


# We need to see if the AisisCore/ Directory exists.
# If it doesn't we will try and create it for you.
# Then to be safe - we check if it exists.
function checkForAisisCore {
    if [[-d "AisisCore/"]]
    then 
        return 0;
    else
       read -p "Would you like us to create the directory?" yn,
       case $yn in
           [[Yy]]*) 
               mkdir "AisisCore/";
               if [[-d "AisisCore/"]]
               then
                   return 0;
               else
                   echo "We could not create the directory as you requested";
                   return 1;
               end
               ;;
           [[Nn]]*) 
               return 1;
               ;;
           *) 
               echo "Please put in either yes or no";
       esac
   fi
}

# We check for the aisis-core inside of vender inside of adam.balan.
# If it exists return true.
# If Not, then alert the user and return false.
function checkForVendor(){
  if [[-d "vender/adam.balan/aisis-core/"]]
  then
      return 0;
  else
      echo "Something is wrong. We could not find the vendor/ folder. Please check that you are running this script inside of your theme or project folder.";
      return 1;
  fi
}

# We need to know if composer.json exists.
# If it does we return true.
# If not - we alert the user.
function checkForComposerJson(){
    if [[-f "composer.json"]]
    then
        return 0;
    else
        echo "You need composer.json with appropriate information about AisisCore";
        return 1;
    fi
}

# ================================ [ Core ] ================================

# Core program.
#
# Check if the composer.json and AisisCore/ exist.
# Run composer install.
# Check for vendor folder.
# If all is good - echo Found Vendor and exit. (For now).
if checkForComposerJson && checkForAisisCore
then
    composer install
    if checkForVendor
    then
        echo "Found vendor"; exit;
    fi
fi

I'm sure there's hundreds of errors and issues but I cant see them because I don't know bash. I think I am on the right track though. How ever I could use a second set of eyes on this script to be like "what are you doing ... "



1
  • 3
    Read the Bash Pitfalls page and clean up your whitespace – some of the above is syntax error. Commented Sep 20, 2013 at 19:45

1 Answer 1

3

Declare your functions before using them. Separate case clauses with double semicolons (;;). Prefer [[ and ]] to [ and ] (see this SO discussion), as they can lead to fewer surprises.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok I updated the code example to better reflect this but it still get the same error, minus the ./composerrun.sh: line 2: checkForComposerJson: command not found
The error on line 27 is because after the Yy conditional, bash expects you to add a line with just ;; before you start the Nn conditional. Same goes for just before the wildcard case.
Ok so I managed to add in the ;; where I believe they should go, like you said but now bash hates them ... its like unexpected token ';;'
@LogicLooking You've overreplaced the square brackets. In the case statement, [Yy] represents a pattern that matches either Y or y. In an if statement, [[ ... ]] is a bash conditional expression, which is a more robust alternative to the test/[ command.
@chepner Oh - so the [[]] should be repelaced with [] ?? for the Yy and Nn, if thats the case it still doesnt like the ;;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.