【问题标题】:.\create.sh: line 52: X: command not found.\create.sh:第 52 行:X:找不到命令
【发布时间】: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)。

标签: bash shell


【解决方案1】:

在你的脚本中你没有调用你的函数create,所以什么也没做。

最后你还在扩展"$@":这会打印你传递的shell参数 试图执行它们。 错误消息来自“第 52 行”,这正是 $@ 所在的位置。

在你的命令中:

sh ./create.sh my_day python all my_day false
  • $0 是脚本的路径 ./create.sh
  • $1my_day
  • $2python
  • ...

$@$1 扩展到最后一个参数,这就是为什么您在my_day 上收到错误,实际上是$1

"$@" 前面加上 create 应该没问题。

也检查What does $@ mean in a shell script?

【讨论】:

  • 也许“用create "$@"替换"$@"应该没问题”?
  • 是的,也是。如果你只写create,它将从主脚本中获取参数,所以它会起作用。但我更喜欢你的解决方案
  • @piertoni 仅使用create不会将参数传递给主脚本,它将运行不带参数的create 函数。
猜你喜欢
  • 2022-11-30
  • 2014-04-12
  • 1970-01-01
  • 2014-12-20
  • 2015-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多