Syntax error: `]' unexpected


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Syntax error: `]' unexpected

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

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


Last edited by rbatte1; 3 Weeks Ago at 11:01 AM..
# 2  
Your script works flawlessly in bash, so I presume you run it with ksh. man ksh:

Quote:
[[ expression ]]
Similar to the test and [ ... ] commands (described later), with the following exceptions:
.
.
.
o The single argument form of test, which tests if the argument has non-zero length, is not valid - explicit operators must be always be used, e.g., instead of [ str ] use [[ -n str ]]
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"?
This User Gave Thanks to RudiC For This Post:
# 3  
Maybe the current shell does not match the dot rc file shell. Try something like:
Code:
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

# 4  
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.
# 5  
Quote:
Originally Posted by RudiC
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"?
I am figuring it out as I go. Is testing for executable better?

Quote:
Originally Posted by rdrtx1
Maybe the current shell does not match the dot rc file shell. Try something like:
Code:
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

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.

Code:
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.

Code:
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?

Quote:
Originally Posted by jim mcnamara
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.
This is for my .profile file. ksh is my default and I want it to switch to bash.
# 6  
Quote:
Originally Posted by cokedude
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:
Code:
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
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error `(' unexpected

I have written this in my script but while running i am getting syntax error `(' unexpected. unload to "$BACKUP_DIR/n_fac_fid-$clliname" select * from n_fac_fid where fac_accesskey in (select fac_accesskey From n_fac_ap_fid where ap_clli="$clliname"); Any help appreciated. (3 Replies)
Discussion started by: suryanmi
3 Replies

2. Shell Programming and Scripting

Syntax error: 'fi' unexpected

unzip file.zip if ] ; then echo "Success" else echo "Some failure." fi ; I tried many time to detect the unzip error, but it keep show the syntax error wherever how I change the syntac. Hope someone can help me fix the issue, thanks. Please use code tags next time for your code and... (5 Replies)
Discussion started by: duncanyy
5 Replies

3. UNIX for Dummies Questions & Answers

Syntax error near unexpected token

hi! just want to seek help on this error: syntax error near unexpected token 'do this is my script # !/bin/sh # for y in 27 25 do exemmlmx -c "ZEEI;" -n XRT$y >> blah done what can be wrong? thanks! (6 Replies)
Discussion started by: engr.jay
6 Replies

4. Shell Programming and Scripting

syntax error: `$' unexpected

Hi all, Am very new to Unix and am currently Involved in Migrating some Shell Scripts from AIX 4 to Solaris 10. While using teh for loop am getting the below error: $ echo $SHELL /usr/bin/ksh $ for file in $(ls *SEBE*) syntax error: `$' unexpected while the same works without issue on... (4 Replies)
Discussion started by: paragkhanore
4 Replies

5. Shell Programming and Scripting

Syntax error near unexpected token `('

Guys , This is an output of my script errored out for "Syntax error near unexpected token `(' " Can someone tell me whats wrong with my script. Below is my original script pasted. #!/bin/bash Script Creation Date 01/21/2010 Author baraghun ... (7 Replies)
Discussion started by: raghunsi
7 Replies

6. UNIX for Advanced & Expert Users

Syntax error near unexpected token

Hi, When I run the below shell script I'm getting the error " syntax error near unexpected token `" Script: REM :: File Name : Refresh_OTL.bat REM :: Parameters : %1 - Region REM :: : %2 - Cube Type REM :: : REM :: Notes : REM ============================== set ENVIRONMENT... (2 Replies)
Discussion started by: tomailraj
2 Replies

7. Shell Programming and Scripting

unexpected syntax error

Hi, i am getting following syntax error ...kingly advice why is it coming ?? #!/bin/bash find . -name "common.log" if ; then echo "1" fi Himnashu@home /bin $ ./a.sh ./a.sh: line 7: syntax error near unexpected token `fi' ./a.sh: line 7: `fi' (9 Replies)
Discussion started by: himvat
9 Replies

8. Shell Programming and Scripting

syntax error at line 28: `(' unexpected

hi can anyone pls look into this....shell script... Pls find the error below: > sh -n tmp tmp: syntax error at line 28: `(' unexpected isql -Usa -S$1 -P`grep $1 dbpassword|cut -d ":" -f3` -w2000 -b<<! set nocount on declare @i int declare @dbname char(6) declare @tmp int if... (10 Replies)
Discussion started by: rajashekar.y
10 Replies

9. Shell Programming and Scripting

sh: syntax error: `...' unexpected???

Hello all, I want to create a script that polls every hour a directory for the existence of a file. The file I look for is a `token` dropped by an external process at the completion of a successful FTP process. I wrote this script `checkfile.ksh`: #!/usr/bin/ksh if ] then mailx... (5 Replies)
Discussion started by: alan
5 Replies

10. Programming

sh: syntax error at line 1: `>' unexpected

I compiled C program under SUN OS sparcv9 ...I had a problem related to SIGBUS which has been resolved by adding an option to the CC compiler which is memory alignement option ..-memalign=1i as I remmber ...after running the program I got the below error please let me KNow more details what should... (2 Replies)
Discussion started by: atiato
2 Replies

Featured Tech Videos