How To: Using Cygwin and Bash Scripts (e.g., Fred's) with IM

A plethora of command-line scripts that perform geometric transforms, blurs, sharpens, edging, noise removal, and color manipulations.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

Very strange combination of syntaxes.

I would not expect either histmatch.sh or ./histmatch.sh to work without the bash, if you have not put the path to the script into your PATH variable. But it should be able to find the scripts if you have put the path to the script into your PATH environment variable. At least for the case when using the Cygwin terminal. Not sure about what happens with the Window terminal in terms of whether it can recognize the need for bash.exe, though it might recognize the need if .sh is associated with bash.exe.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

From the Windows console, yes, when I don't say where the script is but it is in my path, as in my first example, bash finds it and runs it. But then it fails.

The problem is the "type" command. Here's a simple script, thepwd.sh:

Code: Select all

#!/bin/bash
echo Current directory:
pwd
echo $0
type $0
Typing "thepwd.sh" in a Windows console gives:

Code: Select all

Current directory:
/cygdrive/f/web/im
F:\web\im\thepwd.sh
F:\web\im\thepwd.sh: line 6: type: F:\web\im\thepwd.sh: not found
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

Does the same script work in the Cygwin terminal? Or does it give error messages about -type as well?

Do you have something like type.exe installed with Cygwin?

There are many unix functions in my scripts and -type is one that I use in all of them. However, the script can be modified to avoid its use. It was just being used for locating the script for use in my error messaging.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

No, Cygwin doesn't have type.exe. According to "man bash", it is builtin.

Yes, thepwd.sh works fine from the Cygwin window, which has bash as the shell:

Code: Select all

$ /cygdrive/f/web/im/thepwd.sh
Current directory:
/home/Alan
/cygdrive/f/web/im/thepwd.sh
/cygdrive/f/web/im/thepwd.sh is /cygdrive/f/web/im/thepwd.sh
When I don't include "bash", I think Windows works out the full path and put that in argument zero. This isn't a major problem as a Cygwin user can call your scripts by starting with "bash", except where your scripts call your scripts. Those will only work if the user starts bash, and runs your script from there.

For example, at the Windows console, I can start bash and successfully run thepwd from there:

Code: Select all

F:\web\im>bash

Alan@Fiona /cygdrive/f/web/im
$ thepwd.sh
Current directory:
/cygdrive/f/web/im
/cygdrive/f/web/im/thepwd.sh
/cygdrive/f/web/im/thepwd.sh is /cygdrive/f/web/im/thepwd.sh

Alan@Fiona /cygdrive/f/web/im
$ exit
exit

F:\web\im>
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

Would you agree that for simplicity and syntax consistency, it would be best to run my scripts from the Cygwin terminal?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

It doesn't need to be that strict. I'd say that for simplicity and syntax consistency, it would be best to run your scripts from with the bash shell. I don't think it matters whether that is in a Cygwin terminal or a Windows console.

Unless the "type" issue can be resolved (which I doubt), associating with bash won't help, so there's no point in adding the .sh extension.

I need to do more testing -- which of your scripts calls other scripts?

histmatch uses a fair selection of Unix tools: tr, awk, sed, but not bc. Any suggestions for "difficult" scripts I might test?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

snibgo wrote:It doesn't need to be that strict. I'd say that for simplicity and syntax consistency, it would be best to run your scripts from with the bash shell. I don't think it matters whether that is in a Cygwin terminal or a Windows console.
It seemed (to me from reading your post) that using the Cygwin window was simpler than opening a bash window and the other things you did. Fewer commands.

snibgo wrote:I need to do more testing -- which of your scripts calls other scripts?
The only ones are two that call 3Drotate: 3Dcover and 3Dreflection.
snibgo wrote:histmatch uses a fair selection of Unix tools: tr, awk, sed, but not bc. Any suggestions for "difficult" scripts I might test?
bc is used in many but not all scripts.

When you say difficult, do you mean with lots of unix tools or just the complexity of the script?

Some of my scripts use sort and all or nearly all use expr and case. head, tail, cut and grep are also used in some of them.

My script, plot, is rather an oddball one. It makes graph-like images for profiles and histograms using gnuplot.

Curves is another complicated script that tries to emulate curves in PS or GIMP. It computes a Catmull-Rom spline for the control points.

Of course, 3Drotate has lots of computations in it. Also 3Dbox makes use of eval to pass data to the convert command to do six perspective distortions and requires the use of 3Drotate.

My script normcrosscorr, does a fast normalize cross correlation using FFTW to do FFT, but needs to use HDRI mode.

Cylinderize does a lot with -displace.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

fmw42 wrote:Fewer commands.
Maybe. I'll write it up as a page, and see what conclusions I come to. This will (almost certainly) include putting scripts in one place and including that as well as Cygwin's bin in the system path.

Thanks for the pointers. I'll grab few and test them for successful use of Unix tools under Cygwin.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

snibgo wrote:
Maybe. I'll write it up as a page, and see what conclusions I come to. This will (almost certainly) include putting scripts in one place and including that as well as Cygwin's bin in the system path.

Thanks for the pointers. I'll grab few and test them for successful use of Unix tools under Cygwin.
A write up on a page would be great. I can link it to my web site if you like.

Feel free to test any of my scripts that look useful to you.

Fred
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

I've written a page with the gory details of running ImageMagick under Cygwin's bash under Windows:
Cygwin. The final section is a summary of instructions to get Fred's scripts running. The main body of the document has detailed instructions, possible alternatives, examples, hints and tips. The summary is:


1. If you haven't already, install the Windows ImageMagick binary from the official Download Binary Releases IM page. Put the directory at the front of the Windows environment variable Path.

2. Install Cygwin from the Cygwin homepage. Include the bc package. Do not include ImageMagick.

3. Create a directory where you will store pre-built scripts.

4. Set Windows environment variables:
- - - Create SHELLOPTS equal to igncr.
- - - Edit Path to include C:\cygwin64\bin and C:\cygwin64\usr\local\bin (or similar), and the directory you made in step (3), and (if it's not already there) the ImageMagick directory from step (1).

5. Download the pre-built scripts to the directory you made in step (3).

6. Start a new command console. If this is a Windows console, type " bash" to start bash.

7. Run any pre-built bash scripts as required by typing the script name and any required arguments. For image files, give directory names in Windows format but with forward slashes, and either relative or absolute paths.

8. If you want to edit the script, go ahead. Fred's scripts often write temporary files to " ./", which is the current directory, and deletes them at the end. This works with no problem, and I suggest you don't change it.

9. If the script refers to absolute paths such as "/tmp", this may cause problems, and these should be changed to relative paths such as "." or "./tmp".
snibgo's IM pages: im.snibgo.com
Kembreg
Posts: 5
Joined: 2015-02-09T06:34:10-07:00
Authentication code: 6789

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by Kembreg »

Snigbo,

What exactly, and in layman's terms does this sentence mean:

"Put the directory at the front of the Windows environment variable Path."

Thanks,

David
Kembreg
Posts: 5
Joined: 2015-02-09T06:34:10-07:00
Authentication code: 6789

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by Kembreg »

Snigbo,

In step 2, you write "Include the bc package. Do not include ImageMagick."

This is what I see when I try to install Cygwin:

Image

Where should I click in order to include the "bc" package but not "ImageMagick" ?

David
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by Bonzo »

When I install Imagemagick on windows I leave the box ticked asking if you want to add Imagemagick to the path or whatever it says. You do not need to worry about manually setting it then. Otherwise there are some instructions on the web and here is the first I found: http://www.computerhope.com/issues/ch000549.htm
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

Somewhere in there, their should be the unix bc package for cygwin. You will have to search each category, unless one of the Windows users knows exactly where it is.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

Kembreg wrote:What exactly, and in layman's terms does this sentence mean:
"Put the directory at the front of the Windows environment variable Path."
See my http://im.snibgo.com/cygwin.htm#envvar for more details on how to do this manually. (Or, as Bonzo says, tell IM installation to do it for you.)
Kembreg wrote:Where should I click in order to include the "bc" package but not "ImageMagick" ?
Again, see the detail on my http://im.snibgo.com/cygwin.htm page.
snibgo's IM pages: im.snibgo.com
Post Reply