Linux 变量与数字的比较

Linux 变量与数字的比较,linux,bash,shell,Linux,Bash,Shell,下面的代码抛出一个错误,我不知道为什么会发生 #! /bin/bash # This script checks if your Apache log is older than two weeks. # If so, the files will be deleted # Defining savepath savePath="/var/log/test.log" # Startup printf "\n*** Starting logrotate at $

下面的代码抛出一个错误,我不知道为什么会发生

#! /bin/bash

# This script checks if your Apache log is older than two weeks.
# If so, the files will be deleted

# Defining savepath
savePath="/var/log/test.log"

# Startup
printf "\n*** Starting logrotate at $(date +'%m-%d-%y %H:%M:%S') ***" >> $savePath

# Check if Apache logs older than two weeks are existing
apacheCount=`sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +15 | wc -l`

# If so, delete 'em!
if [ "$apacheCount" != "0" ]; then

    sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +8 -exec rm -f {} \;
    newValue=sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +15 | wc -l

    if [ "$newValue" == "0" ]; then
            printf "\n$(date +'%m-%d-%y %H:%M:%S'): $apacheCount Apache Log(s) has / have been deleted." >> $savePath
    else
            printf "\n$(date +'%m-%d-%y %H:%M:%S'): There was an error. $(($apacheCount-$newValue)) items were not deleted." >> $savePath
    fi
else
    printf "\n$(date +'%m-%d-%y %H:%M:%S'): No Apache Log older than two weeks found." >> $savePath
fi
执行程序时,会抛出以下错误:

[:意外运算符

算术表达式:应为“1-”


我对bash比较陌生,如果你能解释一下我的错误所在,我将不胜感激

newValue=`sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +15 | wc -l`
改进代码:

#! /bin/bash

# This script checks if your Apache log is older than two weeks.
# If so, the files will be deleted

# Defining savepath
savePath="/var/log/test.log"

# Startup
printf "\n*** Starting logrotate at $(date +'%m-%d-%y %H:%M:%S') ***" >> $savePath

# Check if Apache logs older than two weeks are existing
apacheCount=`sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +15 | wc -l`

# If so, delete 'em!
if [ "$apacheCount" != "0" ]; then

   sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +8 -exec rm -f {} \;
   #the line below had an error, `` added
   newValue=`sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +15 | wc -l`

   if [ "$newValue" == "0" ]; then
        printf "\n$(date +'%m-%d-%y %H:%M:%S'): $apacheCount Apache Log(s) has / have been deleted." >> $savePath
   else
        printf "\n$(date +'%m-%d-%y %H:%M:%S'): There was an error. $(($apacheCount-$newValue)) items were not deleted." >> $savePath
   fi
else
   printf "\n$(date +'%m-%d-%y %H:%M:%S'): No Apache Log older than two weeks found." >> $savePath
fi

您好,在您的脚本中,您错误地捕获了
newValue
中的值。请在代码中修改以下两行,以解决此问题:-

#better use below syntax
apacheCount=$(sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +15 | wc -l)


newValue=$(sudo /usr/bin/find /var/log/apache2/ -iname "access.log.*.gz" -mtime +15 | wc -l)

newValue=sudo/usr/bin/find/var/log/apache2/-iname“access.log.*.gz”-mtime+15 | wc-l
将是一个问题。相反
newValue=$(sudo/usr/bin/find/var/log/apache2/-iname“access.log.*.gz”-mtime+15 | wc l)
wc-l
的输出捕获到变量中。此外,将此内容转储到变量中会告诉您它的所有错误以及改进建议。@jnevil:
shellcheck
(或在线服务)我会告诉你它的一些错误。如果它能找出
newValue
没有设置的原因,我会印象深刻,因为错误的命令是完全有效的-只是不是预期的。@TobySpeight我同意,但它会告诉你
newValue
没有设置,这是一个巨大的线索。我看不出你复制的代码是如何用于你的问题的可能会产生您复制的错误,因为它不进行数字比较。如果您想进行模拟比较,您需要使用
-ne
-eq
,而不是
=
!=
,这是字符串比较。对于bash,最好使用
(…)
或至少
[…]
。您的意思是保留
==
投诉的
[
吗?代码虽然“改进”,但肯定不是固定的。与其使用反勾号,不如使用
newvalue=$(命令)
@TobySpeight Gracias。固定评论。@TobySpeight这是一个新的bash东西。你可以使用
=
或者现在就使用
=
。感谢你对我的代码进行了改进!当我使用
bash./logrotate.sh
而不是
sh
时,它不会抛出“意外运算符”-由于某种原因出现错误,程序保持正常运行。