LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-06-2014, 07:21 AM   #1
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Rep: Reputation: Disabled
A brief shell script.The result is not what I wanted.


#vi example.sh

Code:
#!/bin/sh

yes_no()
{
echo "Is your name $* ?"
while true
do
        echo "enter y/n"
        read x
        case "$x" in
            y ) return 0;;
            n ) return 1;;
            * ) echo "Error,Reenter y/n"
        esac
done
}

echo "Orginal paramenters are $*"

if yes_no "$1"
then
        echo "Hi  $1, nice name"
else
        echo "never mind"

fi
exit 0
execute
Code:
./example.sh Rike Neil
the result is
Code:
Orginal paramenters are Rike Neil
Is your name Rike ?
enter y/n
y
Hi  Rike, nice name
if I enter "y" ,the function "yes_no" would return "0".
but ,why execute the
Code:
then
        echo "Hi  $1, nice name"
,rather than
Code:
else
        echo "never mind"
I can't understand it.
thanks.

Last edited by mirage1993; 03-06-2014 at 07:32 AM.
 
Old 03-06-2014, 07:31 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
It is working as it should. When yes_no returns 0, the if test passes.
 
1 members found this post helpful.
Old 03-06-2014, 07:32 AM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,448

Rep: Reputation: Disabled
Quote:
Originally Posted by mirage1993 View Post
if I enter "y" ,the function "yes_no" would return "0".
but ,why execute the
Code:
then
        echo "Hi  $1, nice name"
.
I can't understand it.
thanks.
Because the function doesn't return the number 0, it returns the status code 0, which is interpreted as "success".

The line if yes_no "$1" ; then <something> ; fi means "if the yes_no command or function (with the $1 parameter) returns the status code 0, do <something>.
 
1 members found this post helpful.
Old 03-06-2014, 08:49 AM   #4
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ser Olmy View Post
Because the function doesn't return the number 0, it returns the status code 0, which is interpreted as "success".

The line if yes_no "$1" ; then <something> ; fi means "if the yes_no command or function (with the $1 parameter) returns the status code 0, do <something>.
by the way,if I want to use "return" to return a number,how can I do.thx

Last edited by mirage1993; 03-06-2014 at 08:51 AM.
 
Old 03-06-2014, 10:15 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,514

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
return does not effectively return a number but rather an exit code. You can use the builtin $? to view the returned code or if you wish to actually return a value and perhaps
assign to a variable then you would need to use echo.

To perhaps help a little with your confusion on the results:

In many languages zero is equivalent to false and any non-zero number interpreted as true.

In linux, the return / exit code works almost opposite in that zero is treated as success and any non-zero value is treated as an error / failure.


Hope that helps a little
 
1 members found this post helpful.
Old 03-06-2014, 10:27 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,508

Rep: Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102
Quote:
Originally Posted by grail View Post
In many languages zero is equivalent to false and any non-zero number interpreted as true.

In linux, the return / exit code works almost opposite in that zero is treated as success and any non-zero value is treated as an error / failure.
If you think of the exit code as a "did an error occur" indication, then it makes sense.
 
1 members found this post helpful.
Old 03-06-2014, 03:28 PM   #7
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,448

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
In many languages zero is equivalent to false and any non-zero number interpreted as true.
On the other hand, just about every OS there is treats a non-zero exit code as an error. Actually, I'm not aware of of any exceptions to that rule.
 
1 members found this post helpful.
Old 03-06-2014, 03:35 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
Quote:
Originally Posted by Ser Olmy View Post
On the other hand, just about every OS there is treats a non-zero exit code as an error. Actually, I'm not aware of of any exceptions to that rule.
Grep exit codes are 0 for found, 1 for not found and 2 for error. I don't consider 1 to be an error in that case.
 
1 members found this post helpful.
Old 03-06-2014, 10:12 PM   #9
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Grep exit codes are 0 for found, 1 for not found and 2 for error. I don't consider 1 to be an error in that case.
hi,I read other books.and the value of return command can be only 1 and 0.Is this right ?

