Sponsored Content
Top Forums UNIX for Dummies Questions & Answers tip: Simple script won't run in cygwin - vim editor involved Post 302290208 by oxysep on Sunday 22nd of February 2009 01:10:24 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
VIMTUTOR(1)						      General Commands Manual						       VIMTUTOR(1)

NAME
vimtutor - the Vim tutor SYNOPSIS
vimtutor [-g] [language] DESCRIPTION
Vimtutor starts the Vim tutor. It copies the tutor file first, so that it can be modified without changing the original file. The Vimtutor is useful for people that want to learn their first Vim commands. The optional argument -g starts vimtutor with gvim rather than vim, if the GUI version of vim is available, or falls back to Vim if gvim is not found. The optional [language] argument is the two-letter name of a language, like "it" or "es". If the [language] argument is missing, the lan- guage of the current locale will be used. If a tutor in this language is available, it will be used. Otherwise the English version will be used. Vim is always started in Vi compatible mode. FILES
/usr/share/vim/vim73/tutor/tutor[.language] The Vimtutor text file(s). /usr/share/vim/vim73/tutor/tutor.vim The Vim script used to copy the Vimtutor text file. AUTHOR
The Vimtutor was originally written for Vi by Michael C. Pierce and Robert K. Ware, Colorado School of Mines using ideas supplied by Charles Smith, Colorado State University. E-mail: bware@mines.colorado.edu. It was modified for Vim by Bram Moolenaar. For the names of the translators see the tutor files. SEE ALSO
vim(1) 2001 April 2 VIMTUTOR(1)
All times are GMT -4. The time now is 06:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy