1

I have a shell script for converting images to an embroidery effect. This is the script : Embroidery script

This script is part of a web application. It runs normally on my Mac, however, after I deployed on my web server running CentOS 7, I noticed that when I call it (which displays the man page) every line is duplicated, see :

embroidery.sh:

USAGE: embroidery [-n numcolors ] [-p pattern] [-t thickness] [-g graylimit]
USAGE: embroidery [-n numcolors ] [-p pattern] [-t thickness] [-g graylimit]
[-f fuzzval] [-b bgcolor] [-a angle] [-r range] [-i intensity] [-e extent]
[-f fuzzval] [-b bgcolor] [-a angle] [-r range] [-i intensity] [-e extent]

This is only the first lines but it does that for each line of the help. Also, the script doesn't work anymore as when I upload an image to convert it via my app, the script generates huge files (137 Mb) from a not even 1 Mb original file.

I am really confused about what's going on here. Does have the slightest idea what could be causing this ?

Can it be OS related ? Might the script live somewhere else and being called two versions at the same time even though I'm running it with the path like so lib/embroidery/embroidery.sh ?

Update : So this issue was unrelated to this behavior, which was well explained by John, the problem with the script generating huge files however was caused by the old version of ImageMagick available through the CentOS repo, I updated to 7.0.3 and it worked again

  • Are you sure this is really a problem with your script, and not the terminal software? Do you still see the duplication if you redirect the output to a file? – Zoredache Oct 13 '16 at 22:02
3

In your script, I observe that the help message is generated via:

usage2()
    {
    echo >&2 ""
    echo >&2 "$PROGNAME:" "$@"
    sed >&2 -e '1,/^####/d;  /^######/g;  /^#/!q;  s/^#*//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
    }

Let's look carefully at the sed command:

sed >&2 -e '1,/^####/d;  /^######/g;  /^#/!q;  s/^#*//;  s/^ //;  4,$p' embroidery

The problem is that (1) by default, sed prints whatever is left in the pattern space at the end of the commands, but (2) the command 4,$p causes an additional print. Thus, every line is printed twice.

One solution is to use the -n option to suppress the default print:

sed >&2 -ne '1,/^####/d;  /^######/g;  /^#/!q;  s/^#*//;  s/^ //;  4,$p' embroidery

Because OSX (BSD) and CentOS (Linux) use different versions of sed, some compatibility issues do occur.


By the way, this somewhat simpler sed command works for me:

sed >&2 -ne '/^#####/q; 1,/^####/d; s/^#* *//p' embroidery

Mini-example

Extracting just some key lines, your script looks something like:

$ cat testfile
#!/bin/bash
#
# misc info 1
# misc info 2
#
####
#
# USAGE: embroidery [-n numcolors ] [-p pattern] [-t thickness] [-g graylimit]
# [-f fuzzval] [-b bgcolor] [-a angle] [-r range] [-i intensity] [-e extent]
# [-N newseed] [-M mix] infile outfile
######
#
usage2()
        {
        sed >&2 -e '1,/^####/d;  /^######/g;  /^#/!q;  s/^#*//;  s/^ //;  4,$p' "$0"
        }

if [ $# -eq 0 ]
then
        # help information
        usage2
        exit 0
fi

If we run this script, the output looks like:

$ ./testfile 


USAGE: embroidery [-n numcolors ] [-p pattern] [-t thickness] [-g graylimit]
USAGE: embroidery [-n numcolors ] [-p pattern] [-t thickness] [-g graylimit]
[-f fuzzval] [-b bgcolor] [-a angle] [-r range] [-i intensity] [-e extent]
[-f fuzzval] [-b bgcolor] [-a angle] [-r range] [-i intensity] [-e extent]
[-N newseed] [-M mix] infile outfile
[-N newseed] [-M mix] infile outfile

Let's replace the sed command above with:

    sed >&2 -ne '/^#####/q; 1,/^####/d; s/^#* *//p' "$0"

Now, the output looks like:

$ ./testfile2

USAGE: embroidery [-n numcolors ] [-p pattern] [-t thickness] [-g graylimit]
[-f fuzzval] [-b bgcolor] [-a angle] [-r range] [-i intensity] [-e extent]
[-N newseed] [-M mix] infile outfile
| improve this answer | |
  • Thanks, this gives a bit more light on this issue. However, my real issue is more that the script is unusable since it produces very huge files. My initial thought was that each line was called twice, hence generating huge files, but this tells me it's not the case. Do you see anything in this script that might have a different behavior on CentOS on the image generation part ? Maybe something around the colors (I think this would be the main factor) – Geoffrey Hug Oct 14 '16 at 0:11
  • @GeoffreyHug Glad that helped. The image generation problem appears to me to be separate and unrelated. I count over 20 invocations of convert in the script. Without knowing what files you are using and what options you have set, I can't begin to guess which invocation is the one that is creating files that are too big. – John1024 Oct 14 '16 at 0:33
  • @GeoffreyHug You may want to run bash -x embroidery arg1 arg2 ... so that you can see what actually gets executed and then see which is the first statement that produces files too large. As a side note, there are many unquoted shell variables which will be subjected to globbing and word splitting and that could be a potential source of problems if any of the file names contain spaces, braces, ?, *, or other shell-active characters. – John1024 Oct 14 '16 at 0:34
  • In the end, what was causing the script to behave differently was simply the ImageMagick version apparently. I had the latest CentOS 7, I upgraded installing with source to 7.0.3 and the script worked as expected. – Geoffrey Hug Oct 15 '16 at 3:02
  • @GeoffreyHug Excellent! – John1024 Oct 15 '16 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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