0

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

sigh I obviously feel like an idiot right about now. Thanks mate. Apologies for what must be the stupidest post here...
@Khushil Never underestimate the stupidity of others :) If you makes you feel better, bash still accepts some bash-isms even when invoked as sh, so it's not always obvious what will work and what won't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.