Reputation: 165
I have the following small script:
#!/usr/bin/env bash
packages_installed=($(apt-cache -q pkgnames | tr - _ | tr . _))
echo $packages_installed
if [ ${#packages_installed[@]} -eq 0 ]; then
echo "+unable_to_find_list_of_installed_packaged"
logger -p user.error -t find_installed_packages "$(hostname -f) was unable to determine list of installed packages using apt-cache"
else
for pkg in "${packages_installed[@]}"
do
echo "+installed_package_$pkg"
done
fi
Running this however prints:
root@olympus:~# sh test.sh
test.sh: 3: test.sh: Syntax error: "(" unexpected
This seems to indicate I've made a mistake on the line where I generate the installed package list. However I'm pretty sure that's how you get a command output into an array?
Feels like I'm missing something dumb.
Upvotes: 0
Views: 142
Reputation: 123458
Feels like I'm missing something dumb.
Indeed.
root@olympus:~# sh test.sh
test.sh: 3: test.sh: Syntax error: "(" unexpected
You are using sh
to execute the script that is causing the error. Your script seems fine and the array assignment shouldn't pose any problems.
Execute it using bash
instead.
Upvotes: 4