Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-17-2012, 11:16 AM   #1
LQ Newbie
Registered: Feb 2012
Posts: 26

Rep: Reputation: Disabled
Bash Script problem running If test on user entered variables

I am writing my first bash script (and first LQ post) and need some help. I am trying to have a user enter data into a defined variable and then display that variable in formatted output. I also want to omit output if the user does not enter data into the variable. In my testing, I have used the Enter Key when the scripts asks for data that I want to leave blank.

Most of this is working well. However, my script displays an error in the output when there is data or not in the variable. So my If test needs some help. I have included my code.


echo "Enter the second source network with subnet mask (No CIDR) : "
read SRC_NET_2


if [ $SRC_NET_2 -ne "1" ]
 then echo " access-list acl_pat_$SITE_ID extended permit ip $SRC_NET_2 any"
elif [ $SRC_NET_2 -eq "" ] ; then echo ""
here is the error with a value entered into the variable

./ line 113: [: too many arguments
./ line 115: [: too many arguments

here is the error with the enter key used to skip data entry

./ line 113: [: -ne: unary operator expected
./ line 115: [: -eq: unary operator expected
Old 02-17-2012, 12:01 PM   #2
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Wrap the variable $SRC_NET_2 in double quotes and use string comparison (!= instead of -ne and == instead of -eq). When you just hit the enter key, $SRC_NET_2 "disappears" so the test becomes [ -eq "" ].

Take a look at the man page for the test command for more details.
Old 02-17-2012, 12:21 PM   #3
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196
Debugging the script will be easier regards this sort of error if you use [[ <test expression> ]] instead of [ <test expression> ] for reasons explained here.
Old 02-17-2012, 02:21 PM   #4
LQ Newbie
Registered: Feb 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thanks for your help! It's working like I want it to now!!

The final iteration of the test looks like this:
if [ -z "$SRC_NET_2" ]; then echo ""
elif [ "$SRC_NET_2 != "1" ]
     then echo "access-list acl_pat_$SITE_ID extended permit ip $SRC_NET_2 any"


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Small script to check if what the user entered is an ip or not robertjinx Linux - Server 6 11-30-2010 01:44 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
[SOLVED] HTML Form that sends data entered to a bash script as variables simplified Programming 2 12-01-2009 03:35 PM
Running functions in bash script as another user with su pwc101 Programming 5 12-13-2008 09:19 AM
Problem running ATI driver test script chrisjbzero Linux - Newbie 1 03-28-2004 03:23 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration