如何为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 stringDEFAULT_LOCATIONinstead 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 ofDEFAULT_LOCATIONwhen 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 (sinceunameoutputs likeMINGW64_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
-pflag 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
相关产品推荐
相关产品推荐

