ceez ceez - 4 months ago 12
Bash Question

Bash control flow skips to else no matter what

I'm trying to write a small script to switch branches for me. I have only ever written in bash once long ago. In the following code, the

if
s and
elif
s are skipped, and no matter what I input as
$1
, it always skips to the
else
. Ive looked at various examples and nothing is working, though it should be.

#!/bin/bash

if [ "$1 " == "-p" ]
then
cd ui;
git checkout p_4.1;
cd ../services;
git checkout p_4.1;
cd ../application;
git checkout p_4.1;
cd ..;
elif [ "$1 " == "-b" ]
then
cd ui;
git checkout b_.1;
cd ../services;
git checkout b_.1;
cd ../application;
git checkout b_.1;
cd ..;
elif [ "$1 " == "-g" ]
then
cd ui;
git checkout g_.1;
cd ../services;
git checkout g_.1;
cd ../application;
git checkout g_.1;
cd ..;
elif [ "$1 " == "-j" ]
then
cd ui;
git checkout j_.1;
cd ../services;
git checkout j_.1;
cd ../application;
git checkout j_.1;
cd ..;
else
echo "No Command Known!"
fi


I have absolutely no idea why it isn't working.

NPE NPE
Answer

The spaces inside the double quotes are significant, and all "$1 " should be "$1" (no trailing space).

Comments