Bash: proper handling of errors

Viewed 133

I have bash script similar to:

{
  # Try block
  command 1
  command 2
  command 3
} || {
  # Catch errors
  while true; do
  {
    # Try block inside
    # Change parameters for command 1, command 2
    command 1
    command 2
    command 3
    break
  } || {
      # Change parameters and try again
      continue
  }
done
}

More or less this code works fine, but...

For some reason try sections works not as expected for me. I thought that it fails if some of my commands return not 0 code, but it's not true.

For some specific cases my command 2 returns 1 code in the first try block, but it doesn't fail and goes into catch section, command 3 executes from this try block and that's it.

Here is my question:

How to handle errors in bash? Just to play with return codes?

UPDATE:

Original code looks very similar to: The main idea is that all 3 commands should be executed one-by-one and all of them are somehow related to folder_name.

folder_name=my_folder
suffix=0
{
  cd /home/test/
  mkdir $folder_name
  ls -la $folder_name
} || {
    while true; do
      suffix=$(expr $suffix + 1)
      {
        folder_name=$folder_name_$suffix
        cd /home/test/
        mkdir $folder_name
        ls -la $folder_name
        break
      } || {
          continue
      }
    done
}
2 Answers
Related