Syntax error: `]' unexpected

I am getting this error Syntax error: `]' unexpected. Did I do something wrong with elif? Does ksh not like double brackets?

if [[ -s ~/.bashrc && /usr/bin/bash ]]; then
    #echo hi
    source ~/.bashrc;
elif [[ -s ~/.kshrc && /usr/bin/ksh ]]; then
    #echo hi
    source ~/.kshrc;
fi

Your script works flawlessly in bash , so I presume you run it with ksh . man ksh :

BTW, what you are doing is a test for a non-zero string e.g. "/usr/bin/bash", not for the existence of a file or command... Why not test for "executable"?

1 Like

Maybe the current shell does not match the dot rc file shell. Try something like:

if [[ -s ~/.bashrc && $BASH = *bash ]]; then
echo running bash
source ~/.bashrc;
elif [[ -s ~/.kshrc && $SHELL = *ksh ]]; then
echo running ksh
source ~/.kshrc;
else
echo No rc file found for current shell
fi

I am missing something.
By default bash sources a file .bashrc if one exists in the current working for each "new" bash invocation. The same is true for ~.kshrc . Each "new" invocation of ksh sources the .kshrc file in the current working directory if it exists.

Per the man pages for each shell.

An example new: executing a script that has a shebang: #!/bin/bash , same for ksh
So why write a script which does exactly what default behavior of your shell already does for you? Good shell coding practice is to place a shebang on the first line, so it is clear what envoronment the shell requires. If bash is not in the PATH then #!/bin/bash will fail which is what you want.

I am figuring it out as I go. Is testing for executable better?

Thats strange. After a lot of experimenting I found out I have SUPER old version of ksh. I got this to work by breaking them apart.

if [ -s ~/.bashrc ] && [ /usr/bin/bash ]; then
    echo hi
    source ~/.bashrc
fi

After that I found out I can't even use "source". I had to use the dot "." to source the file.

if [ -s ~/.bashrc ] && [ /usr/bin/bash ]; then
    echo hi
    . ~/.bashrc
fi

Why does your code work without not being broken up into single brackets?

What would I need to change to switch to bash?

This is for my .profile file. ksh is my default and I want it to switch to bash.

I gave you a pointer in your previous thread on this subject. Forcing a shell to source the .bashrc file does not turn it into bash. Your default shell when logging in will be in the $SHELL variable (unless somebody knows better). So if you cannot change your default shell to bash try something like this at the end of your .profile:

case "$SHELL" in
*bash) . ./.bashrc ;;
*ksh) 
   for b in /bin/bash /usr/bin/bash /usr/local/bin/bash
   do
      if [ -x "$b" ]
      then
         SHELL=$b
         export SHELL
         exec $SHELL
      fi
   done
   . .kshrc
   ;;
esac
. ./.kshrc ;;
esac

Andrew