LinuxQuestions.org
Register a domain and help support LQ
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 04-20-2004, 03:35 AM   #1
pedrosan
LQ Newbie
 
Registered: Apr 2004
Location: Osaka, JAPAN
Posts: 21

Rep: Reputation: 15
Is it possible to return variables from a script?


Hi guys

Need a Hand.

I want to define an array of strings as the output of a script.

Using "exit" or "return", I think I can only return a 0 to 255 number.

I tried to use "export" to define it as an environment variable of the current shell. But when I run my script the shell is closed.

Does anyone know how I could return any kind of data from a script in order to be able to use a line such as the following one?
var=$(my_script)

Thank U for your help

PEDROSAN
 
Old 04-20-2004, 03:42 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Use the output of the script as your data channel.
One way to pass information is to pipe data from one program to other.
Alternatively you can pick ouput to a variable with the syntax you just described.
e.g.
Code:
foo=$(ls)
echo contents of variable foo is $foo
 
Old 04-20-2004, 04:08 AM   #3
pedrosan
LQ Newbie
 
Registered: Apr 2004
Location: Osaka, JAPAN
Posts: 21

Original Poster
Rep: Reputation: 15
Actually, what I am wondering is how I define the output of my script as my data channel.
 
Old 04-20-2004, 05:31 AM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
I'm using magical words here. What I meant was that just echo the stuff you want to be passed to the main program to stdout.
 
Old 04-20-2004, 05:38 AM   #5
pedrosan
LQ Newbie
 
Registered: Apr 2004
Location: Osaka, JAPAN
Posts: 21

Original Poster
Rep: Reputation: 15
This is so easy that I didn't even think about it.
I will try this.

Thank U for the help

Have a nice day.

Here in Japan this is time to go back home

BYE

PEDROSAN
 
Old 04-20-2004, 05:50 AM   #6
brasshopper
LQ Newbie
 
Registered: Apr 2004
Location: Boca Raton, Florida
Distribution: Fedora/Redhat
Posts: 8

Rep: Reputation: 3
Processes are isolated...

What you have to understand is that when a "command" is run in UNIX or Linux, it is highly isolated from its parent.

The rule is that is has no unexpected side effects. In Windows, if you start a command and the command changes your working directory, the working directory of the parent is also changed. But with a proper, isolated process model, the parent must specifically do something to allow the child to affect it.

The export verb in shell writing is actually used to add the variable to the list of variables that are exported to the child.

The typical way that you extract things from a child process is to have the child output them. Then you capture the output in one of several ways.

One way is simply to not start a child process. This is called "sourcing". For example, the login scripts affect the shell process that they run in. They can change the directory, or set variables. This is because the process interprets the script without starting a child process.

You can do the same thing, by prefacing the filename with a separate period.

Thus:

. path/to/file

indicates that the file (which must be in the same script language that it is being sourced into) is to be interpreted right there. Any variables that it sets and perhaps exports are set in the current shell's process, as for aliases and so forth. The directory changes stick.

Another possibility is that you can interpolate the output of the commands into other commands. For example, you can say
#! /bin/sh

echo this file contains $(wc -l < $0) lines and $(wc -c < $0) characters\
for a total of $(($(wc -l < $0) + $(wc -c $c < $0 )))


Which produces the output
this file contains 5 lines and 142 characters for a total of 147

So, how did that work? Well, it runs the wc command 4 rimes, always using the current file as an input. When you run the wc (word count) command, it outputs a number. This number is directly put into the current line, which is going to be arguments to echo. The second time, the output from the wc command is stuck into a mathematical expression, which is indicated by $(( )).

So $( ) brackets a command, and $(( )) brackets a numerical expression. We could also have done the command this way:

#! /bin/sh
a=`wc -l < $0`
b=`wc -c < $0`
echo this file contains $a lines and $b characters\
for a total of $((a+b))

and the output is
this file contains 6 lines and 119 characters for a total of 125

In this case, we use the backtic to bracket the commands that are to be executed. This allows us to grab the output of the command as a variable, which we can then use in other expressions. q#! /bin/sh
declare -a b
b=("$@")
set -- $( wc -lc < $0)
echo ${#b[*]} ${b[*]}
echo "this file (${b[0]}) contains ${1} lines and (${b[1]}) ${2} characters for a total of $(($1+$2))"


which, given this input puts this out:

/tmp/ex1 a b c d e
5 a b c d e
this file (a) contains 7 lines and (b) 183 characters for a total of 190


So, how did THIS work?

We first save the arguments that we are passed - we declare variable b as an array. The syntax I use thereupon makes a separate token for each element of the argument list. The double quotes are essential, as this means that if one of the arguments has characters in $IFS, it will not be subjected to word splitting. The parenthesis in the assignment say. "assign each word to a different element". So we have saved the arguments. The set command is the traditional way of breaking the output of a process by word and assigning it to separate variables, but it reuses the $n positionals, which is why I had to save them in the first place.

You can see the substitutions. This is the most general way to do this, and will work in shells other than bash. You might see it as a leftover from other scripts, which is why I present it. However, I also suggest that if you are only going to run your script under bash, this is the "right" way to do it.

#! /bin/sh
declare -a b
b=($( wc -lc < $0) )
echo "this file contains ${b[0]} lines and ${b[1]} characters for a total of $((b[0]+b[1]))"

this file contains 5 lines and 139 characters for a total of 144

Here, we simply use the ability of the assignment to run a command and break the output of the command into words. Each of those words are assigned to a separate array element. Then you can access those array elements.

This will be the fastest shell script. There are cheap operations in the shell, and expensive ones. The most expensive operations run commands. The less expensive operations just access files from within the shell, and the least expensive operations only access internal shell commands and variables. The last version only runs one external command, as opposed to the four external commands that the first version of this script ran, and that is a pain. For tis scripr it does not matter, but it will, someday. The use of $(( )) for mathematical work rather than expr will help, as will avoiding "if true" for "if [[ 1 ]]" since true, in this context, is an external command.

http://infocom.cqu.edu.au/Units/aut9...uestions/bash/ is worth reading.
 
Old 04-20-2004, 06:51 AM   #7
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 42.1_64+Tumbleweed-KDE, Mint 17.3
Posts: 3,975

Rep: Reputation: Disabled
Thundering applause and standing ovations from the cheap seats. Are you in the teaching professions? Again, well done and thanks, brasshopper .
 
  


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
Return variables in scripts Elec490 Programming 8 08-22-2005 10:10 AM
Do Perl functions usually modify variables, or return modified copies? Why? johnMG Programming 3 02-06-2005 11:22 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM
After some hard time, a script to return your IP frandalla LinuxQuestions.org Member Success Stories 3 01-29-2004 02:15 PM
return key on shell script chupacabra Programming 2 10-22-2002 01:11 PM


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