LinuxQuestions.org
Help answer threads with 0 replies.
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, 12:16 PM   #1
Regnets1
LQ Newbie
 
Registered: Feb 2012
Posts: 15

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, 01:01 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,753
Blog Entries: 1

Rep: Reputation: 238Reputation: 238Reputation: 238
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, 01:21 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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, 03:21 PM   #4
Regnets1
LQ Newbie
 
Registered: Feb 2012
Posts: 15

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


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 02:44 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 07:09 AM
[SOLVED] HTML Form that sends data entered to a bash script as variables simplified Programming 2 12-01-2009 04:35 PM
Running functions in bash script as another user with su pwc101 Programming 5 12-13-2008 10:19 AM
Problem running ATI driver test script chrisjbzero Linux - Newbie 1 03-28-2004 04:23 PM


All times are GMT -5. The time now is 03:05 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration