LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-24-2012, 07:36 AM   #1
alavala53
LQ Newbie
 
Registered: Oct 2012
Location: Hyderabad
Posts: 10

Rep: Reputation: Disabled
am not getting any result when am using 'if', without getting result for below script


when i am executing below script usisg if , not getting any result for below script.without 'if' getting result is 1 for below script.
if anybody know, plese post me ans.
str1="GOOd boy"
str2="Bad boy"
if test "$str1" = "$str2"
then
echo $?
fi
 
Old 10-24-2012, 08:14 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
The return code for the test is being sucked in by if so doing the echo of return code in your then is meaningless.

You'd have to do something like:
Code:
str1="GOOd boy"
str2="Bad boy"
if test "$str1" = "$str2"
then echo 0
     exit 0
else echo 1
     exit 1
fi
The echo is literal (0 = true, 1 = false) rather than being something you got from the if itself. The "then" occurs when it is "true" so doing the literal echo there is OK. You can then set the return code (exit status) within the then or within the else so the overall script exits with the return code you want. If you run the above script then run "echo $?" at command line immediately it will show you "1".
 
Old 10-24-2012, 08:27 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Some useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 10-25-2012, 06:00 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
http://mywiki.wooledge.org/BashFAQ/031
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Specific result of awk script jeesun Programming 3 01-24-2010 05:42 AM
Hung script as a result of bg process Poki Linux - Newbie 1 05-01-2009 05:01 PM
suppress the command's result in script ramesh_manu Red Hat 2 02-16-2007 12:02 PM
how to grep within a script, and test result? bobbyr Programming 4 01-13-2004 12:11 PM
no result from running script cowardnewbie Programming 1 09-09-2001 07:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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