0

I'm currently creating a list of commands so for example by saying "directory install plugin-name" I can install all needed plugins specified in an external list. This list is just a txt file with all plugin names. But I'm struggling getting all names in an associative array.

I've tried this one:

while IFS=";" read line;
do " communtyList[ $line ]=1 " ;
done < community-list.txt;

The desired output should be

  • communityList[test1]=1
  • communityList[test2]=1....

It need to be an associative array because I want to access it by words and not by index. This word will be implemented as parameters/arguments.

For example "install plugin" instead of "1 plugin"

So I can ask for example this way:

if [ ! -z "${!communtyList[$2]}" ];

Update, here the whole code:

#!/usr/bin/env bash
community(){
declare -A communtyList
while IFS= read line;
do communtyList[$line]=1 ;
done < community-list.txt;
#     communtyList[test1]=1
#     communtyList[test2]=1
#     communtyList[test3]=1
#     communtyList[test4]=1
if { [ $1 = 'install' ] || [ $1 = 'activate' ] || [ $1 = 'uninstall' ] || [ $1 = 'deactivate' ] ; } && [ ! -z $2 ] ;  then
     if [ $2 = 'all' ];
        then echo "$1 all community plugins....";
        while IFS= read -r line; do echo "$1  $line "; done < community-list.txt;
     elif [ ! -z "${!communtyList[$2]}" ];
        then echo "$1 community plugin '$2'....";
     else
        echo -e "\033[0;31m Something went wrong";
        echo " Plugin '$2' does not exist.";
        echo " Here a list of all available community plugins: ";
        echo ${!communtyList[@]}
        echo -e " \e[m"
    fi
else
    echo -e "\033[0;31m Something went wrong";
    if [ -z $2 ];
        then echo -e "[Plugin name] required. [community][action][plugin name] \e[m"
    else
        echo " Action '$1' does not exist.";
        echo -e " Do you mean some of this? \n install \n activate \n uninstall \e[m"
    fi
fi
echo ${!communtyList[@]}
}
"$@"
tripleee
  • 175,061
  • 34
  • 275
  • 318
Rakowu
  • 154
  • 10

2 Answers2

1

To use asociative array you have to declare it first

declare -A communityList

Then you can add values

communityList[test1]=1
communityList[test2]=2
...

Or with the declaration

declare -A communityList=(
    communityList[test1]=1
    communityList[test2]=2
    ...
)
Ivan
  • 6,188
  • 1
  • 16
  • 23
  • Hi thanks for your anser. The declaration was already in my script but i did not added it here. maybe i should post the whole code. – Rakowu Feb 07 '20 at 10:31
1

The quotes around " communtyList[ $line ]=1 " mean you try to evaluate a command whose first character is a space. You want to take out those quotes, and probably put quotes around "$line" instead.

It's also unclear why you have IFS=";" -- you are not splitting the line into fields anyway, so this is not doing anything useful. Are there semicolons in your input file? Where and why; what do they mean?

You should probably prefer read -r unless you specifically require read to do odd things with backslashes in the input.

Finally, as suggested by Ivan, you have to declare the array's type as associative before you try to use it.

With those things out of the way, try

declare -A communityList

while read -r line; do
    communtyList["$line"]=1
done < community-list.txt
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Hi, Sorry that didn't work for me :) It says ") syntax error: invalid arithmetic operator (error token is " But thank you for you answer – Rakowu Feb 07 '20 at 10:32
  • Looks like you probably used a Windows editor. That's unrelated to this answer, and a common FAQ; https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings – tripleee Feb 07 '20 at 10:40
  • Currently im running git-bash and the code is written in a sh file. Maybe i'm confusing here something i learned today for the first time bash/sh commands :S – Rakowu Feb 07 '20 at 10:42
  • Bash doesn't care what you call your file; but the `.sh` extension is misleading for human readers. – tripleee Feb 07 '20 at 10:43
  • so what should it be instaed? – Rakowu Feb 07 '20 at 10:45
  • If it was called `donkeys.sh` a better name would be `donkeys`. – tripleee Feb 07 '20 at 10:55