public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* General scripting issues vs. Linux
@ 2023-03-10 13:47 Markus Becker
  2023-03-10 13:59 ` Ken Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Becker @ 2023-03-10 13:47 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 1235 bytes --]

Dear Guys,

I am quite an newby in Cygwin scripting and encountered several execution
issues with bash scripts. For example, when i try to execute the following
simple scriptfile "skript1.sh":

# This is a testscript
Statement="This is the testscript number 3"
FILE="home/mbecker/Secure_Copy_Beispiel.txt"
ls -l $FILE
echo $Statement
echo The file is $FILE

i got these results:

$ ./skript1.sh
ls: cannot access 'home/mbecker/Secure_Copy_Beispiel.txt'$'\r\r': No such
file or directory
This is the testscript number 3
The file is home/mbecker/Secure_Copy_Beispiel.txt

or another results from a different script:

$ ./skript7.sh
./skript7.sh: line 3: $'clear\r': command not found
Dr▒cken sie beliebige Tasten und dann return
': not a valid identifierd: `TASTE

These are just two of several issues coming up with bash scripting in
Cygwin. Maybe this is merely a corse problem with my platform
understanding. But why is Cygwin calling errors when performing standard
Linux bash commands? Is it due to a different syntax? Or is it even simpler?

I would appreciate any advice and information on this. As i said, i am
quite used to Cygwin, but not so to scripting in Cygwin.

Thanks a lot
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: General scripting issues vs. Linux
  2023-03-10 13:47 General scripting issues vs. Linux Markus Becker
@ 2023-03-10 13:59 ` Ken Brown
  2023-03-10 16:15   ` Brian Inglis
  0 siblings, 1 reply; 4+ messages in thread
From: Ken Brown @ 2023-03-10 13:59 UTC (permalink / raw)
  To: Markus Becker, cygwin

On 3/10/2023 8:47 AM, Markus Becker via Cygwin wrote:
> Dear Guys,
> 
> I am quite an newby in Cygwin scripting and encountered several execution
> issues with bash scripts. For example, when i try to execute the following
> simple scriptfile "skript1.sh":
> 
> # This is a testscript
> Statement="This is the testscript number 3"
> FILE="home/mbecker/Secure_Copy_Beispiel.txt"
> ls -l $FILE
> echo $Statement
> echo The file is $FILE
> 
> i got these results:
> 
> $ ./skript1.sh
> ls: cannot access 'home/mbecker/Secure_Copy_Beispiel.txt'$'\r\r': No such
> file or directory
> This is the testscript number 3
> The file is home/mbecker/Secure_Copy_Beispiel.txt
> 
> or another results from a different script:
> 
> $ ./skript7.sh
> ./skript7.sh: line 3: $'clear\r': command not found
> Dr▒cken sie beliebige Tasten und dann return
> ': not a valid identifierd: `TASTE
> 
> These are just two of several issues coming up with bash scripting in
> Cygwin. Maybe this is merely a corse problem with my platform
> understanding. But why is Cygwin calling errors when performing standard
> Linux bash commands? Is it due to a different syntax? Or is it even simpler?

It looks like your scripts have CRLF line endings.

Ken

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: General scripting issues vs. Linux
  2023-03-10 13:59 ` Ken Brown
@ 2023-03-10 16:15   ` Brian Inglis
  2023-03-10 18:42     ` Backwoods BC
  0 siblings, 1 reply; 4+ messages in thread
From: Brian Inglis @ 2023-03-10 16:15 UTC (permalink / raw)
  To: cygwin

On 2023-03-10 06:59, Ken Brown via Cygwin wrote:
> On 3/10/2023 8:47 AM, Markus Becker via Cygwin wrote:
>> I am quite an newby in Cygwin scripting and encountered several execution
>> issues with bash scripts. For example, when i try to execute the following
>> simple scriptfile "skript1.sh":
>> # This is a testscript
>> Statement="This is the testscript number 3"
>> FILE="home/mbecker/Secure_Copy_Beispiel.txt"
>> ls -l $FILE
>> echo $Statement
>> echo The file is $FILE
>> i got these results:
>> $ ./skript1.sh
>> ls: cannot access 'home/mbecker/Secure_Copy_Beispiel.txt'$'\r\r': No such
                                                              ^^^^^
>> file or directory
>> This is the testscript number 3
>> The file is home/mbecker/Secure_Copy_Beispiel.txt
>> or another results from a different script:
>> $ ./skript7.sh
>> ./skript7.sh: line 3: $'clear\r': command not found
                                 ^^^
>> Dr▒cken sie beliebige Tasten und dann return
>> ': not a valid identifierd: `TASTE
>> These are just two of several issues coming up with bash scripting in
>> Cygwin. Maybe this is merely a corse problem with my platform
>> understanding. But why is Cygwin calling errors when performing standard
>> Linux bash commands? Is it due to a different syntax? Or is it even simpler?

> It looks like your scripts have CRLF line endings.

Utilities such as coreutils, gawk, grep, sed, etc. Cygwin packages had Cygwin 
tweaks removed in 2017 to be compatible with Linux and other platforms in 
handling '\r' before newlines, except for the single exception of Cygwin text 
mounts, where '\r' may be stripped if a program opens a file from that mount in 
text "t" mode, and may be added on writes to a file in text "t" mode on a text 
mount:

	https://www.cygwin.com/cygwin-ug-net/using-textbinary.html

