FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 02-04-2010, 09:09 PM
Dotan Cohen
 
Default Bash question: get output as a variable?

I'm scripting a backup solution, the line that does the business looks
like this:

tar -zcvf - * --exclude-from $EXCLUDES | openssl des3 -salt -k $1 |
dd of=$(hostname)-$(date +%Y%m%d).tbz

Because of the "v" flag tar writes to stdout the name of each file
copied. How can I get that output redirected to a variable, to use
later in the script?

Thanks!

--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il

Please CC me if you want to be sure that I read your message. I do not
read all list mail.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-04-2010, 09:17 PM
Alex Samad
 
Default Bash question: get output as a variable?

On Fri, Feb 05, 2010 at 12:09:28AM +0200, Dotan Cohen wrote:
> I'm scripting a backup solution, the line that does the business looks
> like this:
>
> tar -zcvf - * --exclude-from $EXCLUDES | openssl des3 -salt -k $1 |
> dd of=$(hostname)-$(date +%Y%m%d).tbz
>
> Because of the "v" flag tar writes to stdout the name of each file
> copied. How can I get that output redirected to a variable, to use
> later in the script?

not sure you can, as you are pushing the tar output via stdout as well.
maybe use a fifo to communicate between tar and openssl

Alex

>
> Thanks!
>

--
Winter is the season in which people try to keep the house as warm as
it was in the summer, when they complained about the heat.
 
Old 02-04-2010, 09:32 PM
Stephen Powell
 
Default Bash question: get output as a variable?

On Thu, 4 Feb 2010 17:09:28 -0500 (EST), Dotan Cohen wrote:
> I'm scripting a backup solution, the line that does the business looks
> like this:
>
> tar -zcvf - * --exclude-from $EXCLUDES | openssl des3 -salt -k $1 |
> dd of=$(hostname)-$(date +%Y%m%d).tbz
>
> Because of the "v" flag tar writes to stdout the name of each file
> copied. How can I get that output redirected to a variable, to use
> later in the script?

First of all, let me preface my remarks by saying that I am just
learning shell scripting myself and definitely consider myself a
novice. Some guru out there may (and probably does) know a better
way.

Using a variable is problematic, since a pipeline runs in a subshell
environment. In fact, each stage of the pipeline is a separate
process. Thus, any variables set in a pipeline stage do not
affect the values of the corresponding variable names in the shell
environment that invoked the pipeline.

How about something like this?

tar -zcvf - * --exclude-from $EXCLUDES | tee /tmp/data$$ |
openssl ...
.
. logic to process the /tmp/data$$ data file
.
rm /tmp/data$$



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-04-2010, 09:42 PM
Javier Barroso
 
Default Bash question: get output as a variable?

On Thu, Feb 4, 2010 at 11:32 PM, Stephen Powell <zlinuxman@wowway.com> wrote:
> On Thu, 4 Feb 2010 17:09:28 -0500 (EST), Dotan Cohen wrote:
>> I'm scripting a backup solution, the line that does the business looks
>> like this:
>>
>> tar -zcvf - * *--exclude-from $EXCLUDES *| openssl des3 -salt -k $1 |
>> dd of=$(hostname)-$(date +%Y%m%d).tbz
>>
>> Because of the "v" flag tar writes to stdout the name of each file
>> copied. How can I get that output redirected to a variable, to use
>> later in the script?
>
> First of all, let me preface my remarks by saying that I am just
> learning shell scripting myself and definitely consider myself a
> novice. *Some guru out there may (and probably does) know a better
> way.
>
> Using a variable is problematic, since a pipeline runs in a subshell
> environment. *In fact, each stage of the pipeline is a separate
> process. *Thus, any variables set in a pipeline stage do not
> affect the values of the corresponding variable names in the shell
> environment that invoked the pipeline.
>
> How about something like this?
>
> * tar -zcvf - * --exclude-from $EXCLUDES | tee /tmp/data$$ |
> * openssl ...
In this case output goes to stderr, so:

