我想在bash脚本中为变量赋予默认值。
这是脚本:
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]] || [[ "$unamestr" == 'Darwin' ]]; then
DEFAULT_LOCATION="/home/$USER/.kaggle/competitions/$1"
elif [[ "$unamestr" == 'CYGWIN' ]] || [[ "$unamestr" == 'MINGW' ]]; then
DEFAULT_LOCATION="C:\\Users\\$USER\\.kaggle\\competitions\\$1"
fi
kaggle competitions download -c $1
KAGGLE_LOCATION=${2:-DEFAULT_LOCATION}
mv KAGGLE_LOCATION .
mkdir data
mv $1/*.zip data/
mv $1/*.csv data/
cd data/
unzip *.zip
rm *.zip
当我执行echo $DEFAULT_LOCATION
时,会出现正确的值。但是当我执行$ KAGGLE_LOCATION并且不输入任何cmd参数时,我得到DEFAULT_LOCATION
作为输出而不是实际值。我的代码出了什么问题?
答案 0 :(得分:1)
您没有扩展变量,请尝试扩展变量,例如“$ KAGGLE_LOCATION”
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]] || [[ "$unamestr" == 'Darwin' ]]; then
DEFAULT_LOCATION="/home/$USER/.kaggle/competitions/$1"
elif [[ "$unamestr" == 'CYGWIN' ]] || [[ "$unamestr" == 'MINGW' ]];then
DEFAULT_LOCATION="C:\\Users\\$USER\\.kaggle\\competitions\\$1"
fi
kaggle competitions download -c $1
KAGGLE_LOCATION=${2:-DEFAULT_LOCATION}
mv "$KAGGLE_LOCATION" .
mkdir data
mv $1/*.zip data/
mv $1/*.csv data/
cd data/
unzip *.zip
rm *.zip