You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shell脚本if语句语法错误排查及日志旧信息删除方法咨询

Fixing Your Shell Script Issues

Hey there! Let’s break down your two shell script problems and solve them step by step.


1. Fixing the if/elif/else Syntax Error ("Unexpected token 'else'")

That error almost always traces back to a formatting misstep in your conditional blocks. Let’s go through the most common culprits and how to fix them:

Common Causes & Quick Fixes

  • Missing spaces around brackets: When using the [ test command, you must add spaces after [ and before ].
    ❌ Wrong: if[$count -gt 5]
    ✅ Correct: if [ $count -gt 5 ]
  • Missing semicolon for inline then: If you write if and then on the same line, a semicolon is required before then.
    ❌ Wrong: if [ $count -gt 5 ] then
    ✅ Correct: if [ $count -gt 5 ]; then
  • Unmatched parentheses: If you’re using extended tests with [[, make sure you close it with ]]—don’t mix [ and [[ in the same condition.
  • Typos: Double-check that you wrote elif (not elif or else if) and that else has no extra characters before it.

Example of a Corrected Block

Here’s a properly formatted if/elif/else structure that won’t throw syntax errors:

#!/bin/bash
count=$(wc -l < input.txt)

if [ $count -gt 100 ]; then
    echo "Cleaning up excess entries..."
    # Your cleanup logic here
elif [ $count -lt 10 ]; then
    echo "Not enough entries to clean."
else
    echo "Entry count is within normal range."
fi

Double-check line 30 of your count.sh—it’s almost certainly one of these formatting issues causing the error.


2. Deleting Old Status Entries with date

To remove the oldest status information from output.txt, you’ll need to identify timestamped lines, compare them to a cutoff date, and keep only newer entries. Here are two practical approaches:

Approach 1: Keep the Latest N Entries

If your status lines are ordered by timestamp (newest last), use tail to retain only the most recent entries:

# Keep the last 500 entries (adjust the number to your needs)
tail -n 500 output.txt > temp.txt && mv temp.txt output.txt

If entries aren’t ordered, sort them first by timestamp, then keep the latest N:

# Assuming timestamp is in the first two fields (e.g., 2024-05-20 14:30:00)
sort -k1,2r output.txt | head -n 500 | sort -k1,2 > temp.txt && mv temp.txt output.txt
  • sort -k1,2r: Sorts lines in reverse order by date + time.
  • head -n 500: Grabs the top 500 newest entries.
  • sort -k1,2: Re-sorts them back to chronological order.

Approach 2: Delete Entries Older Than X Days

If you want to remove entries older than a specific timeframe (e.g., 7 days ago), use date to generate a cutoff timestamp and filter lines with awk:

#!/bin/bash
# Calculate cutoff timestamp (7 days ago, in seconds since epoch)
cutoff=$(date -d "7 days ago" +%s)

# Filter entries newer than the cutoff (adjust $1 $2 to match your timestamp fields)
awk -v cutoff="$cutoff" '{
    timestamp_str = $1 " " $2
    cmd = "date -d \"" timestamp_str "\" +%s"
    cmd | getline entry_timestamp
    close(cmd)
    if (entry_timestamp > cutoff) print $0
}' output.txt > temp.txt && mv temp.txt output.txt

If your timestamps use ISO 8601 format (e.g., 2024-05-20), skip timestamp conversion and compare strings directly:

cutoff_date=$(date -d "7 days ago" +%Y-%m-%d)
awk -v cutoff="$cutoff_date" '$1 >= cutoff' output.txt > temp.txt && mv temp.txt output.txt

内容的提问来源于stack exchange,提问作者Desert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:44:36