Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The following works from the command line, but fails as script:

gunzip -c /disk1/hadoop/gunzip_tmp/in/part-00006.gz >> /disk1/hadoop/gunzip_tmp/outfile.000

The following script lines:

UZPCMD="gunzip -c "$TMPDIR"/in/"$FILEONLY" >> "$TMPDIR"/outfile.000"
echo $TMPDIR
echo $FILEONLY
echo $UZPCMD

produce the following output - the gunzip command line looks as it should:

/disk1/hadoop/gunzip_tmp
part-00006.gz
gunzip -c /disk1/hadoop/gunzip_tmp/in/part-00006.gz >> /disk1/hadoop/gunzip_tmp/outfile.000

However, issuing the command line from the script:

$UZPCMD

sends line after line of the unzipped file to stdout (as if the redirection were being ignored).

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You cannot specify the redirection as part of the variable string value.

The shell does some additional evaluation on input when you enter something directly on the command line, and that doesn't happen for string values. >> is just another argument to gunzip.

$ CMD="echo >> foo"
$ $CMD
>> foo

You can use eval instead. In the example, eval $CMD would correctly redirect echo output to the file foo. Note that this can introduce problems with unexpected input.

share|improve this answer
    
eval $UZPCMD worked like a charm. Thanks, Daniel! –  MarkP Feb 14 '12 at 21:53
    
@MarkP: in general, you should enclose variable substitutions in double-quotes to prevent unwanted extra parsing, e.g. UZPCMD="gunzip -c $TMPDIR/in/$FILEONLY >> $TMPDIR/outfile.000" and eval "$UZPCMD" -- it doesn't matter in this particular case, but is a good habit to get into. –  Gordon Davisson Feb 14 '12 at 23:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.