public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Gene Pavlovsky <gene.pavlovsky@gmail.com>
To: cygwin@cygwin.com
Subject: Re: Bash shell script issue
Date: Fri, 09 Sep 2016 19:37:00 -0000	[thread overview]
Message-ID: <CAPTiy3MNiVnnRF9MNym-VCMEMCt6FRTrKKAWx2AdS3_3k6VNXg@mail.gmail.com> (raw)
In-Reply-To: <eb1c8bc2-6f49-47ae-73ee-7acefd7fefc8@redhat.com>

On 7 September 2016 at 21:27, Eric Blake <eblake@redhat.com> wrote:
>>> project_root=$PWD
>>
>> Still bad.
>
> No, that one's good.  Quotes are only REQUIRED when you want to ensure
> that there is no word splitting or globbing going on, and assignment
> context has neither of those.  However, although the quotes are optional
> in that context, using them out of habit makes it easier to remember to
> use quotes in the contexts where it does matter.

I was going to say that. Consider the following example:

# path1='/mnt/c/Program Files (x86)'
22:12:49 ~
# path2=$path1
22:12:54 ~
# path3=$path2/tools
22:13:00 ~
# echo $path1
/mnt/c/Program Files (x86)
22:13:05 ~
# echo $path2
/mnt/c/Program Files (x86)
22:13:06 ~
# echo $path3
/mnt/c/Program Files (x86)/tools

Assignment requires no extra quoting.
However if you try
# test -d $path2 && echo $path2 is a directory
bash: test: too many arguments

Because $path2 becomes split by space into words, while `test -d`
expects only one argument.
# test -d "$path2" && echo $path2 is a directory
/mnt/c/Program Files (x86) is a directory

This is really bash basics, and you should read Bash Beginner's Guide:
http://tldp.org/LDP/Bash-Beginners-Guide/html/

And just remember that UNIX tools such as bash, sed, awk etc. expect
scripts to use UNIX-style line endings (one LF character ends the
line), as opposed to a pair of CR and LF characters used in Windows.
Whenever you encounter problems or error messages that don't seem to
make any sense, check the file line endings. You can use the `file`
command to check the file's line endings. An example bash session:

22:22:49 ~
# cat >test.sh <<EOF
> #!/bin/sh
>
> echo line ending test script
> EOF
22:23:29 ~
# chmod 755 test.sh
22:23:14 ~
# u2d test.sh
unix2dos: converting file test.sh to DOS format...
22:23:24 ~
# file test.sh
test.sh: POSIX shell script, ASCII text executable, with CRLF line terminators
22:23:36 ~
# ./test.sh
./test.sh: line 2: $'\r': command not found
line ending test script
22:23:37 ~
# d2u test.sh
dos2unix: converting file test.sh to Unix format...
22:23:41 ~
# file test.sh
test.sh: POSIX shell script, ASCII text executable
22:23:43 ~
# ./test.sh
line ending test script

Here I've created a test shell script, and deliberately converted it
to DOS (Windows) line endings. The `file` command explicitly tells you
it has CRLF line terminators.
Next try to run the script and it will not work, because the
supposedly empty line is considered as a single `\r` (CR) character,
which is not a valid command.
After converting the file back to UNIX line endings, the `file`
command doesn't mention anything about line endings, because they are
the standard/default/normal UNIX-style ones (`\n` = LF). The script
runs.

Personally I use Notepad2-mod to edit shell scripts, the `File > Line
Endings` shows you the current line endings, allows to convert between
different line ending styles, and allows to set UNIX style as default.
Link if you're interested: http://xhmikosr.io/notepad2-mod/

> No, that one's okay.  In fact, if you are going for minimalism, you
> could use:
>
> x=$project_root/tools
>
> as the ${} form is only REQUIRED when you are using operators inside {}
> or when the next character after } is ambiguous with a continuation of
> the name of the variable in after the $.

Minimalism is good, but when writing anything more or less
complicated, consistent style and coding conventions become more
important. Quoting a variable in one context, but not the other, using
${var1}_${var2} here, but plain $var1$var2 etc. - it will work, but it
might not be best idea to code/script like this. Same as sometimes
it's better to put an extra set of otherwise unnecessary parenthesis
in a math expression, just to group terms and make the expression more
readable and operator precedence more obvious.
Bash has a lot of different syntax, and beginners would bump into less
problems if they overquote rather than underquote...
This is personal opinion based on my software development experience,
mostly working with high-level languages, involving complicated
architectural frameworks: sometimes you write more than strictly
necessary, to prevent problems in the future, make code more readable,
maintainable etc.

Regards,
--Gene

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

  reply	other threads:[~2016-09-09 19:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <330568691.2384551.1473201409220.ref@mail.yahoo.com>
2016-09-06 22:39 ` Kipton Moravec
2016-09-07  1:21   ` Eric Blake
2016-09-07  1:41     ` Michel LaBarre
2016-09-07 15:35       ` Kipton Moravec
2016-09-07 15:49         ` Marco Atzeri
2016-09-07 17:41           ` Erik Soderquist
2016-09-07 16:05         ` Andrey Repin
2016-09-07 16:25           ` Kipton Moravec
2016-09-07 16:22     ` Kipton Moravec
2016-09-07 17:35       ` Andrey Repin
2016-09-07 18:09         ` Eliot Moss
2016-09-07 18:28         ` Eric Blake
2016-09-09 19:37           ` Gene Pavlovsky [this message]
2016-09-07 19:16       ` Brian Inglis
     [not found]   ` <32e004cb44addbaefde0839df5500d60@www.ds.net>
     [not found]     ` <039bcc69d005bf6db5aba99a90f51e36@www.ds.net>
2016-09-07 17:54       ` wilson
2016-09-07 18:13         ` Eliot Moss

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAPTiy3MNiVnnRF9MNym-VCMEMCt6FRTrKKAWx2AdS3_3k6VNXg@mail.gmail.com \
    --to=gene.pavlovsky@gmail.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).