tar -zcvf - * --exclude-from $EXCLUDES 2> /tmp/data$$ | openssl ...


> * .
> * . logic to process the /tmp/data$$ data file
> * .
> * rm /tmp/data$$
>
>
>
> --
> To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
>
>


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-04-2010, 11:06 PM
Stephen Powell
 
Default Bash question: get output as a variable?

On Thu, 4 Feb 2010 17:42:45 -0500 (EST), Javier Barroso wrote:
> In this case output goes to stderr, so:
>
> tar -zcvf - * --exclude-from $EXCLUDES 2> /tmp/data$$ | openssl ...

Is that something you just have to find out by trial and error?
I checked the man page for tar, and there's nothing in there about
the -v output being written to stderr. I'll take your word for it,
but in the general case, it's hard to tell. Since stdout and
stderr both default to the terminal, and since the doc doesn't
say, how else would you know other than by trial and error?


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-05-2010, 06:20 AM
Javier Barroso
 
Default Bash question: get output as a variable?

On Fri, Feb 5, 2010 at 1:06 AM, Stephen Powell <zlinuxman@wowway.com> wrote:
> On Thu, 4 Feb 2010 17:42:45 -0500 (EST), Javier Barroso wrote:
>> In this case output goes to stderr, so:
>>
>> tar -zcvf - * --exclude-from $EXCLUDES 2> /tmp/data$$ | openssl ...
>
> Is that something you just have to find out by trial and error?
> I checked the man page for tar, and there's nothing in there about
> the -v output being written to stderr. *I'll take your word for it,
> but in the general case, it's hard to tell. *Since stdout and
> stderr both default to the terminal, and since the doc doesn't
> say, how else would you know other than by trial and error?
If you are using stdout as tar output, including filenames there will
corrupt that output, so it is logical that in this case filenames goes
to stderr.

Sorry my bad english, I hope you understand my opinion


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-05-2010, 06:59 AM
Alexey Salmin
 
Default Bash question: get output as a variable?

On Fri, Feb 5, 2010 at 1:20 PM, Javier Barroso <javibarroso@gmail.com> wrote:
> On Fri, Feb 5, 2010 at 1:06 AM, Stephen Powell <zlinuxman@wowway.com> wrote:
>> On Thu, 4 Feb 2010 17:42:45 -0500 (EST), Javier Barroso wrote:
>>> In this case output goes to stderr, so:
>>>
>>> tar -zcvf - * --exclude-from $EXCLUDES 2> /tmp/data$$ | openssl ...
>>
>> Is that something you just have to find out by trial and error?
>> I checked the man page for tar, and there's nothing in there about
>> the -v output being written to stderr. *I'll take your word for it,
>> but in the general case, it's hard to tell. *Since stdout and
>> stderr both default to the terminal, and since the doc doesn't
>> say, how else would you know other than by trial and error?
> If you are using stdout as tar output, including filenames there will
> corrupt that output, so it is logical that in this case filenames goes
> to stderr.
>
> Sorry my bad english, I hope you understand my opinion
>

That's true: programs using stdout for data output certainly have to
use stderr as a way to report any additional info.

Alexey


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-05-2010, 07:03 AM
Brian Ryans
 
Default Bash question: get output as a variable?

Quoting Stephen Powell on 2010-02-04 18:06:58:
> but in the general case, it's hard to tell. Since stdout and
> stderr both default to the terminal, and since the doc doesn't
> say, how else would you know other than by trial and error?

Trial and error is an effective way to figure it out. [1] Depending on a
few factors though, it may be more instructive long-term to read the
source. I've recently been putting that philosophy to use in various
things where the documentation's not quite clear. Both methods have
their merits.

[1] possibly redirect std{out,err} to separate temp files and view them?

