I've got a Cygwin script that outputs stuff to a file. My only problem is that it always uses the \n "lf" line endings instead of \r\n "cr-lf". I want to either replace them or just get cygwin to always output \r\n for line endings.
I've tried the following two commands to no avail within the script.
unix2dos d:/temp.txt
sed -i -e 's/\n/\r\n/g' d:/temp.txt
Your unix2dos call should work. Are you getting some kind of error?
The Cygwin installer has an option for selecting the default line ending convention (DOS or Unix), although it might only show it during a new installation -- I don't remember offhand.
You're specifying the path to unix2dos using a bizarre mix of Windows and Linux file endings.
If you're calling it from a Windows command shell, use this:
unix2dos d:\temp.txt
If you're calling it from within Cygwin, or a Cygwin shell script or similar, use this:
unix2dos /cygdrive/d/temp.txt
Related
Hello I'm trying to run a perl script on a Windows 64 bit. I'm getting the error like this :
/usr/bin/perl : bad interpreter : Permission denied
I have my perl script on my windows 64 bit C:\test\perlscripts\testperl.pl.
You probably saved the Perl script with DOS style line endings. The shell is looking for a file called /usr/bin/perl<CR>.
Save your files with Unix-style line endings. My .vimrc which I use with my natively compiled vim and gvim has:
set fileformat=unix
set fileformats=unix,dos
Check your editor's settings for the appropriate options.
To fix line endings in a particular file, use $ dos2unix filename.
You may not install Perl in default cygwin64 package. Please ensure that you have Perl at /usr/bin/perl.exe.
If it is not there, run setup-x86_64.exe again and select Perl interpreter.
I have a unix command
(script) which has a nested perl script in it.
when i run this unix command from command line it works fine.
If I am running same command from a tcl file using exec, i am getting following error:
'sh: /cmdpath/cmd.pl: /usr/local/bin/perl5: bad interpreter: Permission denied'
Any Idea what could be causing this. My tcl code is trying to execute this command several times ( more than 100 times).
Thanks
Ruchi
Almost certainly your Perl script is encoded in DOS/Windows line-ending format, which uses \r\n to terminate lines. Since Unix terminates lines with \n only, the \r is interpreted as belonging to the executable name, so that the kernel tries to run a program named perl5\r and fails.
Deleting the trailing \r on this line should fix the problem.
Alternatively, it may be that the perl5 executable either does not exist at the given path, or exists but lacks the execute permission bit. If you have this executable living somewhere else in the filesystem, update the path on the first line of the script to point to it. To fix the latter problem, run
chmod +x /usr/local/bin/perl5
You will need to be root to do this.
Given the output you are showing, you are likely executing "sh cmd.pl". In turn, sh is trying to execute the perl interpreter.
Why not spawn "/usr/local/bin/perl5 cmd.pl" directly, this will be more efficient, especially if you are doing that hundreds of time.
I would like my scripts works on Windows and UNIX system, so I want some code as blew(just a sample, not work)
if $^O eq 'MSWin32' {
#!/c:/perl/bin/perl.exe
}
else { / non-windows system
#!/usr/local/bin/perl
}
is there any way to do this in perl? it seems the #! line must be the first line of a perl script, so this is impossible?
The shebang (#!/path/to/interpreter) is only used on unix-like systems. And it has to be the first line. On Windows systems, the shebang is not used, instead file ending associations may be used: you can associate the .pl file ending with the perl interpreter.
Command line switches in the shebang will be interpreted by the perl interpreter, regardless of platform.
A safe way to launch perl scripts on all platforms is to actually use the perl command. It will be available in case of an successfull Perl installation. E.g. perl myscript.pl instead of ./myscript.pl. The second options requires that the file is set as "executable" on *nix systems.
#! must be the first two characters of the file, because that's where the kernel looks for them when it tries to launch your file. They're part of a directive for the OS (called the shebang line), and the OS knows nothing about Perl.
If you use a standard Perl installer (such as ExtUtils::MakeMaker) to installer your script, just use #!perl and the installer will adjust any existing #! line for your system.
It's not possible. The "#!" is looked at by the exec*() family of Unix functions, to determine how to run the file. So you can't do any scripting there.
It's not all that useful anyway, since the Windows command prompt (or CreateProc() function, but I'm not sure whether that can be used to launch a batch file) doesn't look for the #!.
I was sent a perl script over mail and asked to run it .I placed it on my local drive as is but when I tried to run the script it shows me
/usr/bin/perl^M: bad interpreter: No such file or directory
I checked and usr/bin/ does have perl in there .I am not sure what is wrong.I checked a bit and looks like I am missing some spaces or something ..I tried adding those at the end of
usr/bin/perl and at the end of the file but that didnt help either.
I even tried to use dos2unix
dos2unix oldfile newfile
'dos2unix' not found.This is on MacOSX.
Might I also mention that I am sshing into my mac using my windows machine at home.
You're on the right track. Your script has DOS style newlines at the end, which is not supported by your kernel.
The solution is to use something to convert the DOS newlines to Unix style. dos2unix would presumably work if you had it, so use something else equivalent.
In the absence of dos2unix, you can use tr (on Mac OS X) to strip the DOS / Windows new-lines:
tr -d '\r' < old.pl > new.pl
This will solve the "bad interpreter" issue.
"Can't locate Gpu.pm in #INC" is a different issue. Either you don't have Gpu.pm installed on your Mac (or whichever computer on which you are running this, I'm confused by your comments) or it's not in your include path. I don't who what that script is or what it does. A quick look on http://search.cpan.org/ revealed nothing.
If you can get that Perl module (presumably from whoever supplied oldfile), you'll have to ensure it is in #INC.
Do this in vim:
:%s/^M//g
save the file and try running it again
execute: vim
when vim opens go to command mode by hitting the escape key .... at the command prompt (:) type: %s/^M//g. This will remove all "^M" characters from the file.
dos2unix in Perl:
perl -pi -e 'tr/\r//d' file.txt
Maybe it's dumbest question in the world, but I seriously have problems with it and could use help. I am trying to run perl script on linux. It's a simple text editing script, nothing fancy. I googled for it and I found that I had to chmod +x it and then just run myscript.pl in the console. Since it's supposed to modify a text file I did myscript.pl > myfile.txt after chmoding it
But it doesn't work. I get: bash: perl myscript.pl: command not found
Unless myscript.pl is in your path you will need to specify the current directory.
$ ./myscript.pl
You can check if the current directory is in your path with $ echo $PATH. If you're frequently using this script you can put it in the path by moving it to a directory that's part of your path, usually ~/bin.
Or by adding the current directory to the $PATH environment variable. Check the documentation for your shell for instructions.
Can you post the first few lines of your script?
Specifically, if you have #!/usr/bin/perl are there any typos on that line, extra spaces, etc.?
Also do a ls /usr/bin/perl (or whatever is on that line) to make sure it's actually there.
It doesn't look like perl is installed on your Linux machine. Do you get the same thing when you try this: # perl -e 'print "hi";' ?
As Chirael said, it sounds like your shebang line (the directive at the top of the file, that tells the shell how to run the script) is invalid somehow. You can bypass the shebang line entirely by invoking your script as:
perl myscript.pl > myfile.txt
You also don't need to set the script's executable bit, as with this method of invocation, you are only reading the script, not executing it (from the shell's perspective).
According to this thread, it could be from different representation of the new line.
Have you written the script on a windows box and copied over to your linux box?
What is your text editor?
I had the same issue, and traced it to DOS line endings (^M). Running dos2unix on the .pl file fixed the issue.
Please use,
./myperl.pl > outfile.txt
to give the current directory path
thanks