LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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,829
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
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: Centos 7.7 (?), Centos 8.1
Posts: 17,735

Rep: Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523
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: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

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


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 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 08:16 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration