脚本新问题:BASH 脚本中不需要“else”

脚本新问题:BASH 脚本中不需要“else”

如果可能的话,有人能帮忙吗?我正在尝试解决此脚本中的问题。我得到了“其他”不是预期的,但逻辑似乎合理。

脚本对我来说很新,但我正在尝试解决 CPU 运行过度的问题。

错误:

Do you want to set global CPU limitations y or n : y
./LIMIT A SINGLE PROCESS.sh: line 28: syntax error near unexpected token `elif'
./LIMIT A SINGLE PROCESS.sh: line 28: `elif test "$y" = "n" ; then'

脚本:

#!/bin/bash
# CPU limit of a process of one application or set global limit
#
# 
DAEMON_INTERVAL=3   # Daemon check interval in seconds

# gnome-terminal -x top

read -p "Do you want to set global CPU limitations y or n : " y

if test "$y" = "y" ; then

    read -p "Enter Global CPU limit :" CPU_LIMIT_ALL 
    echo $'\nAll Processes shall be limited to:' $CPU_LIMIT_ALL 

 while true
      do
       PID_1="top -b -n1 -c | awk 'NR>6 && \$9>CPU_LIMIT_ALL {print \$1}' CPU_LIMIT_ALL=$CPU_LIMIT_ALL"       # Set global CPU limit reads TOP list 
       NEW_PIDS=$(eval "$PID_1")                                                                               # Violating PIDs
       LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}')       
                                          # Already limited PIDs                                                   
       QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v '^$')   # PIDs in queue

   for i in $QUEUE_PIDS
      do
       cpulimit -p "$i" -l "$CPU_LIMIT_ALL" -z &   # Limit new violating processe 
done

elif test "$y" = "n" ; then

    read -p  "Enter process to be restricted or press enter :" r
    read -p  "Enter value of CPU limit or press enter :" l

   while true
    do
     echo $'\nProcess Entry Found' 
     echo $'CPU Entry Found\n'

     echo "Limit the Process of: $r to $l"  

     cpulimit --exe "$r" -b -l "$l" -z &                    # Set CPU limit for process
     sleep 60
done

else
  echo "No input found" 
  exit 1
fi

答案1

我认为您在第 28 行的 elif 之前缺少一个。循环中done有一个,但是while 循环中没有。donefordone

答案2

您没有使用 done 关闭“while”循环。您那里还有另一个“for”,它已关闭。

您没有进行正确的缩进,所以才会出现这些错误。

相关内容