LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2012, 06:25 PM   #1
birdman16
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: Disabled
Help with IF statements and SPACES


Hey guys, I'm working on a script here and I need a bit of help, the area where I bolded, I want to know how to make the script read spaces as errors.

What I mean is. The user has to enter both their first name and last name, I try testing it out but it doesn't seem to work in my favour, the script is ignoring the fact that im using spaces or not


#!/bin/bash
entername=1
name=""
while [ $entername -le 1 ]
do
echo -n "Enter your first name and last name: "
read name

if [ name = "*_*" ]; then
echo "Hello" $name

entername=2
else
echo "Please ENTER both first and last name"
entername=0

fi
done

~

Last edited by birdman16; 07-10-2012 at 01:07 AM.
 
Old 07-09-2012, 06:50 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I'm having trouble following---please put the code into [code] tags.

2 things jump out at me:

You say you want to test for a space, but the code shows an underscore

I don't know what this does (try it by itself in a terminal):
Code:
echo "Hello" $nameentername=2
 
Old 07-09-2012, 06:58 PM   #3
birdman16
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
sorry, I figured it out, but thank you!
 
Old 07-09-2012, 08:31 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED once you have a solution. Also, nice to advise if you are using the suggested solution or an alternate one and if so to demonstrate
for others to also learn by
 
Old 07-10-2012, 01:06 AM   #5
birdman16
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
How do I mark as solved?

Never mind, ill also edit the script to.

My mistake was

if [ name = "*_*" ]; then
echo "Hello"
$nameentername=2


I didn't put the $ beside name.
 
Old 07-10-2012, 02:36 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It's generally cleaner and more efficient to use a case statement instead in this kind of situation. Also, you might consider using a character class to eliminate all non-alphanumeric characters (or else build your own list of illegal characters inside the bracket expression). Wrapping the prompt in a while+true loop ensures that it will keep asking until you get a legal name.

Code:
while true; do

	read -r -p "Please enter your first name: " firstname

	case $firstname in

		*[^[:alnum:]]*) echo "Name contains illegal characters."
				echo "Please try again."
				continue
		;;

			     *) echo "First name accepted: $firstname"
		   		break
		;;

	esac

done

# do the same for the last name...

Also, when using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression

(I'm specifically referring to your "while [ $entername -le 1 ]" line here.)


Speaking of which, the bash/ksh "[[" extended test can also do complex globbing and regex matches, but note that any special patterns on the right-hand-side need to remain unquoted.


Finally, please note that you were asked to use ***[code][/code] tags*** around your code and data, which preserves formatting and improves readability. Please do not use quote tags, colors, bolding, or other fancy formatting.

Last edited by David the H.; 07-10-2012 at 02:44 AM. Reason: addendum + minor edits & fixes
 
Old 07-10-2012, 03:32 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
How do I mark as solved?
Under Thread Tools at the top of the question.

Also, the following (as written) will not work in bash:
Code:
$nameentername=2
Try it on the command line to see the reply from the shell.
 
  


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] if statements wtruong Programming 1 01-07-2010 03:56 PM
Spaces and escaped spaces pslacerda Linux - Newbie 13 12-20-2008 09:03 AM
if else statements 0.o Programming 7 09-27-2005 02:59 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
if then statements vapor Programming 4 07-16-2003 06:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:50 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