In your code:
if [[ "$(echo "$2" | sed 's/.two//g')" == "load" ]] &&
[[ "$1" == "Decrypt" ]] ||
[[ "$(echo "$2" | sed 's/.two//g')" == "load" ]] &&
[[ "$1" == "Encrypt" ]]
The sed call could be simplified to just: ${2%?two} if the replacement is at the end of the variable $2. Please understand that the "any character" that the dot (.) represents in sed, is equivalent to the question mark (?) in patterns (thanks @terdon). If the replacement needs to be done for all occurrences of .two, then we should use: "${2//?two}".
Then we get this shorter version:
if [[ "${2//?two}" == "load" ]] && [[ "$1" == "Decrypt" ]] ||
[[ "${2//?two}" == "load" ]] && [[ "$1" == "Encrypt" ]]
which is doing if A && B || C && D.
When A is true (load = load) B is executed.
If B is true (Decrypt = Decrypt) the following || phrase (C) is skipped and
then D is executed (Decrypt = Encrypt).
Which results (the last command executed) in a false value.
Then the else is executed .....
I suspect that what you mean is if ( A && B ) || ( C && D ) which, as A is the same as C, is exactly the same as if ( A && B ) || ( A && D ),
which can be simplified (using the distributive property) to
if A && ( B || D ):
if [[ "${2//?two}" == "load" ]] &&
( [[ "$1" == "Decrypt" ]] || [[ "$1" == "Encrypt" ]] );
then
key=aNXlye1tGbd0uP
else
if [ -z "$key" ]
then key="$2"
fi
fi
The -z test for "$key" could be simplified to a simple expansion: key="${key:-$2}"
And, maybe, it would be more readable (IMO) like this:
if A; then
if B || D; then
Which translates to this:
if [[ "${2//?two}" == "load" ]]
then if [[ "$1" == "Decrypt" ]] || [[ "$1" == "Encrypt" ]]
then key=aNXlye1tGbd0u
else key="${key:-$2}"
fi
else key="${key:-$2}"
fi
Or could be, using @terdon’s idea, written as this:
if [[ "${2//?two}" == "load" ]] &&
[[ "$1" =~ (De|En)crypt ]]
then key=aNXlye1tGbd0u
else key="${key:-$2}"
fi
Please Note that this is also equivalent:
if [[ ( "${2//?two}" == "load" ) &&
( "$1" =~ (De|En)crypt )
]]
then key=aNXlye1tGbd0u
else key="${key:-$2}"
fi
The parenthesis are not strictly needed, but added to enforce the idea that inside [[ test you could give structure to your tests adding whitespace (tab, space, newline) and parenthesis. That does not work the same in [ tests.