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

Shell脚本参数顺序错位时的验证方法咨询

Sequential Argument Validation for Bash Scripts

Great question! Checking just the number of arguments is a solid first step, but to catch cases where users mix up the order (like passing a retention number where a directory path should be), you need to add targeted validation for each parameter's type/format. Let's break this down for your specific parameters:

Expected Parameter Requirements

First, let's formalize what each argument should look like:

  • FILE_DIR ($1): Must be an existing directory
  • RETENTION_DAYS ($2): Must be a positive integer (since retention days shouldn't be zero or negative)
  • FILE_NAME_PREFIX ($3): Should be a non-empty string (adjust if you need to restrict special characters)

Implementing the Validation

Here's how to add these checks to your script, with clear error messages for users:

#!/bin/bash

fn_usage() {
    echo "Usage: $0 <file_directory> <retention_days> <file_name_prefix>" >&2
    echo "Example: $0 /var/log/backups 7 daily_backup" >&2
    exit 1
}

# First, check argument count
if [ $# -ne 3 ]; then
    echo "Error: Invalid number of arguments. Expected 3, got $#." >&2
    fn_usage
fi

# Validate FILE_DIR ($1) is an existing directory
FILE_DIR="$1"
if [ ! -d "$FILE_DIR" ]; then
    echo "Error: '$FILE_DIR' is not a valid or existing directory." >&2
    fn_usage
fi

# Validate RETENTION_DAYS ($2) is a positive integer
RETENTION_DAYS="$2"
if ! [[ "$RETENTION_DAYS" =~ ^[1-9][0-9]*$ ]]; then
    echo "Error: '$RETENTION_DAYS' is not a valid positive integer for retention days." >&2
    fn_usage
fi

# Validate FILE_NAME_PREFIX ($3) is non-empty
FILE_NAME_PREFIX="$3"
if [ -z "$FILE_NAME_PREFIX" ]; then
    echo "Error: File name prefix cannot be empty." >&2
    fn_usage
fi

# If all checks pass, proceed with your script logic
echo "All arguments validated successfully!"
echo "Directory: $FILE_DIR"
echo "Retention Days: $RETENTION_DAYS"
echo "File Prefix: $FILE_NAME_PREFIX"

Key Notes & Adjustments

  • Customizing the integer check: If you want to allow 0 as a valid retention day (e.g., delete all files immediately), modify the regex to ^[0-9]+$.
  • Restricting prefix characters: If your file prefix shouldn't contain slashes or other special characters, add an extra check like:
    if [[ "$FILE_NAME_PREFIX" =~ / ]]; then
        echo "Error: File name prefix cannot contain slashes." >&2
        fn_usage
    fi
    
  • Error messaging: Using >&2 ensures error messages are sent to standard error instead of standard output, which is good practice for shell scripts.

This approach will catch most common order mix-ups—for example, if a user passes a number first, the directory check will fail immediately and tell them what's wrong.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:02:57