简体   繁体   English

命令执行时没有这样的文件或目录

[英]No such file or directory on command execution

I want to clone a git repository with bash.我想用 bash 克隆一个 git 存储库。 Apart from that I need to execute several different commands and they all should be checked with the same part of code.除此之外,我需要执行几个不同的命令,并且它们都应该使用相同的代码部分进行检查。 So I wrote this method:所以我写了这个方法:

exec_func() {
    print_msg "$1"
    if [ "$(bash "$2" >> log.txt 2>&1;echo $?)" -eq "0" ]; then
            # Task success
    else
            # Task fails
    fi
    printf "\n\n" >> log.txt
}

Calling this function takes two arguments.调用这个 function 需要两个 arguments。 The first argument is just a string explaining which command is executed and it will just be output with some colors through the function print_msg.第一个参数只是解释执行哪个命令的字符串,它将只是 output 和一些 colors 通过 function print_msg。 The second argument is a command.第二个参数是命令。 I can execute another bash script with this function, but with the following command it fails:我可以使用此 function 执行另一个 bash 脚本,但使用以下命令会失败:

exec_func "Update using gitlab" \
      "git clone https://${git_username}:${git_password}@gitlab.com/*****/${GIT_PROJECT_NAME}.git"

All needed variables are set correctly (git_username, git_password, GIT_PROJECT_NAME), the command works if I copy it to the terminal and run it.所有需要的变量都已正确设置(git_username、git_password、GIT_PROJECT_NAME),如果我将它复制到终端并运行它,该命令将起作用。 Also, git is in the path - to be exact: git is located in /usr/bin/git and /usr/bin is in $PATH .此外, git 在路径中 - 确切地说: git 位于/usr/bin/git/usr/bin位于$PATH

Why am I getting this error?为什么我会收到此错误?

bash: git clone https://*****:*****@gitlab.com/*****/*****.git: No such file or directory

"$(bash "$2" >> log.txt 2>&1;echo $?)" is wrong, the shell is trying to execute a commande whose name is "$(bash "$2" >> log.txt 2>&1;echo $?)"错误,shell 正在尝试执行一个名为

'git clone https:// : @gitlab.com/ / .git' 'git 克隆 https:// : @gitlab.com/ / .git'

(all the line at once) (一次所有线路)

either you execute the command directly without an other subshell ( using $(...) already does)或者您直接执行命令而不使用其他子shell(使用 $(...) 已经这样做了)

"$($2 >> log.txt 2>&1;echo $?)"

either you use the -c option of bash to pass the command:要么使用 bash 的 -c 选项来传递命令:

"$(bash -c "$2" >> log.txt 2>&1;echo $?)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM