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
NPM-RUN-SCRIPT(1)                                                                                                                NPM-RUN-SCRIPT(1)

NAME
npm-run-script - Run arbitrary package scripts SYNOPSIS
npm run-script <command> [--silent] [-- <args>...] alias: npm run DESCRIPTION
This runs an arbitrary command from a package's "scripts" object. If no "command" is provided, it will list the available scripts. run[-script] is used by the test, start, restart, and stop commands, but can be called directly, as well. When the scripts in the package are printed out, they're separated into lifecycle (test, start, restart) and directly-run scripts. As of ` https://blog.npmjs.org/post/98131109725/npm-2-0-0, you can use custom arguments when executing scripts. The special option -- is used by getopt https://goo.gl/KxMmtG to delimit the end of the options. npm will pass all the arguments after the -- directly to your script: npm run test -- --grep="pattern" The arguments will only be passed to the script specified after npm run and not to any pre or post script. The env script is a special built-in command that can be used to list environment variables that will be available to the script at run- time. If an "env" command is defined in your package, it will take precedence over the built-in. In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix. For example, if there is a devDependency on tap in your package, you should write: "scripts": {"test": "tap test/*.js"} instead of "scripts": {"test": "node_modules/.bin/tap test/*.js"} to run your tests. The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe. The actual shell referred to by /bin/sh also depends on the system. As of ` https://github.com/npm/npm/releases/tag/v5.1.0 you can customize the shell with the script-shell configuration. Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run. npm run sets the NODE environment variable to the node executable with which npm is executed. Also, if the --scripts-prepend-node-path is passed, the directory within which node resides is added to the PATH. If --scripts-prepend-node-path=auto is passed (which has been the default in npm v3), this is only performed when that node executable is not found in the PATH. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install, just in case you've forgotten. You can use the --silent flag to prevent showing npm ERR! output on error. You can use the --if-present flag to avoid exiting with a non-zero exit code when the script is undefined. This lets you run potentially undefined scripts without breaking the execution chain. SEE ALSO
o npm help 7 scripts o npm help test o npm help start o npm help restart o npm help stop o npm help 7 config January 2019 NPM-RUN-SCRIPT(1)
All times are GMT -4. The time now is 11:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy