bash 错误cp:无法创建常规文件“”:没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20936531/
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 09:10:14  来源:igfitidea点击:

Error cp: cannot create regular file "": no such file or directory

bashshellunix

提问by Alwyn Gardner

I am trying to move files that have been searched for from a source folder into a target folder and make them read only in the process, below is my current code, copyfile() is a subroutine that iterates the process for all files in the source folder.

我正在尝试将已从源文件夹中搜索到的文件移动到目标文件夹中,并使它们在该过程中只读,下面是我当前的代码,copyfile() 是一个子例程,它对源中的所有文件进行迭代文件夹。

    #!/bin/bash

    copyfile()
    {     
    ls -l "$f"
    read -p "Copy y/n? " yn
    if [ "$yn" == "y" ]
    then
        cp "$f" ""  
    else
        echo $f skipped
    fi
    }

    if [ -d  ] #  source folder
   then
       echo
   else
       echo This directory does not exist
   fi
   if [ -d  ] #  target folder
   then
       echo Directory Exists
   else
       mkdir 
       echo  created
   fi
   for f in /*
   do
       echo "file is $f"
       copyfile "$f"
   done

I currently have not managed to get the permission change to work for the copied files (when they have done so with different versions of the code).

我目前还没有设法获得权限更改以用于复制的文件(当他们使用不同版本的代码这样做时)。

Any help is much appreciated in advance.

任何帮助都非常感谢提前。

回答by anubhava

Have your function like this:

有这样的功能:

copyfile() {
    ls -l ""
    read -p "Copy y/n? " yn
    if [ "$yn" == "y" ]
    then
        cp "" ""
        chmod 644 "" 
    else
        echo " skipped"
    fi
}

and call it like this:

并这样称呼它:

for f in /*
do
       echo "file is $f"
       copyfile "$f" ""
done