Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm new to bash scripting, and I just want to do a simple little thing, but everything I read online seems to not work for me!

So I have this script here:

  #!/bin/bash
  if [[ $1 = "32"]]
  then
      mv config.h config64.h
      mv config32.h config.h
      mv Makefile Makefile64
      mv Makefile32 Makefile
      echo "READY FOR 32 BITS!"
  elif [[ $2 = "64" ]]
  then
      mv config.h config32.h
      mv config64.h config.h
      mv Makefile Makefile32
      mv Makefile64 Makefile
      echo "READY FOR 64 BITS!"
  fi

and I get the error:

./switch-bits.sh: line 3: syntax error in conditional expression
./switch-bits.sh: line 4: syntax error near `then'
./switch-bits.sh: line 4: `then'

So my hands are thrown in the air here… what is wrong?

share|improve this question
5  
Apart from the syntax error, did you consider what happens to your config files if you run this script twice with the same parameter? You will end up with two copies of the same file (config and Makefile)! Consider using links instead - that way you always have the two copies! –  Floris Apr 23 '13 at 6:08
add comment

2 Answers

up vote 7 down vote accepted

To elaborate a bit on my comment, I would suggest changing your script as follows:

#!/bin/bash
if [[ $1 = "32" ]]
then
  rm config.h
  ln -s config32.h config.h
  rm Makefile
  ln -s Makefile32 Makefile
  echo "READY FOR 32 BITS!"
elif [[ $1 = "64" ]]
then
  rm config.h
  ln -s config64.h config.h
  rm Makefile
  ln -s Makefile64 Makefile
  echo "READY FOR 64 BITS!"
fi

And a shorter (?cleverer) version:

#!/bin/bash
rm config.h
ln -s config$1.h config.h
rm Makefile
ln -s Makefile$1 Makefile
echo "READY FOR $1 BITS!"

Note - it would be a good idea, if you did the shorter version, to include some error checking - i.e. make sure that the input is either "32" or "64" and not something else. I'll leave that up to you...

share|improve this answer
    
using symbolic links is ingenious! Like I said I'm new to bash scripting so it didn't occur to me, thank you! also, minor thing, and this is a mistake I made in the original script, the $2 comparison to 64 should be a $1 THANKS! –  Ethan Apr 23 '13 at 13:50
    
@Ethan I have fixed the $2 error which I had overlooked. Thanks for spotting! –  Floris Apr 23 '13 at 14:17
add comment

you need a space between "32" and ]]

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.