--
_ Brian Ryans 8B2A 54C4 E275 8CFD 8A7D 5D0B 0AD0 B014 C112 13D0 .
( ) ICQ UIN: 43190205 | Mail/MSN/Jabber: BrianLRyans@gmail.com ..:
X ASCII Ribbon Campaign Against HTML mail and v-cards: asciiribbon.org
/ Any technology distinguishable from magic is insufficently advanced.
 
Old 02-05-2010, 07:07 AM
Ken Teague
 
Default Bash question: get output as a variable?

On Thu, Feb 4, 2010 at 2:09 PM, Dotan Cohen <dotancohen@gmail.com> wrote:
> I'm scripting a backup solution, the line that does the business looks
> like this:
>
> tar -zcvf - * *--exclude-from $EXCLUDES *| openssl des3 -salt -k $1 |
> dd of=$(hostname)-$(date +%Y%m%d).tbz
>
> Because of the "v" flag tar writes to stdout the name of each file
> copied. How can I get that output redirected to a variable, to use
> later in the script?

You probably want to put the data into an array rather than a
variable. An explanation of how to do so can be found here:
http://tldp.org/LDP/abs/html/arrays.html

Scroll down to the end of Example 26-7 (just about Example 26-8).
Basically, direct stdout to a file, then cat the file to add it to an
array.

<snip>
The array=( element1 element2 ... elementN ) initialization
operation, with the help of command substitution, makes it possible to
load the contents of a text file into an array.

#!/bin/bash

filename=sample_file

# cat sample_file
#
# 1 a b c
# 2 d e fg


declare -a array1

array1=( `cat "$filename"`) # Loads contents
# List file to stdout #+ of $filename into array1.
#
# array1=( `cat "$filename" | tr '
' ' '`)
# change linefeeds in file to spaces.
# Not necessary because Bash does word splitting,
#+ changing linefeeds to spaces.

echo ${array1[@]} # List the array.
# 1 a b c 2 d e fg
#
# Each whitespace-separated "word" in the file
#+ has been assigned to an element of the array.

element_count=${#array1[*]}
echo $element_count # 8
<snip>


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 02-05-2010, 07:39 AM
bruno
 
Default Bash question: get output as a variable?

Ken Teague wrote:

On Thu, Feb 4, 2010 at 2:09 PM, Dotan Cohen <dotancohen@gmail.com> wrote:


I'm scripting a backup solution, the line that does the business looks
like this:

tar -zcvf - * --exclude-from $EXCLUDES | openssl des3 -salt -k $1 |
dd of=$(hostname)-$(date +%Y%m%d).tbz

Because of the "v" flag tar writes to stdout the name of each file
copied. How can I get that output redirected to a variable, to use
later in the script?



You probably want to put the data into an array rather than a
variable. An explanation of how to do so can be found here:
http://tldp.org/LDP/abs/html/arrays.html

Scroll down to the end of Example 26-7 (just about Example 26-8).
Basically, direct stdout to a file, then cat the file to add it to an
array.

<snip>
The array=( element1 element2 ... elementN ) initialization
operation, with the help of command substitution, makes it possible to
load the contents of a text file into an array.

#!/bin/bash

filename=sample_file

# cat sample_file
#
# 1 a b c
# 2 d e fg


declare -a array1

array1=( `cat "$filename"`) # Loads contents
# List file to stdout #+ of $filename into array1.
#
# array1=( `cat "$filename" | tr '
' ' '`)
# change linefeeds in file to spaces.
# Not necessary because Bash does word splitting,
#+ changing linefeeds to spaces.

echo ${array1[@]} # List the array.
# 1 a b c 2 d e fg
#
# Each whitespace-separated "word" in the file
#+ has been assigned to an element of the array.

element_count=${#array1[*]}
echo $element_count # 8
<snip>




Why not simply use the t option for content listing :

tar tvf * --exclude-from $EXCLUDES

Bruno



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 

Thread Tools




All times are GMT. The time now is 03:17 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org