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 11-23-2016, 08:33 AM   #1
chy_vak
LQ Newbie
 
Registered: Nov 2016
Posts: 3

Rep: Reputation: Disabled
Script returning unwanted result


Hi linuxuestions,

Here's what I currently have (top bash window) and the result the syntax returns (bottom bash window), image link below.

When a name does not exist, such as "xyz" it returns the appropriate response.

When a name does exist, such as "joel" it echos an unwanted response before providing the correct response..

What am I missing?
Please advise,
Thanks in advance!

Here's the image
 
Old 11-23-2016, 08:39 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Because the grep line needs to go between the if blocks.

ie:

Code:
if 
read blah
fi

grep

if some test
echo failed
fi

Last edited by szboardstretcher; 11-23-2016 at 08:40 AM.
 
Old 11-23-2016, 08:52 AM   #3
chy_vak
LQ Newbie
 
Registered: Nov 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Here's the updated syntax http://imgur.com/a/AcKFf

The issue continues to persist, except now when a name doesnt exist, it doesnt echo "$name not in directory"

 
Old 11-23-2016, 09:00 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Because your exit code test is wrong.

Code:
## This line is saying "if the last exit code is NOT 'a selection was not found'" then print 'not found' ...
## which is the OPPOSITE of what you are saying you want
if [ $? -ne 1 ]; then
 echo "Not found"
fi
https://www.gnu.org/software/grep/ma...it-Status.html
 
Old 11-23-2016, 09:16 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,680

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
To elaborate on szboardstretcher post... From the grep man page
Quote:
EXIT STATUS
The exit status is 0 if selected lines are found, and 1 if not found.
 
Old 11-23-2016, 09:18 AM   #6
chy_vak
LQ Newbie
 
Registered: Nov 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
I got the script to work with this
http://imgur.com/a/86vwp

For some reason, however, the tutorial Im using is telling me that "phone4 is not returning the expected result"

Odd, I cant continue the tutorial without passing this part.
 
Old 11-23-2016, 09:22 AM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Hit up their community or help section to see what it is that they want. Can't help here without specifics. Good luck!

fyi: I made a oneliner so you can see the different grep exit statuses:

Code:
echo asdf > testfile_abc; grep asdf testfile_abc; echo "Found text, exit code: $?"; grep qwerf testfile_abc; echo "Did not find text, exit code: $?"
 
Old 11-23-2016, 10:15 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
In the future, please use the [code][/code] tags instead of screen grabs and links to other pages.

Interestingly, your first solution could have worked with a small tweak:
Code:
name=$1

if [[ -z "$name" ]]
then
  echo -n "Enter a name to search for: "
  read name  # if user presses enter and name is blank this could cause problems
fi

output=$(grep -i "$name" phonebook)

if [[ -z "$output" ]]
then
  echo "Name $name not in directory"
else
  echo "$output"
fi
By moving the 'output' variable down, we negate the fact that the user might not enter a name at the command line. Either time the 'name' variable is blank will cause grep to wait for more input.

You could also place the request for a name in a loop until the user enters something. You may also wish to consider what happens if the user puts unusual characters in for a name.

Last edited by grail; 11-23-2016 at 10:16 AM.
 
Old 11-23-2016, 10:32 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Thanks for typing that out grail. I saw your post with the actual [code] tags, before you fixed it

I suppose if I were to continue making it as small as possible, while avoiding null inputs, I would do it this way:

Code:
name=$1

while [ -z "$name" ]; do
 read -p "Enter a name to search for: " name
done

 grep -i "$name" phonebook > /dev/null && echo "$name found" || echo "$name not found"
 
Old 11-23-2016, 12:32 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Whilst I follow your logic szboardstretcher, the OP wants the output from the phonebook, not just the name entered by the user
 
Old 11-23-2016, 12:37 PM   #11
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Ahh. Missed that. I blame it on the pictures

I guess removing the redirection and the && would do that.

Code:
name=$1
while [ -z "$name" ]; do
 read -p "Enter a name to search for: " name
done
 grep -i "$name" phonebook || echo "$name not found"
 
  


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
am not getting any result when am using 'if', without getting result for below script alavala53 Linux - General 3 10-25-2012 06:00 PM
Beginner NASM arithmetic & unwanted output/result displaytor Programming 1 02-01-2011 02:17 PM
Unwanted dd result pofadda Linux - General 5 02-21-2010 06:23 PM
first cgi script not returning anything on browser linux_cv Linux - Newbie 1 05-28-2009 01:49 AM
m4 sendmail.mc > sendmail.cf generate unwanted result on OpenSolaris johncsl82 Solaris / OpenSolaris 1 09-08-2008 11:25 PM

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

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