-1

I need to form a single variable from a for loop.

My script:

#! /bin/sh
if [ "$#" -le 1 ]; then
    echo "Illegal number of parameters"
    exit 1
else
    # Form domains variable
    DOMAINS=""
    for i in "${@:2}"; do
        DOMAINS+="$i,"
    done

    echo "${DOMAINS::-1}"
fi

When I execute it:

    sh script.sh command domain1 domain2

I get the following error:

    certbot.sh: line 10: DOMAINS+=mmand,: not found
    certbot.sh: line 10: DOMAINS+=domain1.com,: not found
    certbot.sh: line 10: DOMAINS+=domain2.com,: not found

It seems as I used bash syntax since the following execution works:

    bash script.sh command domain1.com domain2.com

I get:

domain1.com,domain2.com

I need it to work as sh not bash. I can't seem to find a solution.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
Sulas
  • 39
  • 8
  • `"${@:2}"` and `${DOMAINS::-1}"` does not look like `sh` you might need to `shift` , but you can paste your script at https://shellcheck.net for validation. – Jetchisel Jun 11 '21 at 10:21
  • @Sulas : I took the liberty to remove the _bash_ tag, since the question is not related to bash. – user1934428 Jun 11 '21 at 10:26
  • `sh` ([Bourne-shell](https://en.wikipedia.org/wiki/Bourne_shell)) is usally not `bash` ([Bourne-again shell](https://en.wikipedia.org/wiki/Bash_(Unix_shell))). – Cyrus Jun 11 '21 at 10:34
  • 1
    Get out of the habit of using ALLCAPS variable names, leave those as reserved by the shell. One day you'll write `PATH=something` and then [wonder why](https://stackoverflow.com/q/27555060/7552) your [script is broken](https://stackoverflow.com/q/28310594/7552). – glenn jackman Jun 11 '21 at 14:29

3 Answers3

1

+= is not valid in a POSIX shell.

Since it is not a valid variable assignment,

DOMAINS+="$i,"

is interpreted as the name of a command, which is obtained by parameter expansion of i. For instance, if i equals 1, the line corresponds to

DOMAINS+=1,

If you had an executable file named DOMAINS+=1, in your PATH, this file would be run.

You have to catenatate variables like this:

FOO=$FOO$BAR$BAZ

You can't avoid repeating the name FOO.

An alternative would be to switch to zsh or bash, where your usage of += would indeed have the desired effect.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • I suggest to use `FOO="$FOO$BAR$BAZ"` – Bodo Jun 11 '21 at 12:34
  • @Bodo: For very old bash version, I believe that quotes were necessary here, if one of the variables contains white space, but at least since bash 4, likely since bash 3 already, they aren't anymore, and I wanted to focus just on the issue of catenation. – user1934428 Jun 11 '21 at 13:00
  • For readability: `foo=${foo}${bar}${baz}` -- quotes not strictly required for variable assignments – glenn jackman Jun 11 '21 at 14:29
1

Just:

IFS=,
echo "$*"

Or you seem to want from a second argument. Then like:

( shift; IFS=,; echo "$*" )
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

Did you try changing DOMAINS+="$i," into DOMAINS="${DOMAINS}${i}," relying on variable substitution?

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74