2

Problem statement:

It appears that I have an issue with with a while loop when I utilize set -e:

 1  #!/bin/bash
 2  # This is a script to perform initial configurations of CentOS 7.2
 3  # Written by Robert J.
 4  
 5  # Exit if unexpected condition
 6  set -e;
 7  
 8  # Simple die function
 9  dieaquickdeath(){
10      code=$1;
11      message=$2;
12      printf "$message\n";
13      exit "$code";
14  }
15  
16  #
17  ## There is an initial prompt here that was removed for simplification.
18  #
19  InputPassword1=1
20  InputPassword2=2
21  
22  # Sanity check for password
23  unset n;
24  while [[ "$InputPassword1" != "$InputPassword2" ]];
25  do
26      # Counter
27      ((n++));
28  
29      # Prompt for new password
30      printf "\nPasswords do not match\n";
31      read -sp "Enter password: " InputPassword1;
32      echo;
33      read -sp "Re-enter password: " InputPassword2;
34  
35      # End loop after passwords match.
36      [[ "$InputPassword1" == "$InputPassword2" ]] &&
37          break;
38  
39      # End loop after 3 attempts
40      [[ "$n" == 3 ]] &&
41          dieaquickdeath 1 "Third failed password attempt";
42  
43  done;

Attempted solutions:

I initially thought that this might have just been because of the exit code for test so I hardwired true statements temporary for troubleshooting purposes (This did not work still):

36      [[ "$InputPassword1" == "$InputPassword2" ]] &&
37          break || 
38          true;
39  
40      # End loop after 3 attempts
41      [[ "$n" == 3 ]] &&
42          dieaquickdeath 1 "Third failed password attempt" || 
43          true;

This does not appear to have any issues when running without the set -e;

[root@Wiki ~]# vi testing
[root@Wiki ~]# ./testing
[root@Wiki ~]# grep -v "set -e" testing > test2
[root@Wiki ~]# ./test2
-bash: ./test2: Permission denied
[root@Wiki ~]# chmod +x test2
[root@Wiki ~]# ./test2

Passwords do not match
Enter password: 
Re-enter password: 
Passwords do not match
Enter password: 
Re-enter password: 
Passwords do not match
Enter password: 
Re-enter password: Third failed password attempt
[root@Wiki ~]#

Questions:

  • Does anyone see where my unexpected exit is?
  • Is this the cause of some bash caveat that I am unaware of?

I feel like I have been staring at this for far too long.

Thanks in advance for being awesome all.

2
  • Run it with -x and it should stop where it it? Commented Jun 13, 2016 at 19:10
  • Thanks man! I didn't know about the bash -x option. This is super helpful. Commented Jun 13, 2016 at 19:17

1 Answer 1

5

To see where it stops, run bash -x testing:

$ bash -x testing
+ set -e
+ InputPassword1=1
+ InputPassword1=2
+ unset n
+ [[ 2 != '' ]]
+ (( n++ ))

It stops because n was (effectively) zero. Observe:

$ unset n; ((n++)); echo code=$?
code=1

An exit code of 1 indicates failure. Hence, set -e terminated execution here.

Here are two possible solutions that will return a zero (success) exit code:

$ unset n; ((++n)); echo code=$?
code=0
$ unset n; ((n++)) || true; echo code=$?
code=0

In general, as discussed here, set -e does strange things. Its use is controversial.

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome man! I bash -x is going to save me a lot of time in the future. I'm kinda a bash noob. Thanks for your time man.
@Robert J, Just like set -e, you can also place it in the script, e.g., set -x for testing purposes and then comment it out or remove it.
and set +x to "turn it off", and export PS3='$LINENO >' to have it display the line number of the code being executed. Good luck to all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.