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.
set -x; export FOO="Decrypt"; [[ "$FOO" == "Decrypt" ]] && echo "yep"
works as expected (i. e.yep
is echoed). If it weren't, you'd not even be getting to the comparison toEncrypt
which occurs later in the conditional chain. For the substring comparison, rather than spawningsed
, I might look into[[ "load" == "${2/.two/}" ]]
for example. – DopeGhoti Dec 28 '15 at 22:38[[ "$1" == "Decrypt" ]]
, the key becomes what I want it to become. – DisplayName Dec 28 '15 at 22:40if
. What are you expecting it to do? You have various AND conditions combined with OR. I think you expect it to group some of them together but you need to tell us how. Yourif
statement boils down toif A && B || C && D
. I think you are afterif (A && B) || (C && D)
, is that correct? – terdon Dec 28 '15 at 22:41"$(echo "$2" | sed 's/.two//g')"
with$2
, leaving theif
expression as[[ "$2" == "load" ]] && [[ "$1" == "Decrypt" ]] || [[ "$2" == "load" ]] && [[ "$1" == "Encrypt" ]]
, to verify that thesed
complication wasn’t causing the problem. Having done so, you should have posted the simplified version in your question, so we wouldn’t have to scroll your script (horizontally) to read it. Also, maybe, switch the order, so the$1
tests appear before the$2
tests (for readability). (2) Please indent your code in a readable style. – G-Man Says 'Reinstate Monica' Dec 29 '15 at 09:10