Error : [: missing `]'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error : [: missing `]'
# 1  
Old 10-27-2010
Power Error : [: missing `]'

Hi,

I am attempting to test the input value for an integer. And if the value is not an integer, the intent is to complain about it and exit. Only if I can get past the syntax error, life will be full.
Code:
# test input to be a number
[ echo "$1" | egrep -q '^[^0-9]+$'; ] && { echo "Invalid input; Enter an integer..."; exit 2; }

When I run it on the command line, I am getting the following error:
Code:
./aaa: line 4: [: missing `]'
./aaa: line 4: ]: command not found

Can someone please help point out how to fix the code?

thanks.

Last edited by IETF; 10-27-2010 at 02:09 PM..
# 2  
Old 10-27-2010
Why the ';'?

Maybe test does not honor ' ?

I usually say it like this in ksh, which I find more readable:
Code:
if [ "${1#*[^0-9]}" != "$1" ]
then
 echo "Invalid input; Enter an integer..."
 exit 2
fi


Last edited by DGPickett; 10-27-2010 at 02:21 PM..
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 10-27-2010
If I remove the ';', then it seems like sh doesn't know when to stop looking for args to 'egrep'. Here is the output:

Code:
./aaa: line 4: [: missing `]'
egrep: ]: No such file or directory

# 4  
Old 10-27-2010
Alternative using "tr", not tried with egrep.

Code:
[ -z "`echo $1 | tr -d [0-9]`" ] || { echo "Invalid input; Enter an integer..."; echo exit 2; }

# 5  
Old 10-27-2010
methyl/DGPicket,

Thanks for offering the working solutions. It is still a head-scratcher why the original code is not working though. I used the hints from suggested solutions to get the regex match to work.
Code:
[ -z "`echo $1 | egrep '^[0-9]+$'`" ] && { echo "Invalid input; Enter an integer..."; exit 2; }
exit 0

Here is the test output:
Code:
$ ./bbb 10; echo $?
0
$ ./bbb abc10; echo $?
Invalid input; Enter an integer...
2
$ ./bbb abc; echo $?
Invalid input; Enter an integer...
2

thanks.
# 6  
Old 10-27-2010
The original code did not have anything to run the programs between the outer square brackets.
The Shell coughed on the sequence:
Code:
[ echo "$1" |

This User Gave Thanks to methyl For This Post:
# 7  
Old 10-27-2010
Of course, I check for not-number without enduring there is a number, an oversight:
Code:
if [ "$1" = "" -o "${1#*[^0-9]}" != "$1" ]

More readable is a case, a great structure for hanging nice comments, and saving you a grep fork/exec():
Code:
case "$i" in
(+([0-9]))
  good-stuff
  ;;
(*)
  error printout and exit
  ;;
esac


Last edited by DGPickett; 10-27-2010 at 04:23 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Error in ksh script: missing right bracket

I have no idea how to write ksh script, but i'm really in need of help with this. I using fedora 30 and in attempt of runnig attached script i getting those errors, i solved first error by removing excess bracket, but i do not know what should i do with other. Pls sorry for trash post. (8 Replies)
Discussion started by: NullPtr
8 Replies

2. UNIX for Beginners Questions & Answers

Missing argument for option: n Error

I am trying to execute the cli.sh script in another shell script passing arguments and getting the below error. Myscript.sh #!/bin/sh /home/runAJobCli/cli.sh runAJobCli -n $Taskname -t $Tasktype I am passing the below 2 arguments and it giving error ./Myscript.sh T_SAMPLE_TEST MTT... (11 Replies)
Discussion started by: Info_Geek
11 Replies

3. UNIX for Beginners Questions & Answers

Compilation error character is missing-UNIX shell script

Dear Friends, Regarding Compilation error character is missing-unix shell script I am new to unix shell script. My requirement is --I need to find out 3 files in my UBM unix directory,if any one(CMUSER) file is available means,then i need to exit from my unix script, Below is my unix... (2 Replies)
Discussion started by: Joseph2017
2 Replies

4. Shell Programming and Scripting

ERROR: Required Parameters Missing

i have modified a korn shell script to include a parameter and i coded the parameter as pSec=$1 pLoopCnt=$2 wheni run the script it gives me an error $ ksh sfs_pcard_load_file.ksh 30 3 ERROR: Required Parameters Missing. USAGE: sfs_pcard_load_file.ksh please help i think i might... (4 Replies)
Discussion started by: wtolentino
4 Replies

5. Shell Programming and Scripting

[: ']' missing Error is coming

Hi guyz, i m new to UNIX (ksh) shell scripting. while running simple code i m getting error. read opt1 read opt2 if && ]; then echo "Yes" fi while running i m getting the below error. 0 YES ./ifelse.ksh: ' missing ./ifelse.ksh: 0: not found ./ifelse.ksh: 0: not found ... (4 Replies)
Discussion started by: Jonty Immortal
4 Replies

6. Red Hat

Yum - resolving missing dependencies that are not missing

I am trying to install VirtualBox on RHEL 5 but I need the 32 bit version for 32 bit Windows. When I run yum I get the following: sudo yum localinstall /auto/spvtg-it/spvss-migration/Software/VirtualBox-4.3-4.3.2_90405_el6-1.i686.rpm Loaded plugins: fastestmirror Setting up Local Package... (13 Replies)
Discussion started by: gw1500se
13 Replies

7. SuSE

How to resolve missing missing dependencies with opensuse 11.3 and 12.3?

Hello, This is a programming question as well as a suse question, so let me know if you think I should post this in programming. I have an application that I compiled under opensuse 12.2 using g77-3.3/g++3.3. The program compiles and runs just fine. I gave the application to a colleague who... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

8. Shell Programming and Scripting

Missing ] error in csh script

The below script gives error: Missing ] #!/bin/csh set MAX=15 set PATTERN='dtsession' set NUM=`ps -eaf | grep -c $PATTERN` echo "No of dtsession = "$NUM if then echo 'Quota exceeded permissible limit' echo 'sending mail...............' mail hiten.r.chauhan@gmail.com<<EOF ... (2 Replies)
Discussion started by: hiten.r.chauhan
2 Replies

9. Shell Programming and Scripting

expect 'missing close-brace' error

my expect script fails when it tries to send a password that I have that uses {}'s. Hoping I can correct that. It doesn't complain about the %, #, *, /, ? or ~ characters only the {}'s. #!/usr/bin/expect spawn ssh root@ca199 expect dsa {send... (1 Reply)
Discussion started by: phpfreak
1 Replies

10. Shell Programming and Scripting

Bash script error: missing destination file name operand.

Ok, i've been messing around in debian the past few days, setting up programs like subversion, mysql and logrotate. The purpose of this script is to use subversion to backup the binary logs. It runs in the cron every 2 hours or so (although I can't get my script to run properly atm, which is why... (1 Reply)
Discussion started by: cganly
1 Replies
Login or Register to Ask a Question