6
\$\begingroup\$

Write a program/script that decompresses and passes any compressed data appended at the end of the binary/file as standard input to a program specified as arguments.

$ gzip --to-stdout input >> your_solution
$ your_solution program args...

should have the same effects as:

$ program args... < input
\$\endgroup\$
0

3 Answers 3

7
\$\begingroup\$

zsh: (20 chars)

Edit: because zsh doesn't re-parse arguments like bash, I can lose the "s and it will still work with spaces in args

#!/usr/bin/zsh
tail -n+3 $0|zcat|$@

Bash (25 24 chars not including shebang line)

#!/bin/bash
tail -n+3 "$0"|zcat|"$@"
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Could -n +3 be changed to -n+3? \$\endgroup\$ Jan 31, 2011 at 23:13
  • \$\begingroup\$ @idealmachine yes, yes it could... (tho i had to test that :D) \$\endgroup\$ Jan 31, 2011 at 23:25
  • \$\begingroup\$ Also just realized that if i used zsh, I would be able to lose the "s and still have it work with spaces... \$\endgroup\$ Jan 31, 2011 at 23:27
2
\$\begingroup\$

C (251 chars)

Way too long to win, but I wanted to do one in a compiled language to make things interesting. (It turned out to be quite straightforward actually).

It's probably very compiler-dependent (I used GCC 4.4.3 with the default settings on 32-bit Linux)

#include<stdio.h>
#define q(x)FILE*x=x##open(
#define w strcpy(o+b
o,s=7462;char b[99];main(int i,char**a){q(f)*a,"r");while(--s)fgetc(f);
for(o=0;++s<i;o++[b]=32)w,a[s]),o+=strlen(a[s]);w,"|zcat");q(p)b,"w");
for(;~(i=fgetc(f));fputc(i,p));fclose(p);}
\$\endgroup\$
1
\$\begingroup\$

Bash (OP solution)

This is an example I came up with:

cat $0 | awk 'NR > 1' | zcat | $* ; exit
\$\endgroup\$
2
  • 1
    \$\begingroup\$ this fails if there are spaces in any args \$\endgroup\$ Jan 28, 2011 at 16:35
  • \$\begingroup\$ Thanks for the info. I will leave it as such, your solution is better. \$\endgroup\$
    – Alexandru
    Jan 28, 2011 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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