LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-01-2014, 05:51 AM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209
Blog Entries: 1

Rep: Reputation: Disabled
My function doesn't return any value to main program!!!


There is my code in below, my problem is that I can't echo return value from _menu function and do other commands...
Code:
_menu()
{
PS3="Enter your choice please: "
select choice in "SSH to VPN" "SSH to APN" "SSH to IP" "Exit"
do
  case "$choice" in
      "SSH to VPN")fping -ga 10.8.0.0/24>IPvar;return 1;;
      "SSH to APN")fping -ga 192.168.40.0/24>IPvar;return 2;;
      "SSH to IP")return 3;;
      "Exit")exit 0;;
  esac
done
}
#while :
#do
   chret=$(_menu)
   echo $chret
   case "$chret" in
      1 | 2)
        cat IPvar
        echo "-------------------------------"
        while read LINE
        do
          IP=$LINE
          ssh  $usr@$IP `cmnd`
        done <IPvar
        echo "-------------------------------"
        ;;
      3)"Enter IP adress to ssh: "
        ssh $usr@$IP `cmnd`
        ;;
    esac
 
Old 03-01-2014, 09:17 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Well the usual story, when you place set -xv at the start of your script, what do you notice is (is not) happening?

But the answer to your question is the difference between a return / exit value (ie was the command successful or not) and does the command send any output back from being called.

Example:
Code:
$ ls *.txt
file.txt
$ echo $?
0
$ x=$(ls *.txt)
$ echo $?
0
$ echo $x
file.txt
In your case, if you issue the 'echo $?' after you call _menu, you will get the numbers you were expecting, but as none of your command send any data back, as most is redirected, then your variable
is quite correctly empty.
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Access main program function from library pielas Programming 2 03-10-2010 03:16 AM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
How to change function parameter value and return back to the main shell program Bassam Linux - General 1 01-26-2004 10:02 AM
Getting return value of main() gommo Programming 2 06-16-2002 08:06 PM

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

All times are GMT -5. The time now is 12:58 AM.

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