I noticed the following example here [1]:
```
#!/bin/sh
if [ "$#" != "1" ]; then
echo "Usage: test.sh <string>"
exit 1
fi;
gap -r -b -q << EOI
LoadPackage( "CrystCat" );
DisplaySpaceGroupType( "$1" );
EOI
```
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
[1] https://stackoverflow.com/questions/13418849/how-can-i-call-gap-functions-from-a-shell-script
Regards,
HZ
I noticed the following example here [1]:
```
#!/bin/sh
if [ "$#" != "1" ]; then
echo "Usage: test.sh <string>"
exit 1
fi;
gap -r -b -q << EOI LoadPackage( "CrystCat" );
DisplaySpaceGroupType( "$1" );
EOI ```
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
I noticed the following example here [1]:
```
#!/bin/sh
if [ "$#" != "1" ]; then
echo "Usage: test.sh <string>"
exit 1
fi;
gap -r -b -q << EOI
LoadPackage( "CrystCat" );
DisplaySpaceGroupType( "$1" );
EOI
```
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
[1] https://stackoverflow.com/questions/13418849/how-can-i-call-gap-functions-from-a-shell-script
Regards,
HZ
I noticed the following example here [1]:
```
#!/bin/sh
if [ "$#" != "1" ]; then
echo "Usage: test.sh <string>"
exit 1
fi;
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
I see no /obvious/ reason.
On a (perhaps) related note, why did the author of that code snippet
use a string test rather than an an arithmetic test? To me, the
code snippet should read
I noticed the following example here [1]:
if [ "$#" != "1" ]; then
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
In our last episode, the evil Dr. Lacto had captured our hero, [email protected] <[email protected]>, who said:
I noticed the following example here [1]:
if [ "$#" != "1" ]; then
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?Consider a different variable
if [ $FOO != 1 ] ; then
if $FOO is completely uninitialized, then this will expand to
if [ != 1 ] ; then
which is a syntax error.
That is the reason that I, personally, almost always use the extra
quotation marks.
--hymie! http://nasalinux.net/~hymie [email protected]
On Thursday, May 12, 2022 at 3:49:49 AM UTC+8, hymie! wrote:
In our last episode, the evil Dr. Lacto had captured our hero,
[email protected] <[email protected]>, who said:
I noticed the following example here [1]:Consider a different variable
if [ "$#" != "1" ]; then
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
if [ $FOO != 1 ] ; then
if $FOO is completely uninitialized, then this will expand to
if [ != 1 ] ; then
which is a syntax error.
That is the reason that I, personally, almost always use the extra
quotation marks.
As commented by others in this thread, the following method will avoid this problem?
[ $# -ne 1 ]
--hymie! http://nasalinux.net/~hymie [email protected]
On 12.05.2022 03:35, [email protected] wrote:
On Thursday, May 12, 2022 at 3:49:49 AM UTC+8, hymie! wrote:
In our last episode, the evil Dr. Lacto had captured our hero,
[email protected] <[email protected]>, who said:
I noticed the following example here [1]:Consider a different variable
if [ "$#" != "1" ]; then
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
if [ $FOO != 1 ] ; then
if $FOO is completely uninitialized, then this will expand to
if [ != 1 ] ; then
which is a syntax error.
That is the reason that I, personally, almost always use the extra
quotation marks.
As commented by others in this thread, the following method will avoid this problem?
[ $# -ne 1 ]$FOO is an arbitrary user defined (or undefined) variable.
$# is a numeric variable set by the shell.
So [ $FOO -ne 1 ] will not avoid the issue.
Of course just trying that code would be faster than writing a post.
--hymie! http://nasalinux.net/~hymie [email protected]
On Thursday, May 12, 2022 at 3:49:49 AM UTC+8, hymie! wrote:
In our last episode, the evil Dr. Lacto had captured our hero,
[email protected] <[email protected]>, who said:
I noticed the following example here [1]:Consider a different variable
if [ "$#" != "1" ]; then
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
if [ $FOO != 1 ] ; then
if $FOO is completely uninitialized, then this will expand to
if [ != 1 ] ; then
which is a syntax error.
That is the reason that I, personally, almost always use the extra
quotation marks.
As commented by others in this thread, the following method will avoid
this problem?
[ $# -ne 1 ]
On Thursday, May 12, 2022 at 9:53:39 AM UTC+8, Janis Papanagnou wrote:
On 12.05.2022 03:35, [email protected] wrote:
On Thursday, May 12, 2022 at 3:49:49 AM UTC+8, hymie! wrote:$FOO is an arbitrary user defined (or undefined) variable.
In our last episode, the evil Dr. Lacto had captured our hero,
[email protected] <[email protected]>, who said:
I noticed the following example here [1]:Consider a different variable
if [ "$#" != "1" ]; then
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
if [ $FOO != 1 ] ; then
if $FOO is completely uninitialized, then this will expand to
if [ != 1 ] ; then
which is a syntax error.
That is the reason that I, personally, almost always use the extra
quotation marks.
As commented by others in this thread, the following method will avoid this problem?
[ $# -ne 1 ]
$# is a numeric variable set by the shell.
So [ $FOO -ne 1 ] will not avoid the issue.
Of course just trying that code would be faster than writing a post.
Yes. All the problems discussed in this post can be illustrated by the following examples:
$ [ "$FOO" = 1 ]; echo $?
1
$ [ "$FOO" = "1" ]; echo $?
1
$ [ $FOO -ne 1 ]
bash: [: -ne: unary operator expected
$ [ "$FOO" -ne 1 ]
bash: [: : integer expression expected
On 12.05.2022 05:42, [email protected] wrote:
On Thursday, May 12, 2022 at 9:53:39 AM UTC+8, Janis Papanagnou wrote:
On 12.05.2022 03:35, [email protected] wrote:
On Thursday, May 12, 2022 at 3:49:49 AM UTC+8, hymie! wrote:$FOO is an arbitrary user defined (or undefined) variable.
In our last episode, the evil Dr. Lacto had captured our hero,
[email protected] <[email protected]>, who said:
I noticed the following example here [1]:Consider a different variable
if [ "$#" != "1" ]; then
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
if [ $FOO != 1 ] ; then
if $FOO is completely uninitialized, then this will expand to
if [ != 1 ] ; then
which is a syntax error.
That is the reason that I, personally, almost always use the extra
quotation marks.
As commented by others in this thread, the following method will avoid this problem?
[ $# -ne 1 ]
$# is a numeric variable set by the shell.
So [ $FOO -ne 1 ] will not avoid the issue.
Of course just trying that code would be faster than writing a post.
Yes. All the problems discussed in this post can be illustrated by the following examples:
$ [ "$FOO" = 1 ]; echo $?In your thread you have asked a question about using the test command
1
$ [ "$FOO" = "1" ]; echo $?
1
$ [ $FOO -ne 1 ]
bash: [: -ne: unary operator expected
$ [ "$FOO" -ne 1 ]
bash: [: : integer expression expected
and quoting to address some issues and, based on some sample code from
the net, understand how it works best. In this context it is noteworthy
that (for own programs) you have more (and better) choices. Some of the issues and caveats that the historic test command and shell's command
syntax with test has (which unfortunately is also the only standard
base for standard shell) are fixed or alleviated with the test syntax
[[...]] that the powerful "modern" shells (ksh, bash, zsh, maybe others
too) support. (In this vein inspect for arithmetic contexts also the arithmetic command ((...)) construct.)
I suggest to try these contemporary constructs with code samples like
the ones you started with above, vary the data from undefined, empty, defined, defined with spaces, use of shell-patterns for comparisons,
use various comparison operators (!=, ==, =) etc., compare it with the
old (standard) syntax, and observe the difference in behavior, the consistency, whether it meets your expectations, or whether one or the
other surprises you. Then draw your conclusions.
JanisHZ
On Wed, 11 May 2022 18:35:24 -0700, [email protected] wrote:
On Thursday, May 12, 2022 at 3:49:49 AM UTC+8, hymie! wrote:
In our last episode, the evil Dr. Lacto had captured our hero,
[email protected] <[email protected]>, who said:
I noticed the following example here [1]:Consider a different variable
if [ "$#" != "1" ]; then
It seems that [ $# != 1 ] is enough. Why use [ "$#" != "1" ] here?
if [ $FOO != 1 ] ; then
if $FOO is completely uninitialized, then this will expand to
if [ != 1 ] ; then
which is a syntax error.
That is the reason that I, personally, almost always use the extra
quotation marks.
As commented by others in this thread, the following method will avoid
this problem?
[ $# -ne 1 ]As a specific solution, yes.
$# is guaranteed to contain a numeric count of the number of arguments
passed into a script. It *cannot* be empty, or contain space delimited values, so bracketing it in doublequotes is redundant. Both sides of the
test contain a decimal values, so a numeric test (such as -ne) would be appropriate.
OTOH, as a general solution, not so much.
In the test
[ $FOO -ne $BAR ]
neither $FOO nor $BAR provide the content guarantees that $# does (not
empty, not space delimited string, numeric value only) so it would be
prudent to bracket each with doublequotes. Also, given that neither guarantees a numeric value, it would be prudent to use the != test
instead of the -ne test, making the /prudent/ test
[ "$FOO" != "$BAR" ]
--
Lew Pitcher
"In Skills, We Trust"
$# is guaranteed to contain a numeric count of the number of arguments
passed into a script. It *cannot* be empty, or contain space delimited values, so bracketing it in doublequotes is redundant.
Lew Pitcher wrote:
$# is guaranteed to contain a numeric count of the number of arguments
passed into a script. It *cannot* be empty, or contain space delimited
values, so bracketing it in doublequotes is redundant.
Double quotes around $# are redundant only if there are no digits in
IFS.
That would certainly be true for the case the OP asked about, where the
$# expansion was in the first command executed in the script. But it
can't be assumed to be true in general.
Lew Pitcher wrote:
$# is guaranteed to contain a numeric count of the number of arguments
passed into a script. It *cannot* be empty, or contain space delimited
values, so bracketing it in doublequotes is redundant.
Double quotes around $# are redundant only if there are no digits in IFS.
That would certainly be true for the case the OP asked about, where the
$# expansion was in the first command executed in the script. But it
can't be assumed to be true in general.
Lew Pitcher wrote:
$# is guaranteed to contain a numeric count of the number of arguments passed into a script. It *cannot* be empty, or contain space delimited values, so bracketing it in doublequotes is redundant.Double quotes around $# are redundant only if there are no digits in IFS.
That would certainly be true for the case the OP asked about, where the
$# expansion was in the first command executed in the script. But it
can't be assumed to be true in general.
--
Geoff Clare <[email protected]>
It's empty on my machine (Ubuntu 20.04.3 LTS):
$ echo $IFS
$
On Sat, 14 May 2022 22:15:41 -0700 (PDT), "[email protected]" <[email protected]> wrote:
It's empty on my machine (Ubuntu 20.04.3 LTS):
$ echo $IFS
$No, it isn't empty. The characters it contains just do not leave
ink on your canvas.
user@host:~ $ printf '>%s<' "$IFS"|hexdump -C
00000000 3e 20 09 0a 3c |> ..<|
00000005
On Sunday, May 15, 2022 at 4:57:34 PM UTC+8, Kees Nuyt wrote:
On Sat, 14 May 2022 22:15:41 -0700 (PDT), "[email protected]"
<[email protected]> wrote:
It's empty on my machine (Ubuntu 20.04.3 LTS):No, it isn't empty. The characters it contains just do not leave
$ echo $IFS
$
ink on your canvas.
user@host:~ $ printf '>%s<' "$IFS"|hexdump -C
00000000 3e 20 09 0a 3c |> ..<|
00000005
I get the following using od:
$ printf '>%s<' "$IFS"| od -xc --endian=big
0000000 3e20 090a 3c00
> \t \n <
0000005
Why aren't they exactly the same?
HZ
On 15.05.2022 15:31, [email protected] wrote:
On Sunday, May 15, 2022 at 4:57:34 PM UTC+8, Kees Nuyt wrote:
On Sat, 14 May 2022 22:15:41 -0700 (PDT), "[email protected]"
<[email protected]> wrote:
It's empty on my machine (Ubuntu 20.04.3 LTS):No, it isn't empty. The characters it contains just do not leave
$ echo $IFS
$
ink on your canvas.
user@host:~ $ printf '>%s<' "$IFS"|hexdump -C
00000000 3e 20 09 0a 3c |> ..<|
00000005
I get the following using od:
$ printf '>%s<' "$IFS"| od -xc --endian=big
0000000 3e20 090a 3c00
\t \n <0000005
Why aren't they exactly the same?The output isn't the same because you used a different program.
The IFS value is the same. Of course the unnecessary > and < will
distract you from the IFS dedails in the od/hexdump output. Keep
it simple
$ printf '%s' "$IFS" | od -t x1
0000000 20 09 0a
That's a Blank, a Tab, and a Newline.
I checked them with the following command:
$ ascii -t 0x20 0x09 0x0a
2/0 32 0x20 0o40 00100000
0/9 9 0x09 0o11 00001001
0/10 10 0x0A 0o12 00001010
How to retrieve the character names at the same time?
On Mon, 16 May 2022 02:00:27 -0700 (PDT), "[email protected]" <[email protected]> wrote:
I checked them with the following command:
$ ascii -t 0x20 0x09 0x0a
2/0 32 0x20 0o40 00100000
0/9 9 0x09 0o11 00001001
0/10 10 0x0A 0o12 00001010
How to retrieve the character names at the same time?1: Explore the options in 'man -s 1 ascii' and try
ascii -a 0xHH ...
plus a bit of awk code
2: You don't have to, because from experience you know
0x20 is space, 0x09 is TAB and 0x0A is linefeed
3: Use the lookup table in 'man -s 7 ascii'
You just failed the quiz.
--
Kees Nuyt
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 714 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 140:50:33 |
| Calls: | 12,087 |
| Files: | 14,998 |
| Messages: | 6,517,429 |