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

如何为Bash脚本中的变量设置默认值?附示例脚本

Hey there, I see the issue with your default variable assignment in the Bash script—let's fix that and clean up a few other things along the way to make it more robust.

Fixing the Default Location Variable & Script Improvements

First, the core problem preventing your default location from working correctly is how you're referencing the fallback value. Let's break down the fixes step by step:

Key Issues in Your Original Code

  • When using the ${var:-default} syntax for default values, you need to reference the variable's value (with a $ prefix) instead of just the variable name. Your original line uses the literal string DEFAULT_LOCATION instead of the value you set earlier.
  • The mv KAGGLE_LOCATION . line is missing a $ to reference the actual variable.
  • Windows paths in Cygwin/MINGW work better with forward slashes (escaped backslashes work too, but forward slashes are simpler and avoid escaping headaches).
  • Variables should be quoted to handle spaces or special characters in paths (e.g., if a competition name has spaces).

Corrected Script

unamestr=$(uname)
DEFAULT_LOCATION=""

# Set default location based on operating system
if [[ "$unamestr" == 'Linux' ]] || [[ "$unamestr" == 'Darwin' ]]; then
    DEFAULT_LOCATION="/home/$USER/.kaggle/competitions/$1"
elif [[ "$unamestr" == 'CYGWIN'* ]] || [[ "$unamestr" == 'MINGW'* ]]; then
    # Use forward slashes for Cygwin/MINGW compatibility
    DEFAULT_LOCATION="C:/Users/$USER/.kaggle/competitions/$1"
else
    echo "Error: Unsupported operating system ($unamestr)"
    exit 1
fi

# Download competition data
kaggle competitions download -c "$1"

# Set KAGGLE_LOCATION: use second argument if provided, else DEFAULT_LOCATION
KAGGLE_LOCATION="${2:-$DEFAULT_LOCATION}"

# Move the competition directory to current working directory
mv "$KAGGLE_LOCATION" .

# Create data directory (ignore error if it already exists)
mkdir -p data

# Move all zip/csv files to data directory
mv "$1"/*.zip data/
mv "$1"/*.csv data/

# Clean up the empty competition directory
rm -rf "$1"

Explanation of Changes

  • Default Variable Fix: ${2:-$DEFAULT_LOCATION} now correctly pulls the value of DEFAULT_LOCATION when no second argument is passed to the script.
  • Quoted Variables: All variable references are wrapped in quotes to handle spaces or special characters in competition names or paths.
  • OS Check Wildcards: Added * to the Cygwin/MINGW checks (since uname outputs like MINGW64_NT-10.0), so the script works for all variants of these environments.
  • Error Handling: Added an else clause to catch unsupported OS and exit with an error message.
  • mkdir -p: The -p flag ensures the data directory is created (including parent directories if needed) without throwing an error if it already exists.
  • Cleanup: Added rm -rf "$1" to remove the original competition directory after moving files to the data folder.

This should get your script working as intended, with the default location properly applied when no custom path is provided.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:26:42