assigning (numeric) command output to var tcsh | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

assigning (numeric) command output to var tcsh

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-28-2008
psran psran is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 1 September 2008, 4:35 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
assigning (numeric) command output to var tcsh

Hello,

I'm trying to assign a numeric value that is returned from one of my programs to a variable in tcsh. I want to do:

@ r10 = './my_prog file 35'

where ./my_prog file 35 returns a decimal value, but this doesn't work. How do I achieve the desired result?

Janet
Sponsored Links
    #2  
Old 08-28-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 23 July 2014, 2:24 PM EDT
Location: Out running a Marathon.
Posts: 2,321
Thanks: 68
Thanked 153 Times in 142 Posts
Tools Child returning data to parent

What you are asking is for a child (called) process to return info to parent (master) program. The construct of unix does not allow for this as whatever is learned by the child does not have a way to naturally pass to parent; the reverse is ok, and is often accomplished with an EXPORT command.

Searching this forum will probably provide several creative solutions. Some are:
(a) return the value by way of the exit error parameter of the child back to the parent
(b) have the child write the data to a temporary file that the parent can then read
Sponsored Links
    #3  
Old 08-28-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 23 July 2014, 3:44 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,661
Thanks: 33
Thanked 185 Times in 169 Posts
Hi.

Keeping in mind that:
Quote:
The `@' command permits
numeric calculations to be performed and the result assigned to a vari-
able. Variable values are, however, always represented as (zero or
more) strings.

-- excerpt from man tcsh
We can use the following little script:

Code:
#!/usr/bin/env tcsh

@ result = 31416 * 2
echo $result

exit

As a source to set a variable in a larger script:

Code:
#!/usr/bin/env tcsh

# @(#) s1       Demonstrate script result capture.

echo
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version tcsh
echo

echo " Nonce script:"
cat -n t1

echo
echo " Results:"
set var1 = `./t1`
echo " var1 is $var1"

exit 0

Producing:

Code:
% ./s1

(Versions displayed with local utility version)
tcsh 6.13.00

 Nonce script:
     1  #!/usr/bin/env tcsh
     2
     3  @ result = 31416 * 2
     4  echo $result
     5
     6  exit

 Results:
 var1 is 62832

Note that the odd quotes (`) are backtics, not straight single quotes. See the man page for other details ... cheers, drl

(The usual advice advocating the use of Bourne family shells as opposed to csh family applies here.)
    #4  
Old 08-29-2008
spirtle spirtle is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 23 March 2010, 12:11 PM EDT
Location: Scotland
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
If by "returned" you mean "written to standard output", then simply use backticks to capture the output of my_prog:

Code:
@ r10 = `./my_prog file 35`

Note that a tcsh @ variable must be assigned an integer value.
Sponsored Links
    #5  
Old 09-01-2008
psran psran is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 1 September 2008, 4:35 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks folks...

Thanks for the really helpful posts.

A combination of writing only ints to standard output from my program and the use of backtics has cracked my problem.

Thanks for spending the time to explain & provide a solution too - much appreciated.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assigning expression value to tcsh variables AB10 Shell Programming and Scripting 2 10-19-2010 07:33 AM
Assigning output of a command to variable jeriryan87 Shell Programming and Scripting 9 06-30-2008 04:55 PM
Assigning output of command to a variable in shell sankar reddy Shell Programming and Scripting 6 02-28-2008 02:01 AM
assigning command output to a shell variable kprattip Shell Programming and Scripting 2 07-09-2007 04:01 AM
Assigning output of command to a variable oma04 Shell Programming and Scripting 5 06-27-2006 12:11 PM



All times are GMT -4. The time now is 02:25 AM.