简体   繁体   English

在Linux中的shell脚本中挣扎

[英]struggling with a shell script in linux

I am trying to create a script that takes two arguments, source folder and target folder. 我正在尝试创建一个使用两个参数的脚本,源文件夹和目标文件夹。 I am trying to show an error message if the source directory is not found and exit the program. 如果找不到源目录,我试图显示一条错误消息,然后退出程序。 If target directory is not found, I would like it to create that directory. 如果找不到目标目录,我希望它创建该目录。 Then I would like to copy all the contents from the source directory to the target directory. 然后,我想将所有内容从源目录复制到目标目录。

When I run the program with two arguments passed, eg /mnt/sdb1 and -root/my-documents/photoarch, I will get the error: 当我通过传递两个参数(例如/ mnt / sdb1和-root / my-documents / photoarch)运行程序时,将收到错误消息:

cp: cannot stat '/mnt/adb1/': No such file or directory cp:无法统计“ / mnt / adb1 /”:没有此类文件或目录

I would like it to show my error that I have coded and create the folder if not already created but it does not do this. 我想显示我已经编码并创建文件夹(如果尚未创建)的错误,但是它不会这样做。

Thanks 谢谢

#!/bin/sh
clear
#checking that arguments exist
if [ $# -eq 0 ]; then
    echo "Usage: phar image_path archive_path"
elif [ -d $1 ]; then
    echo "Source directory not found"
    exit
elif [ -d $2 ]; then
    mkdir ~root/my-documents/$2
else
    cp -r $1 $2
fi

You are missing negations: 您缺少各种否定词:

elif [ ! -d $1 ]; then
...
elif [ ! -d $2 ]; then

As a best-practice recommendation, I suggest you enclose all occurrences of $1 and $2 in double quotes. 作为最佳实践建议,我建议您将所有出现的$1$2用双引号引起来。 Otherwise you might run into problems with paths containing spaces. 否则,您可能会遇到包含空格的路径的问题。 For example: 例如:

cp -r "$1" "$2"

In the 3rd elif clause, you make the destination directory if it doesn't already exist, but you create it as a subdirectory of root's home directory and then attempt to copy to the non-existent directory that you just verified doesn't exist (once you invert the flags as @Stefan Majewsky noted). 在第三个elif子句中,创建目标目录(如果尚不存在),但将其创建为root主目录的子目录,然后尝试复制到刚刚验证的不存在的目录中(一旦您按照@Stefan Majewsky的指示倒置标志)。 If you are going to be creating the destination directory as a subdirectory of root's home (or anywhere else, for that matter), I would recommend restructuring your code somewhat to set variables such as SRCDIR and DSTDIR so that you can more easily manipulate them if they need to change. 如果您打算将目标目录创建为root的主目录的子目录(或其他任何地方),我建议您对代码进行某种程度的重组,以设置诸如SRCDIR和DSTDIR之类的变量,以便在以下情况下更容易操作它们:他们需要改变。 With this, you could have the code read something like this: 这样,您就可以让代码读取如下内容:

#!/bin/sh
clear
#checking that arguments exist
if [ $# -eq 0 ]; then
    echo "Usage: phar image_path archive_path"
    exit 1
fi
SRCDIR="$1"
DSTDIR="$2"

if [  ! -d $SRCDIR ]; then
    echo "Source directory not found"
    exit
elif [ ! -d $DSTDIR ]; then
    DSTDIR="~root/my-documents/$DSTDIR"
    echo "Creating destination directory $DSTDIR"
    mkdir $DSTDIR
fi

cp -r "$SRCDIR/*" "$DSTDIR"

I hope this answers your question adequately. 我希望这能充分回答您的问题。

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

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