Skip to contentSkip to conversation

Fix some bashisms

PATCH Garrett Cooper 3 participants 7 messages Jul 6, 2009 – Jul 7, 2009 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Jiri,
This patch looks ok for the most part, but here are some comments:

1. if [[ -z "$vnet0" -z "$vnet1" ]] => if [ -z "$vnet0" ] || [ -z "$vnet1" ]

It's better read as the following, IMO:

[ -z "$vnet0" -o -z "$vnet1" ]

2. All numeric test(1) comparisons could and should be switched from:

command
if [ $? = 0 ]; then

to:

if command; then

for brevity.

3. About &>

Redirecting Standard Output and Standard Error
Bash allows both the standard output (file descriptor 1) and
the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.

       There are two formats for redirecting standard output and standard error:

              &>word
       and
              >&word

The output redirection above captures all output (and most of the time you're capturing stdout, but losing stderr to the operating console, which can be undesirable). >& is more portable IIRC (it works with many ash variants, like FreeBSD's sh -- for sure -- and Solaris's sh -- IIRC), which makes it more ideal than &>.

Thanks,
-Garrett

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#2 lore raw
Quoted 10 lines · On Monday 06 July 2009 13:45:08 Garrett Cooper wrote:

2. All numeric test(1) comparisons could and should be switched from:

command
if [ $? = 0 ]; then

to:

if command; then

for brevity.

that works the command is sufficiently short. if it's a long command and/or has line wrappings itself, i find nesting it in the if statement makes the situation much worse.

Quoted 15 lines

3. About &>

Redirecting Standard Output and Standard Error
Bash allows both the standard output (file descriptor 1) and
the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.

There are two formats for redirecting standard output and standard
error:

              &>word
       and

              >&word
Quoted 5 lines

The output redirection above captures all output (and most of the time you're capturing stdout, but losing stderr to the operating console, which can be undesirable). >& is more portable IIRC (it works with many ash variants, like FreeBSD's sh -- for sure -- and Solaris's sh -- IIRC), which makes it more ideal than &>.

what bash does doesnt matter. the redirection used needs to be in POSIX:
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07

the "&>" form is not in POSIX, so should never be used. the ">&" format cannot be used in POSIX without a word argument (you didnt say it could, just making sure things are kept explicit).
-mike

------------------------------------------------------------------------------

_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#3 lore raw
In reply to Garrett Cooper
Quoted 8 lines · On Monday 06 July 2009 19:45:08 Garrett Cooper wrote:

Jiri,
This patch looks ok for the most part, but here are some comments:

1. if [[ -z "$vnet0" -z "$vnet1" ]] => if [ -z "$vnet0" ] || [ -z "$vnet1" ]

It's better read as the following, IMO:

[ -z "$vnet0" -o -z "$vnet1" ]

I think this is another "I like this more than that, because this is cool and that sucks" debate. If other think test's "-o" should be used in this case, I would make the change. After all, this can be done by hand directly on the patch...

Quoted 10 lines

2. All numeric test(1) comparisons could and should be switched from:

command
if [ $? = 0 ]; then

to:

if command; then

for brevity.

Why should them be changed? This is only a cosmetic change, which would make the patch unnecessarily larger (when it's already a little too large IMHO). If it could be in the former form till now, I think it can stay there a few months/years.

IMHO this usage is still much better than

command; RC=$?
if [ $RC = 0 ] ...
... RC is not read later ...

or (incorrect)

command || RC=$?
if [ $RC = 0 ] ...

Quoted 13 lines

3. About &>

Redirecting Standard Output and Standard Error
Bash allows both the standard output (file descriptor 1) and
the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.

       There are two formats for redirecting standard output and standard error:

              &>word
       and
              >&word
Quoted 5 lines

The output redirection above captures all output (and most of the time you're capturing stdout, but losing stderr to the operating console, which can be undesirable). >& is more portable IIRC (it works with many ash variants, like FreeBSD's sh -- for sure -- and Solaris's sh -- IIRC), which makes it more ideal than &>.

Still, it isn't POSIX, it doesn't work with dash and actually causes pain. See:

cmd &> fn

POSIX: run cmd in background, write empty file fn
BASH: run cmd (foreground), send its (normal & error) output to file fn

cmd >& fn

POSIX: syntax error
BASH: same as above

But I don't see the point you are making here - do you have a system, where the POSIX way

cmd > fn 2>&1

doesn't work? Or did you see something else in the patch?

Regards
Jiri Palecek

BTW: Jiri Palecek <[email protected]> is not my address.

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#4 lore raw
Quoted 11 lines · Jiri Palecek
Quoted 8 lines · On Monday 06 July 2009 19:45:08 Garrett Cooper wrote:

Jiri,
    This patch looks ok for the most part, but here are some comments:

1. if [[ -z "$vnet0" -z "$vnet1" ]] => if [ -z "$vnet0" ] || [ -z "$vnet1" ]

It's better read as the following, IMO:

[ -z "$vnet0" -o -z "$vnet1" ]

I think this is another "I like this more than that, because this is cool and that sucks" debate. If other think test's "-o" should be used in this case, I would make the change. After all, this can be done by hand directly on the patch...

Why call [(1) twice?

Quoted 15 lines
Quoted 10 lines

2. All numeric test(1) comparisons could and should be switched from:

command
if [ $? = 0 ]; then

to:

if command; then

for brevity.

Why should them be changed? This is only a cosmetic change, which would make the patch unnecessarily larger (when it's already a little too large IMHO). If it could be in the former form till now, I think it can stay there a few months/years.

IMHO this usage is still much better than

Quoted 8 lines

 command; RC=$?
 if [ $RC = 0 ] ...
 ... RC is not read later ...

or (incorrect)

 command || RC=$?
 if [ $RC = 0 ] ...

Yes, I agree with what you said about the above usage, but if you're not using $? for other than just the [ $? -ne 0 ], then I wouldn't even bother doing that, because if command; then saves a fork-exec, and an additional line in the source. As long as it's readable and doesn't expand multiple lines with a trailing \, you're fine.

Quoted 28 lines
Quoted 13 lines

3. About &>

   Redirecting Standard Output and Standard Error
       Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.

       There are two formats for redirecting standard output and standard error:

              &>word
       and
              >&word

Quoted 5 lines

The output redirection above captures all output (and most of the time you're capturing stdout, but losing stderr to the operating console, which can be undesirable). >& is more portable IIRC (it works with many ash variants, like FreeBSD's sh -- for sure -- and Solaris's sh -- IIRC), which makes it more ideal than &>.

Still, it isn't POSIX, it doesn't work with dash and actually causes pain. See:

 cmd &> fn

POSIX: run cmd in background, write empty file fn
BASH: run cmd (foreground), send its (normal & error) output to file fn

 cmd >& fn

Quoted 8 lines

POSIX: syntax error
BASH: same as above

But I don't see the point you are making here - do you have a system, where the POSIX way

 cmd > fn 2>&1

doesn't work? Or did you see something else in the patch?

This changes the behavior of the patch though. That's the point ;)...

Quoted 4 lines

Regards
   Jiri Palecek

BTW: Jiri Palecek <[email protected]> is not my address.

Ok. Your original REPLY-TO email is wonky in your emails though, so I was playing it by ear...

Thanks,
-Garrett

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/blackberry
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#5 lore raw
Quoted 10 lines · Garrett Cooper
Quoted 9 lines · Jiri Palecek
Quoted 8 lines · On Monday 06 July 2009 19:45:08 Garrett Cooper wrote:

Jiri,
    This patch looks ok for the most part, but here are some comments:

1. if [[ -z "$vnet0" -z "$vnet1" ]] => if [ -z "$vnet0" ] || [ -z "$vnet1" ]

It's better read as the following, IMO:

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

[ -z "$vnet0" -o -z "$vnet1" ]

I think this is another "I like this more than that, because this is cool and that sucks" debate. If other think test's "-o" should be used in this case, I would make the change. After all, this can be done by hand directly on the patch...

Why call [(1) twice?

Does it (measurably) matter? Do the shell scripts contribute a big part to the time the tests take?

Quoted 24 lines
Quoted 24 lines
Quoted 10 lines

2. All numeric test(1) comparisons could and should be switched from:

command
if [ $? = 0 ]; then

to:

if command; then

for brevity.

Why should them be changed? This is only a cosmetic change, which would make the patch unnecessarily larger (when it's already a little too large IMHO). If it could be in the former form till now, I think it can stay there a few months/years.

IMHO this usage is still much better than

 command; RC=$?
 if [ $RC = 0 ] ...
 ... RC is not read later ...

or (incorrect)

Quoted 8 lines
Quoted 2 lines

 command || RC=$?
 if [ $RC = 0 ] ...

Yes, I agree with what you said about the above usage, but if you're not using $? for other than just the [ $? -ne 0 ], then I wouldn't even bother doing that, because if command; then saves a fork-exec, and an additional line in the source. As long as it's readable and doesn't expand multiple lines with a trailing \, you're fine.

Are you talking only about efficiency and bash scripts? :-)

Anyway, if you want to save the fork-exec, you can just use a shell with a test builtin (ksh, bash, dash ... and I believe many others do).

If you are concerned about readability, I suggest considering another alternative:

  command || {
    do_something_on_error
  }

  command && {
    do_something_when_ok
  }

Of coures, usable only when there's no "else" branch.

Still, I think the original form is correct and quite straightforward. I would probably not use the extra test myself, but I don't feel like it needs changing. Just looking at the patch, I see places where the check is done really suspiciously:

tst_resm TINFO "Test #1: changing mtu size of eth0:1 device."

- ip link set eth0:1 mtu 300 &>$LTPTMP/tst_ip.err
+ ip link set eth0:1 mtu 300 >$LTPTMP/tst_ip.err 2>&1
if [ $RC -ne 0 ]
then
tst_res TFAIL $LTPTMP/tst_ip.err \

I think this needs much more attention than "calling [ twice" or "extra line in the script".

Quoted 33 lines
Quoted 33 lines
Quoted 14 lines

3. About &>

   Redirecting Standard Output and Standard Error
       Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.

       There are two formats for redirecting standard output and standard error:

              &>word
       and
              >&word

Quoted 5 lines

The output redirection above captures all output (and most of the time you're capturing stdout, but losing stderr to the operating console, which can be undesirable). >& is more portable IIRC (it works with many ash variants, like FreeBSD's sh -- for sure -- and Solaris's sh -- IIRC), which makes it more ideal than &>.

Still, it isn't POSIX, it doesn't work with dash and actually causes pain. See:

 cmd &> fn

POSIX: run cmd in background, write empty file fn
BASH: run cmd (foreground), send its (normal & error) output to file fn

 cmd >& fn

POSIX: syntax error
BASH: same as above

Quoted 8 lines
Quoted 6 lines

But I don't see the point you are making here - do you have a system, where the POSIX way

 cmd > fn 2>&1

doesn't work? Or did you see something else in the patch?

This changes the behavior of the patch though. That's the point ;)...

How does it change the semantics? It redirects standard output to fn, and standard error to standard output (which is redirected to fn). Is there anything else it should do?

Regards
Jiri Palecek

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/blackberry
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#6 lore raw

2009/7/6 Jiří Paleček <[email protected]>:

Quoted 54 lines
Quoted 12 lines · Garrett Cooper
Quoted 11 lines · Jiri Palecek
Quoted 9 lines · On Monday 06 July 2009 19:45:08 Garrett Cooper wrote:

Jiri,
    This patch looks ok for the most part, but here are some comments:

1. if [[ -z "$vnet0" -z "$vnet1" ]] => if [ -z "$vnet0" ] || [ -z "$vnet1" ]

It's better read as the following, IMO:

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

[ -z "$vnet0" -o -z "$vnet1" ]

I think this is another "I like this more than that, because this is cool and that sucks" debate. If other think test's "-o" should be used in this case, I would make the change. After all, this can be done by hand directly on the patch...

Why call [(1) twice?

Does it (measurably) matter? Do the shell scripts contribute a big part to the time the tests take?

Quoted 27 lines
Quoted 26 lines
Quoted 10 lines

2. All numeric test(1) comparisons could and should be switched from:

command
if [ $? = 0 ]; then

to:

if command; then

for brevity.

Why should them be changed? This is only a cosmetic change, which would make the patch unnecessarily larger (when it's already a little too large IMHO). If it could be in the former form till now, I think it can stay there a few months/years.

IMHO this usage is still much better than

 command; RC=$?
 if [ $RC = 0 ] ...
 ... RC is not read later ...

or (incorrect)

 command || RC=$?
 if [ $RC = 0 ] ...

Quoted 7 lines
Quoted 5 lines

Yes, I agree with what you said about the above usage, but if you're not using $? for other than just the [ $? -ne 0 ], then I wouldn't even bother doing that, because if command; then saves a fork-exec, and an additional line in the source. As long as it's readable and doesn't expand multiple lines with a trailing \, you're fine.

Are you talking only about efficiency and bash scripts? :-)

Efficiency, and more importantly readability.

Quoted 10 lines

Anyway, if you want to save the fork-exec, you can just use a shell with a test builtin (ksh, bash, dash ... and I believe many others do).

If you are concerned about readability, I suggest considering another alternative:

 command || {
  do_something_on_error
 }

Quoted 5 lines

 command && {
  do_something_when_ok
 }

Of course, usable only when there's no "else" branch.

That's ugly and hard to read with large logic blocks.

Quoted 8 lines

Still, I think the original form is correct and quite straightforward. I would probably not use the extra test myself, but I don't feel like it needs changing. Just looking at the patch, I see places where the check is done really suspiciously:

       tst_resm TINFO "Test #1: changing mtu size of eth0:1 device."

Quoted 8 lines

-       ip link set eth0:1 mtu 300 &>$LTPTMP/tst_ip.err
+       ip link set eth0:1 mtu 300 >$LTPTMP/tst_ip.err 2>&1 if [ $RC -ne 0 ]
       then
               tst_res TFAIL $LTPTMP/tst_ip.err \

I think this needs much more attention than "calling [ twice" or "extra line in the script".

Yeah, true. Why not run everything through set -u then?

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 14 lines

3. About &>

   Redirecting Standard Output and Standard Error
       Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word with this construct.

       There are two formats for redirecting standard output and standard error:

              &>word
       and
              >&word

Quoted 5 lines

The output redirection above captures all output (and most of the time you're capturing stdout, but losing stderr to the operating console, which can be undesirable). >& is more portable IIRC (it works with many ash variants, like FreeBSD's sh -- for sure -- and Solaris's sh -- IIRC), which makes it more ideal than &>.

Still, it isn't POSIX, it doesn't work with dash and actually causes pain. See:

 cmd &> fn

POSIX: run cmd in background, write empty file fn
BASH: run cmd (foreground), send its (normal & error) output to file fn

 cmd >& fn

POSIX: syntax error
BASH: same as above

But I don't see the point you are making here - do you have a system, where the POSIX way

 cmd > fn 2>&1

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

doesn't work? Or did you see something else in the patch?

This changes the behavior of the patch though. That's the point ;)...

How does it change the semantics? It redirects standard output to fn, and standard error to standard output (which is redirected to fn). Is there anything else it should do?

Ugh. Nevermind -- you're right ><. Keep on forgetting about that bloody syntax. -Garrett

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/blackberry
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#7 lore raw
Quoted 59 lines · Garrett Cooper

2009/7/6 Jiří Paleček <[email protected]>:

Quoted 37 lines
Quoted 34 lines · Garrett Cooper
Quoted 32 lines · Jiri Palecek
Quoted 11 lines · On Monday 06 July 2009 19:45:08 Garrett Cooper wrote:

2. All numeric test(1) comparisons could and should be switched from:

command
if [ $? = 0 ]; then

to:

if command; then

for brevity.

Why should them be changed? This is only a cosmetic change, which would
make the patch unnecessarily larger (when it's already a little too large
IMHO). If it could be in the former form till now, I think it can stay there
a few months/years.

IMHO this usage is still much better than

 command; RC=$?
 if [ $RC = 0 ] ...
 ... RC is not read later ...

or (incorrect)

 command || RC=$?
 if [ $RC = 0 ] ...

Quoted 7 lines
Quoted 5 lines

Yes, I agree with what you said about the above usage, but if you're not using $? for other than just the [ $? -ne 0 ], then I wouldn't even bother doing that, because if command; then saves a fork-exec, and an additional line in the source. As long as it's readable and doesn't expand multiple lines with a trailing \, you're fine.

Are you talking only about efficiency and bash scripts? :-)

Efficiency, and more importantly readability.

Quoted 11 lines

Anyway, if you want to save the fork-exec, you can just use a shell with a
test builtin (ksh, bash, dash ... and I believe many others do).

If you are concerned about readability, I suggest considering another alternative:

 command || {
  do_something_on_error
 }

Quoted 7 lines
Quoted 5 lines

 command && {
  do_something_when_ok
 }

Of course, usable only when there's no "else" branch.

That's ugly and hard to read with large logic blocks.

I don't think so; I have no problems with a similar syntax in C, moreover, I consider the braces more distinguishable than keywords.

Quoted 16 lines
Quoted 16 lines

Still, I think the original form is correct and quite straightforward. I would probably not use the extra test myself, but I don't feel like it needs
changing. Just looking at the patch, I see places where the check is done
really suspiciously:

       tst_resm TINFO "Test #1: changing mtu size of eth0:1 device."

-       ip link set eth0:1 mtu 300 &>$LTPTMP/tst_ip.err
+       ip link set eth0:1 mtu 300 >$LTPTMP/tst_ip.err 2>&1 if [ $RC -ne 0 ]
       then
               tst_res TFAIL $LTPTMP/tst_ip.err \

Quoted 5 lines
Quoted 3 lines

I think this needs much more attention than "calling [ twice" or "extra line
in the script".

Yeah, true. Why not run everything through set -u then?

It would not help, IMO. At least in this particular case, the variable is properly initialized.

Regards
Jiri Palecek

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/blackberry
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list