-1

I have got the following code snippet.

if [ "$2" == "azure" ] && [ -n $11 ]; then
    CRED_KIND=$2
    CRED_NAME=$3
    CRED_UNAME=$4
    CRED_PWD=$5
    TWR_UNAME=$6
    TWR_PWD=$7
    CLNT=$8
    SEC=$9
    SUBS=$10
    TEN=$11
    credsplaybook $CRED_KIND $CRED_NAME $CRED_UNAME $CRED_PWD $TWR_UNAME $TWR_PWD $CLNT $SEC $SUBS $TEN
    exit 1
fi

For some reason, even when i pass only 7 arguments, it keeps executing the if condition considering only first check and skips the second one. As per the condition, it should check if the second argument is "azure" and whether a total of 11 arguments are passed.

./createResourcesPlaybook.sh cred azure test123 myuser mypass tower towerpass
[INFO] Creating Playbook for Credential with type azure

.
.
.
rest of output
Ankit Vashistha
  • 325
  • 6
  • 17
  • 1
    `$11` is like `${1}1`, it always expands to a non-empty string, thus `-n $11` always evaluates to true. Change it to `${11}` and it'll work. – oguz ismail Jan 20 '20 at 07:06
  • I tried this `if [ "$2" == "azure" ] && [ -n ${11} ]; then` but this results in the same issue. My script still executes the first condition only. – Ankit Vashistha Jan 20 '20 at 07:08
  • What happens when you quote the reference, like `[ -n "${11}" ]` – oguz ismail Jan 20 '20 at 07:10
  • @oguzismail My bad, sorry. Your suggestion works. The issue was the same what you highlighted. It was considering $11 as ${1}1. I fixed that with your suggestion and it works now. Thanks a lot. I learned something new today. Can you please post this as an answer so that it would be helpful for others. – Ankit Vashistha Jan 20 '20 at 07:23
  • Why has the question got a negative vote? How would i know if this issue is related to the problem of having more than 10 arguments? Isn't it an abuse to the voting rights and actually violating the whole purpose of allowing novice users post questions by demotivating them in such way? – Ankit Vashistha Jan 21 '20 at 07:03

1 Answers1

0

[ "$2" == "azure" ] && [ -n $11 ] are two unrelated commands chained together, the second only is executed if the first is "true".

Also, and as mentioned by others, double-digit (or more) positional argument variables needs to be enclosed in braces. So $11 needs to be ${11}.

And the -n option to the test command ([ is an alias of test) is to check if a string is empty or not but if ${11} doesn't exist then it's not equal to an empty string but rather nothing at all, making the -n check invalid.

To solve both these problems use the -a option for logical and of two conditions, and use double-quotes to make ${11} a string:

if [ "$2" == "azure" -a -n "${11}" ]
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    Or `if [[ "$2"" == "azure" && -n "${11}" ]]` etc.? – Henno Brandsma Jan 20 '20 at 07:11
  • I have updated the condition to `if [ "$2" == "azure" -a -n "$11" ]; then` unfortunately, it still results in the same problem. The code executes the first condition only. – Ankit Vashistha Jan 20 '20 at 07:12
  • 1
    My bad, the `-n "${11}"` works fine. – Ankit Vashistha Jan 20 '20 at 07:27
  • 1
    Yes, the braces are required. `man bash` *"When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces (see EXPANSION below)."* (approximately line 547 in the man page for 5.0.11) – David C. Rankin Jan 20 '20 at 08:30
  • `-a` in `[` is not POSIX. Maybe prefer the Bash-only `[[` variant in a previous comment, and/or recommend `&& [ -n "${11}" ]` in POSIX `sh`. – tripleee Jan 20 '20 at 08:47