tip: Simple script won't run in cygwin - vim editor involved


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tip: Simple script won't run in cygwin - vim editor involved
# 1  
Old 02-22-2009
Lightbulb tip: Simple script won't run in cygwin - vim editor involved

I ran into this issue and thanks to various postings in various forums, was
able to figure out the solution but didn't see one posting that laid the
whole issue out cleanly. So thought the following might help others ...

------------------------------------------------------------------------
The Problem
You use vim to create a simple shell script in a cygwin directory but the
script doesn't run properly. Example:

A script called test.sh:
#! /bin/sh
ls
echo "++++++"
cat a1.txt

produces this when run:
$ ./test.sh
./test.sh: line 2: $'ls\r': command not found
++++++
: No such file or directory

------------------------------------------------------------------------
The Solution
The problem is that vim has written the file in "DOS mode", which puts
an extra (as far as cygwin is concerned) carriage return (aka: control-M,
^M, \r) at the end of each line.

You can see that using the "-v" option with cat:
$ cat -v test.sh
#! /bin/sh^M
ls^M
echo "++++++"^M
cat a1.txt^M

The solution is to bring the file back into vi, set vim into binary mode with
the ":se binary" command, then save the file. Now, "cat -v" shows that
the carriage returns have been removed:
$ cat -v test.sh
#! /bin/sh
ls
echo "++++++"
cat a1.txt

And the program runs just fine:
$ ./test.sh
760log2trk.pl a1.txt spot.gpxcon.pl.rc
760log2trk.pl.rc a2.txt spot.gpxcon.tailers.kml
++++++
This is a file with a
few lines of text
in it.

------------------------------------------------------------------------
Note 1: The first line of the script file is common practice for a Unix script,
but isn't really required for the script to work in cygwin.

Note 2: Running the script by putting "./" in front of it is required in
cygwin as long as you don't have "." (the current directory) in the search
path. By default, cygwin doesn't include the current directory when looking
for commands. And for security reasons, it's not a good practice to put .
in your search path.

Note 3: Having saved the file in binary mode, you don't need to worry
about that setting for future edits of the file. vim will recognize the nature
of the file when it opens it and will keep it in that mode, so lines added
to the file will not be written with the carriage return. (vim tells you
that the file is in binary mode by putting "[unix]" after the file name
at the bottom of the window.)

Last edited by otheus; 03-08-2009 at 12:23 PM.. Reason: added lightbulb icon, edited subject
# 2  
Old 03-05-2009
Wait, do you have a question, or is this a cool tip you are sharing with the world?
# 3  
Old 03-07-2009
No question, just a tip

Nope, not a question, just a tip (or set of tips). Sorry for the confusion.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script won't run because hardware won't produce display

Can anyone offer any advice on how to modify the script below to work on a new system we have, that has no graphics capability? We admin the system through a serial RAS device. I've tried running the below script through the RAS and through an ssh -X session. It failed with something like "GTK... (3 Replies)
Discussion started by: yelirt5
3 Replies

2. Windows & DOS: Issues & Discussions

run cygwin bash script from notepad++

I'm using Notepad++ to edit my BASH scripts and using CYGWIN to run them from Windows7. In Notepad++ there is a 'Run' capability. How do I get this to run my scripts directly without having to enter the script name from the Cygwin command line? (3 Replies)
Discussion started by: millsy5
3 Replies

3. Shell Programming and Scripting

run vi/vim encrypted shell script without decryption on multiple servers

Hello Everyone, How do we run vi/vim encrypted shell script without decryption on multiple servers. It is a simple bash script and vim -nx <filename> has been used to encrypt with desired password. Now I have few errors, the syntax is absolutely fine as I have run that script multiple times on... (0 Replies)
Discussion started by: lovesaikrishna
0 Replies

4. Shell Programming and Scripting

Cygwin and simple script with if

I am having an issue with using cygwin (on Windows XP). My script errors on if -command. The script here works fine on Linux, but cygwin fails... #!/bin/sh if then echo "Test" fi The error -message: ./ShellTest.sh: line 4: syntax error near unexpected token `fi' ./ShellTest.sh:... (1 Reply)
Discussion started by: jussist
1 Replies

5. UNIX for Dummies Questions & Answers

A very simple script, but alias won't work

I am new to unix and therefore I did a lot of reading before posting. So please, if this has been answered before, forgive me for re-posting and point me to the right place for the answer. I have spent many hours searching the net and read over 50 posts in this forum and even tried a few thing but... (20 Replies)
Discussion started by: sssccc
20 Replies

6. Shell Programming and Scripting

Problems with simple script in cygwin

Hello! I have somo problems with simple scripts like this: #!/bin/bash echo -n "Enter your name and press : " read var_name echo "Your name is: $var_name" When I try to run it, this error occurs: ':not a valid identifier var_name. Why?? (I work in cygiwin) Is there anybody out... (10 Replies)
Discussion started by: blianna
10 Replies

7. UNIX for Dummies Questions & Answers

How to use cygwin to run bash script

Hi, all, I try to run a quite simple bash script mytest.sh in cygwin, it's content is: #!/bin/bash echo "It is my first bash shell" there are three lines in the script. The second line is blank line. When I run it use command: bash c:/mytest.sh, ... (6 Replies)
Discussion started by: Jenny.palmy
6 Replies

8. Shell Programming and Scripting

how to run cgi -script on Cygwin ?

All, I would like to run a cgi script in cygwin which i have installed in WinXP. My CYGWIN directory structure is /var/www/ drwxrwx---+ 2 user Users 0 Nov 23 16:24 cgi-bin drwxrwx---+ 3 user Users 0 Oct 22 17:21 htdocs drwxrwx---+ 3 user Users 0 Oct 22 17:22 icons and another... (1 Reply)
Discussion started by: jambesh
1 Replies

9. UNIX for Dummies Questions & Answers

Made command into a script but now won't run

Hello, After seeing in a Unix cheatsheet that I could add commands into a file and run that file as a command to save on typing, i tried it with this: #! /bin/csh # Backup website excluding directories that do not change rsync -e "ssh -p 2222" -axzvc --progress --stats --compress-level=9... (9 Replies)
Discussion started by: patwa
9 Replies

10. UNIX for Dummies Questions & Answers

Why won't my script run?

On the following script that I get an error when I try to execute as root: #./mv_log.sh bash: /root/util/mv_log.sh: Permission denied #!/usr/bin datetag=`date --date='1 day ago' +"%b%d%Y"` logname=`find /opt/bea/wlserver6.1/config/*/logs/ -iname 'access.log0*' -mtime -1 -print` mv... (4 Replies)
Discussion started by: darthur
4 Replies
Login or Register to Ask a Question