LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 02-17-2012, 11:16 AM   #1
Regnets1
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.

Code:
SRC_NET_2=1

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

multi-source_pat(){

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 ""
fi
}
here is the error with a value entered into the variable

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

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

./msb.sh: line 113: [: -ne: unary operator expected
./msb.sh: line 115: [: -eq: unary operator expected
 
Old 02-17-2012, 12:01 PM   #2
weibullguy
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
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
Regnets1
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:
Code:
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"
fi
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:40 PM.

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