Reputation: 978
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;
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 ~]#
I feel like I have been staring at this for far too long.
Thanks in advance for being awesome all.
Upvotes: 2
Views: 2373
Reputation: 113814
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.
Upvotes: 5