Skip to main content
added 221 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

The exit status of a for loop compound command is that of the last command executed in itit¹.

for cmd in true false; do
  "$cmd"
done

Returns false (a non-zero exit status) because false was the last command run.

echo will return true as long as it successfully manages to write what we're telling it to.

If you want to return false/failure if any of the tidy command fails, you'd need to either record the failure, or exit upon the first failure:

#! /bin/bash -
shopt -s globstar
ok=true
for i in ./**/*.html; do
  if tidy -mq "$i"; then
    printf '%s\n' "$i"
  else
    ok=false
  fi
done
"$ok"

or:

#! /bin/bash -
shopt -s globstar
for i in ./**/*.html; do
  tidy -mq "$i" || exit # if tidy fails
  printf '%s\n' "$i"
done

That one could still return false/failure if printf fails (for instance, when stdout is to a file on a filesystem that is full).

If you want to ignore any error and your script to return true/success in any case, just add a true or exit 0 at the end of your script.


¹ at least for the body part. For for i in $(exit 42); do :; done, most shells return 0 (AT&T ksh being the exception). They all return 0 for for i in; do :; done < "$(echo /dev/null; exit 42)".

The exit status of a for loop compound command is that of the last command executed in it.

for cmd in true false; do
  "$cmd"
done

Returns false (a non-zero exit status) because false was the last command run.

echo will return true as long as it successfully manages to write what we're telling it to.

If you want to return false/failure if any of the tidy command fails, you'd need to either record the failure, or exit upon the first failure:

#! /bin/bash -
shopt -s globstar
ok=true
for i in ./**/*.html; do
  if tidy -mq "$i"; then
    printf '%s\n' "$i"
  else
    ok=false
  fi
done
"$ok"

or:

#! /bin/bash -
shopt -s globstar
for i in ./**/*.html; do
  tidy -mq "$i" || exit # if tidy fails
  printf '%s\n' "$i"
done

That one could still return false/failure if printf fails (for instance, when stdout is to a file on a filesystem that is full).

If you want to ignore any error and your script to return true/success in any case, just add a true or exit 0 at the end of your script.

The exit status of a for loop compound command is that of the last command executed in it¹.

for cmd in true false; do
  "$cmd"
done

Returns false (a non-zero exit status) because false was the last command run.

echo will return true as long as it successfully manages to write what we're telling it to.

If you want to return false/failure if any of the tidy command fails, you'd need to either record the failure, or exit upon the first failure:

#! /bin/bash -
shopt -s globstar
ok=true
for i in ./**/*.html; do
  if tidy -mq "$i"; then
    printf '%s\n' "$i"
  else
    ok=false
  fi
done
"$ok"

or:

#! /bin/bash -
shopt -s globstar
for i in ./**/*.html; do
  tidy -mq "$i" || exit # if tidy fails
  printf '%s\n' "$i"
done

That one could still return false/failure if printf fails (for instance, when stdout is to a file on a filesystem that is full).

If you want to ignore any error and your script to return true/success in any case, just add a true or exit 0 at the end of your script.


¹ at least for the body part. For for i in $(exit 42); do :; done, most shells return 0 (AT&T ksh being the exception). They all return 0 for for i in; do :; done < "$(echo /dev/null; exit 42)".

added 286 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

The exit status of a for loop compound command is that of the last command executed in it.

for cmd in true false; do
  "$cmd"
done

Returns false (a non-zero exit status) because false was the last command run.

echo will return true as long as it successfully manages to write what we're telling it to.

If you want to return false/failure if any of the tidy command fails, you'd need to either record the failure, or exit upon the first failure:

#! /bin/bash -
shopt -s globstar
ok=true
for i in ./**/*.html; do
  if tidy -mq "$i"; then
    printf '%s\n' "$i"
  else
    ok=false
  fi
done
"$ok"

or:

#! /bin/bash -
shopt -s globstar
for i in ./**/*.html; do
  tidy -mq "$i" || exit # if tidy fails
  printf '%s\n' "$i"
done

That one could still return false/failure if printf fails (for instance, when stdout is to a file on a filesystem that is full).

If you want to ignore any error and your script to return true/success in any case, just add a true or exit 0 at the end of your script.

The exit status of a for loop compound command is that of the last command executed in it.

for cmd in true false; do
  "$cmd"
done

Returns false (a non-zero exit status) because false was the last command run.

echo will return true as long as it successfully manages to write what we're telling it to.

If you want to return false/failure if any of the tidy command fails, you'd need to either record the failure, or exit upon the first failure:

#! /bin/bash -
shopt -s globstar
ok=true
for i in ./**/*.html; do
  if tidy -mq "$i"; then
    printf '%s\n' "$i"
  else
    ok=false
  fi
done
"$ok"

or:

#! /bin/bash -
shopt -s globstar
for i in ./**/*.html; do
  tidy -mq "$i" || exit # if tidy fails
  printf '%s\n' "$i"
done

The exit status of a for loop compound command is that of the last command executed in it.

for cmd in true false; do
  "$cmd"
done

Returns false (a non-zero exit status) because false was the last command run.

echo will return true as long as it successfully manages to write what we're telling it to.

If you want to return false/failure if any of the tidy command fails, you'd need to either record the failure, or exit upon the first failure:

#! /bin/bash -
shopt -s globstar
ok=true
for i in ./**/*.html; do
  if tidy -mq "$i"; then
    printf '%s\n' "$i"
  else
    ok=false
  fi
done
"$ok"

or:

#! /bin/bash -
shopt -s globstar
for i in ./**/*.html; do
  tidy -mq "$i" || exit # if tidy fails
  printf '%s\n' "$i"
done

That one could still return false/failure if printf fails (for instance, when stdout is to a file on a filesystem that is full).

If you want to ignore any error and your script to return true/success in any case, just add a true or exit 0 at the end of your script.

Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

The exit status of a for loop compound command is that of the last command executed in it.

for cmd in true false; do
  "$cmd"
done

Returns false (a non-zero exit status) because false was the last command run.

echo will return true as long as it successfully manages to write what we're telling it to.

If you want to return false/failure if any of the tidy command fails, you'd need to either record the failure, or exit upon the first failure:

#! /bin/bash -
shopt -s globstar
ok=true
for i in ./**/*.html; do
  if tidy -mq "$i"; then
    printf '%s\n' "$i"
  else
    ok=false
  fi
done
"$ok"

or:

#! /bin/bash -
shopt -s globstar
for i in ./**/*.html; do
  tidy -mq "$i" || exit # if tidy fails
  printf '%s\n' "$i"
done