Re: Bash question: get output as a variable?

2010-02-06 Thread Mart Frauenlob
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-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-06 Thread Javier Barroso
On Fri, Feb 5, 2010 at 7:10 PM, Chris Jackson c.jack...@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 ) 21 )

 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)) 21)

Regards,


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



Re: Bash question: get output as a variable?

2010-02-06 Thread Mart Frauenlob
On 06.02.2010 14:17, Javier Barroso wrote:
 On Fri, Feb 5, 2010 at 7:10 PM, Chris Jackson c.jack...@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 ) 21 )

 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)) 21)
 
 Regards,
 
 
Warning:

~# read  (printf %s\n  foo bar )
~# printf '%s'\n' $REPLY
' foo bar '
~# read l  (printf %s\n  foo bar )
~# printf '%s'\n' $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-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-06 Thread Mart Frauenlob
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='$\n'

 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-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-06 Thread Mart Frauenlob
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.jack...@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 ) 21 )

 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)) 21)

 Regards,


 Warning:
 

[..]

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

or by:
IFS=$'\n'

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

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

 
 


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Alexey Salmin
On Fri, Feb 5, 2010 at 1:20 PM, Javier Barroso javibarr...@gmail.com wrote:
 On Fri, Feb 5, 2010 at 1:06 AM, Stephen Powell zlinux...@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-requ...@lists.debian.org
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-05 Thread Ken Teague
On Thu, Feb 4, 2010 at 2:09 PM, Dotan Cohen dotanco...@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 '\n' ' '`)
#change linefeeds in file to spaces.
#  Not necessary because Bash does word splitting,
#+ changing linefeeds to spaces.

echo ${arra...@]}# 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-requ...@lists.debian.org
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-05 Thread bruno

Ken Teague wrote:

On Thu, Feb 4, 2010 at 2:09 PM, Dotan Cohen dotanco...@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 '\n' ' '`)
#change linefeeds in file to spaces.
#  Not necessary because Bash does word splitting,
#+ changing linefeeds to spaces.

echo ${arra...@]}# 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-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org




Re: Bash question: get output as a variable?

2010-02-05 Thread Mart Frauenlob
On 05.02.2010 09:07, Ken Teague wrote:
 On Thu, Feb 4, 2010 at 2:09 PM, Dotan Cohen dotanco...@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
 
 #!/bin/bash

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+

regards

Mart


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Stephen Powell
On Fri, 5 Feb 2010 02:20:48 -0500 (EST), Javier Barroso wrote:
 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.

That does make sense, now that I think about it.  I didn't look
closely enough.  I saw what I expected to see.  The extracted
files themselves always go to disk.  But they don't in this
case.  Thanks.


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Ken Teague
On Fri, Feb 5, 2010 at 12:39 AM, bruno bruno.deb...@cyberoso.com wrote:
 Why not simply use the  t option for content listing :

 tar tvf  * --exclude-from $EXCLUDES

He's already creating the archive with -v.  Why process the archive a
2nd time just to get a listing when it comes from stdout the 1st time?


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



Re: Bash question: get output as a variable?

2010-02-05 Thread bruno

Ken Teague wrote:

On Fri, Feb 5, 2010 at 12:39 AM, bruno bruno.deb...@cyberoso.com wrote:
  

Why not simply use the  t option for content listing :

tar tvf  * --exclude-from $EXCLUDES



He's already creating the archive with -v.  Why process the archive a
2nd time just to get a listing when it comes from stdout the 1st time?


  

because it's a simplier way to get the list into a variable
because the script gets much clearer
because it is not safe to rely on stderr since it is supposed to display 
errors, which it might do as well
because he would get the list of the files that have really been 
compressed, not just the processed ones
because the overhead may not be worth the complication of trying to do 
both things at the same time
because he might as well never use the v option (it's not useful in my 
proposition either, and it would even reduce the overhead)


because it is another way to do it and nobody had proposed it before


Bruno


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




Re: Bash question: get output as a variable?

2010-02-05 Thread Paul E Condon
On 20100205_135919, Alexey Salmin wrote:
 On Fri, Feb 5, 2010 at 1:20 PM, Javier Barroso javibarr...@gmail.com wrote:
  On Fri, Feb 5, 2010 at 1:06 AM, Stephen Powell zlinux...@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

A comment towards 'how would I know':

Traditional Unix and traditional C both made provision for stdin,
stdout, and stderr. When Stroustrup, et al., started working on C++
and its earlier predicessors, someone recognized the need for a fourth
stdXXX, namely stdlog.  I think it was/is a good idea, but it has
never gained enough traction to justify all the changes that would be
required if it were introduced into the POSIX standard. Instead, the
community has kept the name stderr, but changed the usage to something
that might better be named stdmsg, indicating both errors and action
logging. Its the sort of historical development that doesn't get recorded
in any man page (or at least any man page that newbies know about).

HTH
-- 
Paul E Condon   
pecon...@mesanetworks.net


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Bob McGowan
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!
 

I would avoid trying to store the file listing in any type of variable,
since you have no idea how big the list will be.

And, as others have noted, in the above case the file names list is sent
to stderr, so it doesn't mess up the stdout you requested for the
archive data.

I'd recommend using a named pipe, or FIFO, like this:

  mknod filenames p
  tar -zcvf - *  --exclude-from $EXCLUDES 2filenames  |
  openssl ... | dd ... 

NOTE:  the '2filenames' and the ampersand to put the command into the
background.

Then, later in the script, you would do something like this:

while read filename
do
  # other work
done  filenames