discussions:

	https://cygwin.com/legacy-ml/cygwin/2017-02/msg00152.html
	https://cygwin.com/legacy-ml/cygwin/2017-02/msg00188.html
	https://cygwin.com/legacy-ml/cygwin/2017-02/msg00189.html

Install package dos2unix which conveniently strips the offending junk from your 
scripts and files e.g.

	d2u -k skript*.sh

Install and use Cygwin editors and utilities, or check editor and utility 
settings to ensure they are not set to behave like Windows e.g. gvim set 
fileformat=unix termencoding=utf-8 fileencoding=utf-8 in your ~/.gvimrc ~/.vimrc 
~/.virc ~/.exrc; emacs (set-buffer-file-coding-system 'mule-utf-8-unix) in 
~/.emacs, type C-x C-q C-m f mule-utf-8-unix, or do the equivalent in more 
sophisticated initializations (auto)detecting file type, encoding, and format; 
for git config --global core.autocrlf = input, see:

https://stackoverflow.com/questions/3206843/how-line-ending-conversions-work-with-git-core-autocrlf-between-different-operat

-- 
Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

La perfection est atteinte                   Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to add
mais lorsqu'il n'y a plus rien à retirer     but when there is no more to cut
                                 -- Antoine de Saint-Exupéry

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: General scripting issues vs. Linux
  2023-03-10 16:15   ` Brian Inglis
@ 2023-03-10 18:42     ` Backwoods BC
  0 siblings, 0 replies; 4+ messages in thread
From: Backwoods BC @ 2023-03-10 18:42 UTC (permalink / raw)
  To: cygwin

On Fri, Mar 10, 2023 at 8:16 AM Brian Inglis via Cygwin
<cygwin@cygwin.com> wrote:
>
> On 2023-03-10 06:59, Ken Brown via Cygwin wrote:
> > On 3/10/2023 8:47 AM, Markus Becker via Cygwin wrote:
> >> I am quite an newby in Cygwin scripting and encountered several execution
> >> issues with bash scripts. For example, when i try to execute the following
> >> simple scriptfile "skript1.sh":
> >> # This is a testscript
> >> Statement="This is the testscript number 3"
> >> FILE="home/mbecker/Secure_Copy_Beispiel.txt"
> >> ls -l $FILE
> >> echo $Statement
> >> echo The file is $FILE
> >> i got these results:
> >> $ ./skript1.sh
> >> ls: cannot access 'home/mbecker/Secure_Copy_Beispiel.txt'$'\r\r': No such
>                                                               ^^^^^
> >> file or directory
> >> This is the testscript number 3
> >> The file is home/mbecker/Secure_Copy_Beispiel.txt
> >> or another results from a different script:
> >> $ ./skript7.sh
> >> ./skript7.sh: line 3: $'clear\r': command not found
>                                  ^^^
> >> Dr▒cken sie beliebige Tasten und dann return
> >> ': not a valid identifierd: `TASTE
> >> These are just two of several issues coming up with bash scripting in
> >> Cygwin. Maybe this is merely a corse problem with my platform
> >> understanding. But why is Cygwin calling errors when performing standard
> >> Linux bash commands? Is it due to a different syntax? Or is it even simpler?
>
> > It looks like your scripts have CRLF line endings.
>
> Utilities such as coreutils, gawk, grep, sed, etc. Cygwin packages had Cygwin
> tweaks removed in 2017 to be compatible with Linux and other platforms in
> handling '\r' before newlines, except for the single exception of Cygwin text
> mounts, where '\r' may be stripped if a program opens a file from that mount in
> text "t" mode, and may be added on writes to a file in text "t" mode on a text
> mount:
>
>         https://www.cygwin.com/cygwin-ug-net/using-textbinary.html
>
> discussions:
>
>         https://cygwin.com/legacy-ml/cygwin/2017-02/msg00152.html
>         https://cygwin.com/legacy-ml/cygwin/2017-02/msg00188.html
>         https://cygwin.com/legacy-ml/cygwin/2017-02/msg00189.html
>
> Install package dos2unix which conveniently strips the offending junk from your
> scripts and files e.g.
>
>         d2u -k skript*.sh
>
> Install and use Cygwin editors and utilities, or check editor and utility
> settings to ensure they are not set to behave like Windows e.g. gvim set
> fileformat=unix termencoding=utf-8 fileencoding=utf-8 in your ~/.gvimrc ~/.vimrc
> ~/.virc ~/.exrc; emacs (set-buffer-file-coding-system 'mule-utf-8-unix) in
> ~/.emacs, type C-x C-q C-m f mule-utf-8-unix, or do the equivalent in more
> sophisticated initializations (auto)detecting file type, encoding, and format;
> for git config --global core.autocrlf = input, see:
>
> https://stackoverflow.com/questions/3206843/how-line-ending-conversions-work-with-git-core-autocrlf-between-different-operat
>
> --
> Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

As a matter of expediency, I always pass files through '| tr -d '\r'
|' when reading them. This is harmless on Linux and you don't have to
worry whether or not your target system has 'd2u' installed since I
believe that 'tr' is part of all base installations. It also means you
don't have to worry about whether or not a particular program handles
'\r' automagically.

I find that it is also a good idea to save all script files as UTF-8
so that they can handle Windows filenames with emojis and such in them
when explicitly named in the script.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-10 18:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-10 13:47 General scripting issues vs. Linux Markus Becker
2023-03-10 13:59 ` Ken Brown
2023-03-10 16:15   ` Brian Inglis
2023-03-10 18:42     ` Backwoods BC

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).