Last edited by mirage1993; 03-07-2014 at 08:09 AM.
 
Old 03-07-2014, 12:32 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,433

Rep: Reputation: 2769Reputation: 2769Reputation: 2769Reputation: 2769Reputation: 2769Reputation: 2769Reputation: 2769Reputation: 2769Reputation: 2769Reputation: 2769Reputation: 2769
That's why you need to read man page, check possible return codes and handle it (as you need it). Actually grep will return 0 if pattern was found and nonzero if not found, so that is usually acceptable.
 
1 members found this post helpful.
Old 03-07-2014, 03:34 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,514

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
Quote:
Originally Posted by Ser Olmy View Post
On the other hand, just about every OS there is treats a non-zero exit code as an error. Actually, I'm not aware of of any exceptions to that rule.
My point was not to say that Linux was different than another OS but that it behaves differently to expected boolean behaviour used by other languages.
 
1 members found this post helpful.
Old 03-07-2014, 06:45 AM   #12
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
return does not effectively return a number but rather an exit code. You can use the builtin $? to view the returned code or if you wish to actually return a value and perhaps
assign to a variable then you would need to use echo.
Hope that helps a little
Click image for larger version

Name:	1.JPG
Views:	12
Size:	52.1 KB
ID:	14900

Can the return command return numeric values? the picture is from beginning linux programming by Neil Matthew and Richard Stones
 
Old 03-07-2014, 07:34 AM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,418
Blog Entries: 12

Rep: Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886
That code works fine using BASH. What shell are you using?

To debug further, if you call that function and then test $? or assign $? to a variable you will see what the return value of that function gives you.

Code:
#!/bin/sh

yes_no()
{
echo "Is your name $* ?"
while true
do
        echo "enter y/n"
        read x
        case "$x" in
            y ) return 0;;
            n ) return 1;;
            * ) echo "Error,Reenter y/n"
        esac
done
}

yes_no $1

answer=$?

echo "Answer was: $answer"

exit
Code:
~/testcode$ yn.sh abc
Is your name abc ?
enter y/n
y
Answer was: 0
~/testcode$ yn.sh abc
Is your name abc ?
enter y/n
n
Answer was: 1
Another thing to do is to turn on some debug by adding set -xv after your !/bin/sh statement, but as I say the code works for me using BASH.
 
1 members found this post helpful.
Old 03-07-2014, 07:56 AM   #14
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
That code works fine using BASH. What shell are you using?

Code:
~/testcode$ yn.sh abc
Is your name abc ?
enter y/n
y
Answer was: 0
~/testcode$ yn.sh abc
Is your name abc ?
enter y/n
n
Answer was: 1
I use Bash(GNU Bourne-Again Shell).you are right .I read some others book and the value of return command can be only 1(failed) and 0(success).is this right?

Last edited by mirage1993; 03-07-2014 at 08:10 AM.
 
Old 03-07-2014, 08:00 AM   #15
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,508

Rep: Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102
Quote:
Originally Posted by mirage1993 View Post
Attachment 14900

Can the return command return numeric values? the picture is from beginning linux programming by Neil Matthew and Richard Stones
You can return any number between 0-255
 
2 members found this post helpful.
  


Reply

Tags
shell script


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
Shell Script to take 2 inputs and process the result shadownet Linux - Newbie 3 05-24-2013 12:58 PM
[SOLVED] Shell Script - Getting an argument from ps result sajjadshahid Linux - Newbie 3 01-10-2012 01:10 AM
Formatting result file using Shell script athreyavc Programming 3 09-22-2008 05:57 AM
I wanted to run my sql program in my shell script? anandv_1234 Linux - Newbie 2 02-25-2008 06:12 PM
Shell script to cd into the result of Find odinsride Programming 5 10-17-2007 10:33 AM

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

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