Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm using this command to process PNG files:

find . -iname "*png" -print0 | xargs -r0 --max-procs=4 -n1 sh -c 'pngnq -s1 $1 && advpng -z -4 -q ${$1%.*}-nq8.png' -

It seems to actually work fine, but also outputs this for each file that it processes:

-: 1: -: Bad substitution

I assume that I'm doing something wrong here ${$1%.*} - but I'm really not sure. The trailing dash is there on purpose, as per the docs here - and I can't just pipe the whole thing because advpng can't process things from stdin/out, sadly - so I have to use filenames.

Anyone know how to fix this so it doesn't have this error?

share|improve this question
    
I'm pretty sure the trailing slash is unnecessary. It's not anywhere in the doc you linked. Simply try to rerun without it and you'll see! (After you fixed as per the answer below.) –  janos Jul 6 '13 at 5:08
    
I was using a trailing dash, but the docs I linked use a trailing string value - it doesn't really matter. If you don't pass an initial value into the shell, you get a different error and nothing works. –  Duncan Lock Jul 6 '13 at 7:19

2 Answers 2

up vote 3 down vote accepted

An obvious problem is the missing quoting:

... sh -c 'pngnq -s1 "$1" && advpng -z -4 -q "${1%.*}"-nq8.png' -

You are right about your assumption that ${$1%.*} is the real problem. You must not repeat the $ in the brackets.

The docs say nothing about a trailing dash, do they? Just about a name.

share|improve this answer
    
I think you should first state the real problem and the solution, and after that as an additional tip mention the potential quoting issue in case the filenames have spaces or other characters that need quoting. –  janos Jul 6 '13 at 5:10
    
I was using a trailing dash, but the docs I linked use a trailing string value - it doesn't really matter. If you don't pass an initial value into the shell, you get a different error and nothing works. Your solution works, though - wrong quoting, thanks! –  Duncan Lock Jul 6 '13 at 7:21

Use GNU Parallel:

find . -iname "*png" -print0 | parallel -0 pngnq -s1 {} '&&' advpng -z -4 -q {.}-nq8.png

You can find more about GNU Parallel at: http://www.gnu.org/s/parallel/

You can install GNU Parallel in just 10 seconds with:

wget -O - pi.dk/3 | sh 

Watch the intro video on http://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

share|improve this answer

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.