【发布时间】:2018-03-22 11:58:53
【问题描述】:
在我的代码中出现此错误
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
这些都是 Ubuntu 16.04 默认服务,目的是检查您的本地机器服务是否符合默认设置。这是代码:
!/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
我已经有一段时间了,我真的很感激一些帮助。谢谢你。
【问题讨论】:
-
$SERVICE in ${Ubuntu1604[@]}不是有效的 shell 语法。此外,您几乎应该总是在变量引用周围加上双引号。试试[shellcheck.net](shellcheck.net]。
标签: arrays bash shell scripting iteration