首页 > 解决方案 > 带有 bash 脚本的自定义文件复制器

问题描述

我编写了这些代码行来将用户提供的文件复制到目录中......但是条件部分不断提出错误......

这是代码:

 echo Enter filename to copy: 
 read files
 echo
 echo Enter Directory to copy files to: 
 read dir
 echo 
 echo copying file $files to $dir directory

 #check if file exists 
 if [ ! -e "$files" ] then 
     echo file does not exist
     exit 0
 else
    if [ ! -d "$dir" ]; then #check if dir exists
        mkdir ~$dir 
        echo ~$dir 
        cp $files $dir 
        ls $dir
    fi 
 fi

预期行为:作为 argv 提供的文件应复制到提供的目录,如果该目录不存在,则应创建它。如果文件不存在,则退出脚本并显示文件不存在的消息。

实际行为:syntax error near unexpected token 'else'

实际行为

标签: bash

解决方案


;在第一if条语句中缺少 a 。它应该是:

if [ ! -e "$files" ]; then

推荐阅读