1

In my code I get this error

test.sh: line 10: [: too many arguments
[ - ] mountnfs.sh is OK
test.sh: line 10: [: too many arguments
[ + ] network-manager is OK
test.sh: line 10: [: too many arguments  
[ + ] networking is OK
test.sh: line 10: [: too many arguments
[ + ] ondemand is OK
test.sh: line 10: [: too many arguments
[ - ] plymouth is OK
test.sh: line 10: [: too many arguments
[ - ] plymouth-log is OK

these are all Ubuntu 16.04 default services, the purpose is to check your local machines services against the defaults. Here is the code:

!/bin/bash 
clear

Ubuntu1604=(acpid alsa-utils anacron apparmor apport avahi-daemon  bluetooth bootmisc.sh brltty checkfs.sh checkroot-bootclean.sh checkroot.sh  console-setup cron cryptdisks cryptdisks-early cups cups-browsed dbus dns-clean grub-common hostname.sh hwclock.sh irqbalance kerneloopskeyboard-setup killprocs kmod lightdm lvm2 lvm2-lvmetad lvm2-lvmpolld mountall-bootclean.sh mountall.sh mountdevsubfs.sh mountkernf.sh mountnfs-bootclean.sh mountnfs.sh network-manager networking ondemand plymouth plymouth-log pppd-dns procps rc.local resolvconf rsync rsyslog saned sendsigs speech-dispatcher thermald udev ufw umountfs umountnfs.sh umountroot unattended-upgrades urandom uuidd whoopsie x11-common)

service --status-all > services.txt

while read SERVICE 
do
    if [ $SERVICE in ${Ubuntu1604[@]} != ${Ubuntu1604[@]} ]; then
    #if [ "${Ubuntu1604[@]}" != "$SERVICE" ]; then 
            echo $SERVICE could be harmful
    else
            echo $SERVICE is OK
    fi

 done < services.txt

I have been going at this for sometime, I would really appreciate some help. Thank you.

  • `$SERVICE in ${Ubuntu1604[@]}` is not valid shell syntax. Also, you should almost always put double-quotes around variable references. Give [shellcheck.net](http://www.shellcheck.net] a try. – Gordon Davisson Oct 11 '17 at 01:43

1 Answers1

1

Try:

#!/bin/bash
Ubuntu1604='acpid|alsa-utils|anacron|apparmor|apport|avahi-daemon|bluetooth|bootmisc.sh|brltty|checkfs.sh|checkroot-bootclean.sh|checkroot.sh|console-setup|cron|cryptdisks|cryptdisks-early|cups|cups-browsed|dbus|dns-clean|grub-common|hostname.sh|hwclock.sh|irqbalance|kerneloopskeyboard-setup|killprocs|kmod|lightdm|lvm2|lvm2-lvmetad|lvm2-lvmpolld|mountall-bootclean.sh|mountall.sh|mountdevsubfs.sh|mountkernf.sh|mountnfs-bootclean.sh|mountnfs.sh|network-manager|networking|ondemand|plymouth|plymouth-log|pppd-dns|procps|rc.local|resolvconf|rsync|rsyslog|saned|sendsigs|speech-dispatcher|thermald|udev|ufw|umountfs|umountnfs.sh|umountroot|unattended-upgrades|urandom|uuidd|whoopsie|x11-common'

while read -r service
do
    if [[ $service =~ $Ubuntu1604 ]]
    then
       echo "$service is good."
   else
       echo "$service is bad."
   fi
done <services.txt

In this version, $Ubuntu1604 is a regex and this allows us to match via bash's [[ command.

Example

As sample input, consider:

$ cat services.txt 
acpid
CatVideos
anacron

This produces the output:

$ bash script.sh
acpid is good.
CatVideos is bad.
anacron is good.
John1024
  • 97,609
  • 11
  • 105
  • 133