bash cp:找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25281942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:07:13  来源:igfitidea点击:

cp: command not found

linuxbashsignalscpbash-trap

提问by VIJAY GUPTA

I am trying to copy one file to other directory and getting error message while interrupt is called.

我试图将一个文件复制到其他目录并在调用中断时收到错误消息。

The Script :

剧本 :

#!/bin/bash


PATH=~/MkFile/

exitfn () {
    trap SIGINT              # Resore signal handling for SIGINT
        echo ; echo 'Called ctrl + c '    # Growl at user,

        cp ./BKP/temp.txt $PATH/backup.txt
            exit                     #   then exit script.
}

trap "exitfn" INT            # Set up SIGINT trap to call function.ii



    read -p "What? "

    echo "You said: $REPLY"
# reset all traps## 


    trap - 0 SIGINT

Output :

输出 :

./signal.sh
What? ^C
Called ctrl + c
./signal.sh: line 9: cp: command not found

Do you have idea what is wrong in this script ??

你知道这个脚本有什么问题吗??

回答by konsolebox

You modified your PATH variable that's why. Perhaps you just want to add another path to it:

您修改了 PATH 变量,这就是原因。也许您只是想为它添加另一条路径:

PATH=$PATH:~/MkFile/

Or if in Bash, simply use the append operator:

或者如果 in Bash,只需使用附加运算符:

PATH+=:~/MkFile/

Come to think of it, I don't think you actually want to use the PATH variable. Just use another parameter name instead:

仔细想想,我不认为你真的想使用 PATH 变量。只需使用另一个参数名称:

DIR=~/MkFile/

And some would recommend just using lowercase letters to avoid conflict with builtin shell variables:

有些人会建议只使用小写字母以避免与内置 shell 变量发生冲突:

path=~/MkFile/

From the manual:

从手册:

PATH    A colon-separated list of directories in which the shell looks for
        commands.  A zero-length (null) directory name in the value of PATH
        indicates the current directory. A null directory name may appear
        as two adjacent colons, or as an initial or trailing colon.
PATH    A colon-separated list of directories in which the shell looks for
        commands.  A zero-length (null) directory name in the value of PATH
        indicates the current directory. A null directory name may appear
        as two adjacent colons, or as an initial or trailing colon.

回答by PedroA

In Linux, $PATH is an environmental variable that holds the directories to search for executable files (see for example http://www.linfo.org/path_env_var.html).

在 Linux 中, $PATH 是一个环境变量,用于保存搜索可执行文件的目录(参见例如http://www.linfo.org/path_env_var.html)。

I don't really know if your purpose is to change the PATH variable. If it is you should follow the konsolebox answer, but if not, you should avoid the use of environmental variables as variables in your script. Try using instead:

我真的不知道您的目的是否是更改 PATH 变量。如果是,您应该遵循 konsolebox 的答案,但如果不是,则应避免在脚本中使用环境变量作为变量。尝试使用:

path=~/MkFile/

路径=~/MkFile/

or

或者

MYPATH=~/MkFile/

MYPATH=~/MkFile/