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-05-2010, 11:01 PM
Ken Teague
 
Default Bash question: get output as a variable?

On Fri, Feb 5, 2010 at 12:42 AM, Mart Frauenlob
<mart.frauenlob@chello.at> wrote:
> tar ... 2> "$filename"
> mapfile array1 < "$filename"
>
> (don't know actually why '-u 2' option for mapfile does not complete for
> me? would make it a 'one-liner'.)
>
> on bash4.0+


This is a great example as well. The only problem is bash4 isn't
readily available to people using lenny. Also, wouldn't he just want
to redirect sdtout and not stderr? I guess it all depends on what he
wants to do with the list.


--
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, 11:02 PM
Bob McGowan
 
Default Bash question: get output as a variable?

Ken Teague wrote:
> On Fri, Feb 5, 2010 at 10:10 AM, Chris Jackson
> <c.jackson@shadowcat.co.uk> wrote:
>> Use $() like you do with the date command. You have to redirect stderr back
>> to stdout, which means running it in a subshell:
>>
>>
>> FILES=$( ( tar -zcvf - * --exclude-from $EXCLUDES | openssl des3 -salt -k
>> $1 | dd of=$(hostname)-$(date +%Y%m%d).tbz ) 2>&1 )
>>
>> It may cause unexpected results if there're spaces in the filenames though.
>
> I think this is the best method I've seen thus far. Putting the data
> into an array should avoid problems with file names containing spaces.
> I don't understand why he would stderr in it, though. I'd like to
> know what the OP plans to do with the list of processed files.
>
>

As noted in some earlier posts, using 'tar -f -' causes the archive tar
creates to be written to stdout.

This means the list of file names printed because of the 'v' option,
must be written to some other file descriptor, else it will corrupt the
archive contents.

The "usual" way for an app to do this is to write to stderr, which is
what 'tar' does.

The tar sdtout is already redirected in the initial pipeline. Putting
the pipeline in parenthesis causes the entire thing to run in a
subshell. The '2>&1' takes the stderr of the subshell and puts it onto
the stdout of the current shell, which is then redirected by the $(...)
into the variable.

--
Bob McGowan


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

On 04.02.2010 23:09, 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?
>
> Thanks!
>

After reading many of the follow-ups, I'd suggest something like that:

#!/bin/bash

# create the archive local first, as if you use 'tar -v' and redirection
# of stderr, you will also catch (possible) error messages of tar, not
# only a file list.

tar -czf archive.tgz

# now list the content with 'tar -t'.
# that ensures that we work only with files, which are in the archive.
# you can put it into an array, or if that's not save enough (as the
# array could become quite big and consume a lot of memory) redirect
# the output to a file.

array=( $(tar -tf archive.tgz) )

# or
tar -tf archive.tgz > file.list

# now do whatever work with the file list, either from the array or
# from the file.

: ...

Best regards

Mart


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

On Fri, Feb 5, 2010 at 7:10 PM, Chris Jackson <c.jackson@shadowcat.co.uk> wrote:
> 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?
>>
>> Thanks!
>>
>
>
> Use $() like you do with the date command. You have to redirect stderr back
> to stdout, which means running it in a subshell:
>
>
> FILES=$( ( tar -zcvf - * *--exclude-from $EXCLUDES *| openssl des3 -salt -k
> $1 | dd of=$(hostname)-$(date +%Y%m%d).tbz ) 2>&1 )
>
> It may cause unexpected results if there're spaces in the filenames though.

If there are spaces in filenames, you can try:

$ n=0; while read l; do files[n]="$l"; ((n++)); done < <((tar -zvcf -
* | openssl > $(hostname)-$(date +%Y%m%d)) 2>&1)

Regards,


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

On 06.02.2010 14:17, Javier Barroso wrote:
> On Fri, Feb 5, 2010 at 7:10 PM, Chris Jackson <c.jackson@shadowcat.co.uk> wrote:
>> 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?
>>>
>>> Thanks!
>>>
>>
>>
>> Use $() like you do with the date command. You have to redirect stderr back
>> to stdout, which means running it in a subshell:
>>
>>
>> FILES=$( ( tar -zcvf - * --exclude-from $EXCLUDES | openssl des3 -salt -k
>> $1 | dd of=$(hostname)-$(date +%Y%m%d).tbz ) 2>&1 )
>>
>> It may cause unexpected results if there're spaces in the filenames though.
>
> If there are spaces in filenames, you can try:
>
> $ n=0; while read l; do files[n]="$l"; ((n++)); done < <((tar -zvcf -
> * | openssl > $(hostname)-$(date +%Y%m%d)) 2>&1)
>
> Regards,
>
>
Warning:

~# read < <(printf "%s
" " foo bar ")
~# printf "'%s'
'" "$REPLY"
' foo bar '
~# read l < <(printf "%s
" " foo bar ")
~# printf "'%s'
'" "$l"
'foo bar'

read strips of leading and trailing spaces.
use the $REPLY variable to avoid that.

rare conditions of newline in filenames is not covered...

Best regards

Mart



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

On 06.02.2010 13:39, Mart Frauenlob wrote:
> On 04.02.2010 23:09, 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?
>>
>> Thanks!
>>
>
> After reading many of the follow-ups, I'd suggest something like that:
>
> #!/bin/bash
>
> # create the archive local first, as if you use 'tar -v' and redirection
> # of stderr, you will also catch (possible) error messages of tar, not
> # only a file list.
>
> tar -czf archive.tgz
>
> # now list the content with 'tar -t'.
> # that ensures that we work only with files, which are in the archive.
> # you can put it into an array, or if that's not save enough (as the
> # array could become quite big and consume a lot of memory) redirect
> # the output to a file.
>

forgot:
oifs=$IFS
IFS='$
'

> array=( $(tar -tf archive.tgz) )
>
> # or
> tar -tf archive.tgz > file.list
>


If filename contains newline the whole thing still is in trouble though.



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

On 06.02.2010 15:43, Mart Frauenlob wrote:
> On 06.02.2010 14:17, Javier Barroso wrote:
>> On Fri, Feb 5, 2010 at 7:10 PM, Chris Jackson <c.jackson@shadowcat.co.uk> wrote:
>>> 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?
>>>>
>>>> Thanks!
>>>>
>>>
>>>
>>> Use $() like you do with the date command. You have to redirect stderr back
>>> to stdout, which means running it in a subshell:
>>>
>>>
>>> FILES=$( ( tar -zcvf - * --exclude-from $EXCLUDES | openssl des3 -salt -k
>>> $1 | dd of=$(hostname)-$(date +%Y%m%d).tbz ) 2>&1 )
>>>
>>> It may cause unexpected results if there're spaces in the filenames though.
>>
>> If there are spaces in filenames, you can try:
>>
>> $ n=0; while read l; do files[n]="$l"; ((n++)); done < <((tar -zvcf -
>> * | openssl > $(hostname)-$(date +%Y%m%d)) 2>&1)
>>
>> Regards,
>>
>>
> Warning:
>

[..]

> read strips of leading and trailing spaces.
> use the $REPLY variable to avoid that.

or by:
IFS=$'
'

while IFS=$'
'; read l [...] ; do ...

>
> rare conditions of newline in filenames is not covered...

>
>


--
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 05:19 AM.

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