cp:找不到命令

VIJ*_*PTA 7 linux bash signals cp bash-trap

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

剧本 :

#!/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
Run Code Online (Sandbox Code Playgroud)

输出:

./signal.sh
What? ^C
Called ctrl + c
./signal.sh: line 9: cp: command not found
Run Code Online (Sandbox Code Playgroud)

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

kon*_*box 14

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

PATH=$PATH:~/MkFile/
Run Code Online (Sandbox Code Playgroud)

或者如果在Bash,只需使用append运算符:

PATH+=:~/MkFile/
Run Code Online (Sandbox Code Playgroud)

想想看,我认为你其实并不想使用PATH变量.只需使用其他参数名称:

DIR=~/MkFile/
Run Code Online (Sandbox Code Playgroud)

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

path=~/MkFile/
Run Code Online (Sandbox Code Playgroud)

从手册:

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.
Run Code Online (Sandbox Code Playgroud)


Ped*_*roA 6

在Linux中,$ PATH是一个环境变量,它包含搜索可执行文件的目录(请参阅http://www.linfo.org/path_env_var.html).

我真的不知道你的目的是改变PATH变量.如果是,你应该遵循konsolebox的答案,但如果没有,你应该避免在你的脚本中使用环境变量作为变量.请尝试使用:

路径=〜/ MkFile /

要么

mypath中=〜/ MkFile /