I've not done the above with anything large enough to know, but the way
a FIFO works, the read loop will block, waiting for input, if it
exhausts the FIFO content.  The FIFO will generate a zero length read
after the last line from tar is captured which will terminate the loop.

-- 
Bob McGowan


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Bob McGowan
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!
 

I forgot to ask, in the last post:  Why are you using the 'dd' command?

I would think you could do:

  tar ... | openssl ...  $(hostname)-$(date +%Y%m%d).tbz

-- 
Bob McGowan
Symantec
US Internationalization


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Chris Jackson

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 ) 21 )


It may cause unexpected results if there're spaces in the filenames though.

--
Chris Jackson
Shadowcat Systems Ltd.


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




Re: Bash question: get output as a variable?

2010-02-05 Thread Boyd Stephen Smith Jr.
On Friday 05 February 2010 10:01:45 Paul E Condon wrote:
 Traditional Unix and traditional C both made provision for stdin,
 stdout, and stderr. When Stroustrup, et al., started working on C++
 and its earlier predicessors, someone recognized the need for a fourth
 stdXXX, namely stdlog.

If you system provides stdlog (or a C++ equivalent), it is likely still file 
descriptor 2, but opened in line-buffered mode.  stderr (and cerr) is supposed 
to be opened in non-buffered mode.
-- 
Boyd Stephen Smith Jr.   ,= ,-_-. =.
b...@iguanasuicide.net  ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-'
http://iguanasuicide.net/\_/


signature.asc
Description: This is a digitally signed message part.


Re: Bash question: get output as a variable?

2010-02-05 Thread Brian Ryans
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: brianlry...@gmail.com   ..:
 X  ASCII Ribbon Campaign Against HTML mail and v-cards: asciiribbon.org
/ \ Any technology distinguishable from magic is insufficently advanced.


signature.asc
Description: Digital signature


Re: Bash question: get output as a variable?

2010-02-05 Thread Ken Teague
On Fri, Feb 5, 2010 at 7:44 AM, bruno bruno.deb...@cyberoso.com wrote:
 because it's a simplier way to get the list into a variable

Can you please explain how it's simpler.  The method I suggested
certainly isn't as easy, but the method I suggested merely showed a
detailed example.  Others here are coming up with 1 or 2 liners that
can do this without having to process the archive a 2nd time.

I don't see how the example you provided puts the list into a
variable.  Would you be so kind as to go into more detail regarding
your suggestion?


 because the script gets much clearer

I agree that your suggestion would be much clearer, and your
suggestion may suit the needs of the OP.  I suppose it's just my
personal feelings to not sacrifice efficiency for making a script
easier to understand.  Comments can be added to the script to explain
how parts of it function.


 because it is not safe to rely on stderr since it is supposed to display
 errors, which it might do as well

Others have recommended stderr.  I'm not sure why.  The method that
would be most effective would be to direct stdout, not stderr.

it...@mybox:~$ ls -l
-rw-r--r-- 1 itsme itsme 0 2010-02-05 14:54 file1
-rw-r--r-- 1 itsme itsme 0 2010-02-05 14:54 file2
-rw-r--r-- 1 itsme itsme 0 2010-02-05 14:54 file3
-rw--- 1 root   root0 2010-02-05 14:53 somefile
it...@mybox:~$ tar cvf test.tar somefile file1 file2 file3 out.txt
tar: somefile: Cannot open: Permission denied
tar: Error exit delayed from previous errors
it...@mybox:~$ cat out.txt
file1
file2
file3


 because he would get the list of the files that have really been compressed,
 not just the processed ones

Not if he uses stdout.  The example shown above only shows what was
added to the tar file.


 because the overhead may not be worth the complication of trying to do both
 things at the same time

Efficiency vs clarity; sometimes it's a personal preference, other
times it may not be.  For example, if I'm passing this script on to
someone else that isn't familiar with the scripting language, I think
clarity would be paramount.  Otherwise, I'd go for efficiency.


 because he might as well never use the v option (it's not useful in my
 proposition either, and it would even reduce the overhead)

The method you suggested is to process the archive a 2nd time to get
the listing, and to use -tv.  That causes more overhead than getting
the list of processed files while the archive is being created.


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Ken Teague
On Fri, Feb 5, 2010 at 10:10 AM, Chris Jackson
c.jack...@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 ) 21 )

 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.


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Tony Nelson
On 10-02-04 19:06:58, Stephen Powell 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?

As the man page notes, see `info tar`.  The answer is buried in 3.9 
Asking for Confirmation During Operations.

-- 

TonyN.:'   mailto:tonynel...@georgeanelson.com
  '  http://www.georgeanelson.com/


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



Re: Bash question: get output as a variable?

2010-02-05 Thread Ken Teague
On Fri, Feb 5, 2010 at 12:42 AM, Mart Frauenlob
mart.frauen...@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-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-05 Thread Bob McGowan
Ken Teague wrote:
 On Fri, Feb 5, 2010 at 10:10 AM, Chris Jackson
 c.jack...@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 ) 21 )

 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 '21' 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-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-04 Thread Alex Samad
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.


signature.asc
Description: Digital signature


Re: Bash question: get output as a variable?

2010-02-04 Thread Stephen Powell
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-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-04 Thread Javier Barroso
On Thu, Feb 4, 2010 at 11:32 PM, Stephen Powell zlinux...@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-requ...@lists.debian.org
 with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org




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



Re: Bash question: get output as a variable?

2010-02-04 Thread Stephen Powell
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-requ...@lists.debian.org 
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Re: Bash question: get output as a variable?

2010-02-04 Thread Javier Barroso
On Fri, Feb 5, 2010 at 1:06 AM, Stephen Powell zlinux...@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-requ...@lists.debian.org
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org