Does the zcat head bash fail?

set -eu 
VAR=$(zcat file.gz  |  head -n 12)

works great

set -eu   -o pipefail
VAR=$(zcat file.gz  |  head -n 12)

calls bash to exit with an error. How does this cause failure?

Note that the .gz file contains millions of lines (~ 750 MB, compressed).

+7
source share
1 answer

Think about it for a moment.

  1. You tell the shell that your entire pipeline should be considered failed if any component fails.
  2. You say zcatwrite your conclusion to head.
  3. Then you say headexit after reading 12 lines from the input stream, which is longer than 12 lines.

, : zcat , ! , - , - - .

zcat , zcat , , , , . .


, zcat , EPIPE, EPIPE write : , - .

head ( FIFO) , EPIPE, . zcat zcat , , , , .


- , , , , - :

var=$(head -n 12 < <(zcat file.gz))

zcat , . ( , $var 12 , /).


Python gzip. Python ( Python 2 3.x), , :

zhead_py=$(cat <<'EOF'
import sys, gzip
gzf = gzip.GzipFile(sys.argv[1], 'rb')
outFile = sys.stdout.buffer if hasattr(sys.stdout, 'buffer') else sys.stdout
numLines = 0
maxLines = int(sys.argv[2])
for line in gzf:
    if numLines >= maxLines:
        sys.exit(0)
    outFile.write(line)
    numLines += 1
EOF
)
zhead() { python -c "$zhead_py" "$@"; }

... zhead, , , - . ( zhead in.gz 5, 5 in.gz).

+15

Source: https://habr.com/ru/post/1666055/


All Articles