John Wooten
John Wooten

Reputation: 745

bash script, file input in piped command

I have the following alias that is sourced during login on my Mac PowerBook running El Capitan, OS X 10.11.3 with Developer Tools install and command line utilities installed:

alias prt_2up="enscript -G -p - \$@ | quarto -2 | lpr -P $PRINTER"

Note that $PRINTER is defined in .bashrc as:

export PRINTER=EPSON_Stylus_CX4800___barsoom

I "used" to use this as:

prt_2up file.txt

and it created a printout with 2 pages per print page with no problems.

Now, I get the following error:

19:54:50 1z [woo:~] > source .bashrc
19:54:54 1z [woo:~] > echo $PRINTER
EPSON_Stylus_CX4800___barsoom
19:54:59 1z [woo:~] > prt_2up test.php
enscript: couldn't open input file "noclobber": No such file or directory
no output generated
lpr: The printer or class does not exist.

Don't understand why $PRINTER isn't said to exist and what the input file "no clobber" is about. BTW, I've done set +o no clobber also. It doesn't appear to matter whether I set it on or off. The quarto is another utility that allows putting more than one page to a sheet of paper. Where does the noclobber come from?

I've also tried using $@ without the leading \ also. No joy.

Suggestions appreciated.

Upvotes: 0

Views: 116

Answers (1)

dan4thewin
dan4thewin

Reputation: 1184

I can't tell you why it worked before, but I definitely see a problem. Before I go into detail, let me suggest you try this:

prt_2up () { enscript -G -p - "$@" | quarto -2 | lpr -P "$PRINTER"; }

The problem I see is with the use of $@ in an alias as opposed to a shell function. Look at the following:

$ set -xv
$ alias foo="echo \$@"
alias foo="echo \$@"
+ alias 'foo=echo $@'
$ foo a b
foo a b
+ echo a b
a b
$ alias bar="echo \$@ | cat"
alias bar="echo \$@ | cat"
+ alias 'bar=echo $@ | cat'
$ bar a b
bar a b
+ echo
+ cat a b
cat: a: No such file or directory
cat: b: No such file or directory
$ baz () { echo "$@" | cat; }
baz () { echo "$@" | cat; }
$ baz a b
baz a b
+ baz a b
+ echo a b
+ cat
a b

The bar alias doesn't do the same thing as the baz shell function, and neither does foo.

$ set 1 2 3
set 1 2 3
+ set 1 2 3
$ echo "$@"
echo "$@"
+ echo 1 2 3
1 2 3
$ alias foo
alias foo
+ alias foo
alias foo='echo $@'
$ foo a b
foo a b
+ echo 1 2 3 a b
1 2 3 a b

The $@ in the alias refers to arguments to the shell itself, while $@ in a shell function refers to arguments to that function.

Upvotes: 1

Related Questions