Error reading two input variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error reading two input variables
# 1  
Old 06-30-2013
Question Error reading two input variables

Hello all,

I've been out of programming for awhile so sorry about the stupid, elementary question.

I'm trying to read two inputs and compare them to a list entered as a parameter via the terminal. The script is

Code:
#!/bin/bash
read -p "Enter the numbers" NUM1 NUM2
for VALUE in $@; do
        if [ $VALUE -lt $NUM1 && $VALUE -gt $NUM2 ]; then COUNT=$((COUNT+1)); fi
done
echo There are $COUNT numbers in between.

I execute the script via:

Code:
./script5 5 10 6 12 5 18 10 4 19 21 5 12 18 22

and enter the values "6 10" upon the query. After the command is run, I am given this as the error:

Code:
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'
./script5: line 4: [: missing `]'

What am I missing? A single quote somewhere?

Last edited by Scrutinizer; 06-30-2013 at 02:58 PM.. Reason: code tags
# 2  
Old 06-30-2013
Hi, try:
Code:
if [ $VALUE -lt $NUM1 ] && [ $VALUE -gt $NUM2 ] ...

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-30-2013
Quote:
Originally Posted by Scrutinizer
Hi, try:
Code:
if [ $VALUE -lt $NUM1 ] && [ $VALUE -gt $NUM2 ] ...

That works, but I now receive the error
Code:
Enter the numbers10 20
./script5: line 4: [5: command not found
./script5: line 4: [6: command not found
./script5: line 4: [5: command not found
./script5: line 4: [4: command not found
./script5: line 4: [5: command not found
There are numbers in between.

# 4  
Old 06-30-2013
Quote:
Originally Posted by EnduranceMan
That works, but I now receive the error
Code:
Enter the numbers10 20
./script5: line 4: [5: command not found
./script5: line 4: [6: command not found
./script5: line 4: [5: command not found
./script5: line 4: [4: command not found
./script5: line 4: [5: command not found
There are numbers in between.

To get these errors, you must have omitted the space between [ and $VALUE. Please retry this using the EXACT spacing shown in Scrutinizer's suggestion.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 06-30-2013
Thank you! That seems to have fixed syntax issues.
# 6  
Old 06-30-2013
You can also use
Code:
[ $VALUE -lt $NUM1 -a $VALUE -gt $NUM2 ]

Please note that you need to enter the larger number first if you want your test to succeed (which may not be what you desire) Also note that it is good practice to double quote variable expansions to be safe from surprises like embedded spaces (not applicable in this case of just numbers)
# 7  
Old 06-30-2013
Note regarding the use of -a:
Quote:
The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts using these expressions should be converted to the forms given below. Even though many implementations will continue to support these obsolescent forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations like:

Code:
test "$1" -a "$2"

should be written as:

Code:
test "$1" && test "$2"

to avoid problems if a user supplied values such as $1 set to '!' and $2 set to the null string.[..]
test: application usage
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in reading the input value

echo "Enter the Value : " read value sed '1s:\(.\{6\}\)\(.\{4\}\):\1'$value':' flextran$RUN_DATE-completed.txt > temp.txt mv temp.txt flextran$RUN_DATE-completed.txt on the run time after entering the input value it waits for keystroke and the values is not input to the function The output... (4 Replies)
Discussion started by: rammm
4 Replies

2. Shell Programming and Scripting

Reading from standard input

So, I am new to shell scripting and have a few problems. I know how to read from standard input but I do not know how to really compare it to say, a character. I am trying to compare it to a character and anything exceeding just a character, the user will get an output message, but the program... (7 Replies)
Discussion started by: Bungkai
7 Replies

3. Shell Programming and Scripting

Reading Standard Input

Hello, I am new to scripting. How do I read multiple lines from the command line? I know read reads one line, but if I have to read multiple lines, how should I do? Thanks, Prasanna (4 Replies)
Discussion started by: prasanna1157
4 Replies

4. Shell Programming and Scripting

Reading from input with sed

I am trying to edit a file in shell script using sed. I need to get the input from command line suppose script.sh sed"/s place=/place=california/g" > /root/user/mark.txt echo " place changed " the above code searches for string place in the file mark.txt and replaces with place=... (5 Replies)
Discussion started by: sriki32
5 Replies

5. Shell Programming and Scripting

Reading input files

Okay, so I've looked on here and found some similar things, but not exactly what I am looking for. I am working on creating a script that can back up some files, based on the contents of another file - the configuration file. First file contains the files to back up - we'll call this... (1 Reply)
Discussion started by: pdxwarrior
1 Replies

6. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

7. Shell Programming and Scripting

reading input from a file

I am trying to read input for a C program (that expects input from the user) from a file using the shell command: progname < filename but it seems that the program considers the char '<' as the first input, hence causing an "error" in my program. I checked it with another program and it... (2 Replies)
Discussion started by: nadbar
2 Replies

8. Shell Programming and Scripting

Reading input from user

how do we read input from a user e.g i want to ask a user to enter 6 sets of numbers how do i control information from the user? i have this....... #!/bin/bash echo "Please enter six numbers" read number echo $number >> file1 but this stops after the first number..how can i... (2 Replies)
Discussion started by: vadharah
2 Replies

9. Shell Programming and Scripting

reading from input

Hi guys , As you know normally ' read ' statement waits for the user to press enter and then terminates the input ............. Can anyone of u tell me how do i read a single character from input without waiting for the user to press enter ................ Thanks, Nagesh. (1 Reply)
Discussion started by: nageshrc
1 Replies

10. UNIX for Dummies Questions & Answers

Reading Input in a Script

#!/usr/bin/sh echo "Enter reason:" echo "> \c" read $reason $reason >> access.log This doesnt work for me. Can someone tell me how I would read the input from what the person types, and then append that to the log file? Regards (2 Replies)
Discussion started by: alwayslearningunix
2 Replies
Login or Register to Ask a Question