【发布时间】:2021-06-10 14:32:21
【问题描述】:
我有这个shell 脚本,但我不知道为什么它在传递参数后不起作用...
在这里
#!/bin/bash
# Check the argument type and change the directory depending on it.
# Open with code
create() {
directory="E:/husseinabbas/programming/Code"
use_directory=""
project_name="$1"
project_type="$2"
project_mode="$3" || "new"
repository_name="$4" || "$project_name"
repository_type="$5" || false
if [[ "$project_type" == "html" ]]
then
use_directory="${directory}/html_css/$project_name"
elif [[ "$project_type" == "node" ]]
then
use_directory="{$directory}/node-projects/$project_name"
elif [[ "$project_type" == "php" ]]
then
use_directory="F:/XAMPP/htdocs/$project_name"
elif [[ "$project_type" == "python" ]]
then
use_directory="{$directory}/python-projects/$project_name"
elif [[ "$project_type" == "flutter" ]]
then
use_directory="{$directory}/android/$project_name"
elif [[ "$project_type" == "test" ]]
then
use_directory="C:/Users/H.Abbas/Desktop/$project_name"
else
return
fi
# Activate the venv
cd "E:/husseinabbas/programming/Code/python-projects/day_automator"
source venv/Scripts/activate
python day_automator.py "$project_name" "$project_type" "$project_mode" "$repository_name" "$repository_type"
cd "$use_directory" || return
echo "# $project_name" >> README.md
git init
git add README.md
git commit -m "init"
git branch -M main
git remote add origin https://github.com/husseinYY/$repository_name.git
git push -u origin main
code .
}
"$@"
我正在使用这个命令来运行它;
sh .\create.sh my_day python all my_day false
它给了我这个:-
.\create.sh: line 52: my_day: command not found
我已阅读此问题的其他答案,但没有帮助
这一段只是因为Stackoverflow机器人想要更多细节嘿嘿
【问题讨论】:
-
您通常使用 ./ 而不是 .\ 在同一目录中运行脚本。另外,最好将参数括在引号中。
-
project_mode="$3" || "new"不是您在 shell 中提供默认值的方式;请改用project_mode="${3:-new}"(请参阅this question)。