S.Smith S.Smith - 4 months ago 12
Bash Question

Shell Script: how to check multiple groups if they exists and display in echo

this is my current code:

if [ $(getent group administrators) ]; then
echo "Group exists"
else
echo "Group does not exist"
fi


My questions are:


  • how do i improve it by making it such that i can include more groups to display?

  • and display the group e.g administrators, appears in the echo like: The group administrators, students exists?



thanks!

Answer

Use && to test multiple conditions.

if [ $(getent group administrators) ] && [ $(getent group students) ]
then
    echo The groups administrators and students exit
else
    echo The groups administrators and students do not both exist
fi
Comments