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

如何在Bash中基于参数对循环执行操作(替代case分支)

Solution: Use Associative Arrays for Clean, Maintainable Mapping

If you're using Bash 4.0 or newer, associative arrays are perfect for this scenario—they let you directly map filenames to their corresponding HDFS directories, eliminating the need for repetitive case statements. Here's how to implement it:

Step 1: Define Your Filename-to-Directory Mapping

First, create an associative array that pairs each target filename with its HDFS path. This is easy to update later—just add new key-value pairs as needed.

# Define your pre-mapped filenames and HDFS directories
declare -A file_hdfs_map=(
    ["BATMAN_JOKER_SUPERMAN.txt"]="$dir1"
    ["BATMAN_HARLEYQUIN_SUPERMAN.txt"]="$dir2"
    ["WONDERWOMAN_CHEETAH_AQUAMAN.txt"]="$dir3"
    # Add more entries here as required
)

Step 2: Loop Through Your Target Folder

Next, iterate over every file in your specified local folder. For each file, check if it exists in your mapping, then run your desired operation if a match is found.

# Set the path to your local folder containing the files
local_folder="/path/to/your/local/files"

# Loop through each file in the local folder
for local_file in "$local_folder"/*; do
    # Extract just the filename (without the full path)
    filename=$(basename "$local_file")
    
    # Check if this filename has a corresponding HDFS directory in our map
    if [[ -n "${file_hdfs_map[$filename]}" ]]; then
        hdfs_target_dir="${file_hdfs_map[$filename]}"
        
        # Execute your desired operation here (example: copy to HDFS)
        echo "Processing $filename -> $hdfs_target_dir"
        hdfs dfs -put "$local_file" "$hdfs_target_dir/"
        
        # Add other operations (like logging, validation) as needed
    else
        # Optional: Handle files that don't have a mapping
        echo "No HDFS mapping found for $filename — skipping."
    fi
done

Alternative: Parallel Arrays for Older Bash Versions

If you're stuck with a Bash version older than 4.0 (which doesn't support associative arrays), you can use parallel arrays instead. Each index in the filenames array corresponds to the same index in the hdfs_dirs array:

# Parallel arrays: index positions match filename to directory
filenames=(
    "BATMAN_JOKER_SUPERMAN.txt"
    "BATMAN_HARLEYQUIN_SUPERMAN.txt"
    "WONDERWOMAN_CHEETAH_AQUAMAN.txt"
)

hdfs_dirs=(
    "$dir1"
    "$dir2"
    "$dir3"
)

local_folder="/path/to/your/local/files"

# Loop through each index in the arrays
for i in "${!filenames[@]}"; do
    current_filename="${filenames[$i]}"
    current_hdfs_dir="${hdfs_dirs[$i]}"
    local_file_path="$local_folder/$current_filename"
    
    # Check if the file exists locally before processing
    if [[ -f "$local_file_path" ]]; then
        echo "Processing $current_filename -> $current_hdfs_dir"
        hdfs dfs -put "$local_file_path" "$current_hdfs_dir/"
    else
        echo "File $current_filename not found in $local_folder — skipping."
    fi
done

Key Benefits Over case Statements

  • Maintainability: Adding new filename-directory pairs is as simple as adding a line to the array, no need to write new case branches.
  • Readability: The mapping is centralized and easy to scan at a glance.
  • Flexibility: Handles spaces in filenames and paths correctly when using proper quoting (as shown in the examples).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:08:09