Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Syntax error: `]' unexpected


Login or Register 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; 2 Days 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 to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Syntax error near unexpected token

Dears, While executing the below script im getting the error at line 30. Please let me know what changes to be done to fix this. test.sh: line 30: syntax error near unexpected token `done' test.sh: line 30: ` done ' #!/bin/sh # Rev. PA1 # author: eillops # date: 26-04-2018 # #... (1 Reply)
Discussion started by: Kamesh G
1 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Syntax error near unexpected token '('

I tried to execute the code but I got this error ./Array.c: line 9: syntax error near unexpected token '(' ./Array.c: line 9: ' nvals = get_data(a,MAXARRAY);' and #include<stdio.h> #define MAXARRAY 1000 main() { int a, nvals; nvals =... (7 Replies)
Discussion started by: sgradywhite
7 Replies

5. Shell Programming and Scripting

Help with ksh syntax error Unexpected Fi

Issue resolved, thanks (6 Replies)
Discussion started by: dangell82
6 Replies

6. 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

7. UNIX for Advanced & Expert Users

syntax error near unexpected token '{

Hi, I am running the following script through cygwin and getting below mentioned error. ******************************************* #!/bin/sh # constants WORK_DIR="deploy" INFOFILE="deploy.info" INTROFILE="Intro.sh" CMGMT_PKG="com.kintana.cmgmt.deploy" DEPLOY_PREFIX="mitg" ... (2 Replies)
Discussion started by: MandyR
2 Replies

8. 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

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