So, I want to create a script where I check sth from a website. I have a text file with different websites in it and I want to check all websites simultaneously. To do this I'm creating a command step by step in a variable calling a function which will check every website. This is my code:
checkWebsite(){
WEBSITE="$2"
echo "Parameter is $WEBSITE"
}
COUNTER=0;
input=websites.txt
myCommand=()
while IFS= read -r line
do
if [[ "$line" != "#"* ]]; #ignoring comments if there are any
then
myCommand[COUNTER]="checkWebsite $line"
COUNTER=$((COUNTER + 1))
myCommand[COUNTER]=" & "
COUNTER=$((COUNTER + 1))
fi
done < "$input"
unset 'myCommand[${#myCommand[@]}-1]' #deleting the last " & "
echo "MY COMMAND: "
echo ${myCommand[@]}
echo " "
echo ${myCommand[0]}
checkWebsite ${myCommand[0]}
"${myCommand[0]}"
These are the results: results
As you can see, if I call the function from the script everything is ok but if I call the function from the variable it doesn't work. I understand why this is happening(it's like I run that command from the terminal) but I don't know how can I make it run the function from the script when I execute the command from the variable. Does anyone know how can I make that happen?
Thanks in advance
"${myCommand[0]}"entirely defeats the whole purpose of using an array -- dereffing just the first element means it's just another string.declare -p myCommandgives you a much more accurate idea of your array's contents thanecho ${myCommand[@]}does. (For that matter,printf '%q\n' "${myCommand[@]}"would be an improvement as well).