LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 06-10-2004, 12:44 AM   #1
facets
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 2 / Damnsmalllinux
Posts: 18

Rep: Reputation: 0
Scripting : Dual line output into next script as variables


Hey all,

I have a script which outputs a two line answer to stdin :
1
2

I need to be able to pick these up and set variables for another script with them.

Any ideas on how I could achieve this?

Cheers, mark.
 
Old 06-10-2004, 01:29 AM   #2
pcardout
Member
 
Registered: Jun 2003
Location: Socorro, New Mexico
Distribution: Debian ("lenny", "squeeze"), Linux Mint, XUbuntu
Posts: 218

Rep: Reputation: 24
Using output from one script as input for next

I think bash scripts suck for lots of things (I keep thinking to myself a REAL programming language wouldn't make you do this ... Thus Python).

You can still do it though. Maybe the real gurus have a more elegant approach. This works though. It's excerpts from a script
in which I want to do calculations on the output of the date
command (basically timing upload and download across a network on a regular basis).


File=~/temp1 #create a junk temporary file
date +%s >$File #Redirect seconds since 1970 to junk file
#Run some commands that waste time
# Now check the time again
date +%s >$File #Redirect seconds since 1970 to junk file
# Now check the time again
date +%s >$File #Redirect seconds since 1970 to junk file
#So the file 'temp1' now contains three lines. Let's say I want
# to know the difference in time between those three times.
{
read time1
read time2
read time3
} < $File

let delta1=$time2-$time1; let delta1=1000/$delta1
echo 'download 1 KBYTE' $delta1 'bytes/s'
let delta2=$time3-$time2; let delta2=500000/$delta2
 
Old 06-10-2004, 02:28 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
If the first script is going to send data to stdout, and the next script is going to take it as stdin (in other words, there's going to be a pipe in here somewhere), why not just you temporary environment variables?

At the end of the first script:
Code:
temp_transfer_var1=value of output line 1
temp_transfer_var2=value of output line 2

export temp_transfer_var1 temp_transfer_var2
Near the beginning of the second script:
Code:
internal_var1=${temp_transfer_var1}
internal_var2=${temp_transfer_var2}

unset temp_transfer_var1 temp_transfer_var2
==============

Another way is to do a classic "while read" (again, assuming the command structure is: script1 | script2)
Code:
internal_var1=""
internal_var2=""

while read output_line ;
do
  if [ "${internal_var1}x" = "x" ] ; then
    internal_var1=${output_line}
  else
    internal_var2=${output_line}
  fi
done
 
Old 06-10-2004, 02:38 AM   #4
pcardout
Member
 
Registered: Jun 2003
Location: Socorro, New Mexico
Distribution: Debian ("lenny", "squeeze"), Linux Mint, XUbuntu
Posts: 218

Rep: Reputation: 24
More elegant!

I like Dark helmet's export of environment variables. Hadn't figured
out how to do that. I want Dark Helmet to know that I think he's a real space ball, and I mean that in a good way.
 
Old 06-10-2004, 12:36 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Making environment variables is similar to solving a scope-issue in a programming language by making a variable global. It's usually not the best way to go about things, but it's quick. If the environment variables are used, please pick some long/obscure name to avoid potentially trashing a variable put in place by the system. It would be very unwise to assign a new value to HOME or EDITOR for example.

======================

"What's the matter Col. Sanders? Chicken?!?" - Dark Helmet
 
Old 06-10-2004, 02:30 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Assume prog1 outsputs to stdout. prog2 needs two variables - it can read them from stdin.

Code:
prog1 | prog2
first lines of prog2

Code:
#!/bin/bash
read var1 var2
 
Old 06-10-2004, 03:28 PM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
<sigh />

As per the usual, I made things too difficult. It's a theme I really need to reign in on.
 
  


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
server variables in bash scripting basher400 Linux - Newbie 4 04-11-2005 07:04 AM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 08:45 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM
Can I redirect script output to a file without ">> $LOGFILE" at the end of each line davee Linux - General 1 12-19-2003 06:01 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 12:07 PM

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

All times are GMT -5. The time now is 11:41 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration