【问题标题】:check if directory or file exist or not on ftp server and put it if does not exist检查ftp服务器上是否存在目录或文件,如果不存在则放置
【发布时间】:2019-02-26 05:41:40
【问题描述】:

我正在编写一个 shell 脚本来检查 FTP 服务器上是否存在指定的目录或文件。我想检查 FTP 服务器上是否存在 lib 目录和 web.config 文件,如果文件或目录不存在,则创建一个目录并将文件放在指定位置(站点/wwwroot)。我已经写了一段sn-p代码。

ftp -ipn $ftphost <<EOF
user $username $pswd
binary
cd site/wwwroot
ls web.config
cd lib
quit
EOF

if [[ $? -eq 0 ]]
then 
  echo "Files Exist";
  ftp -ipn $ftphost <<EOF
  user $username $pswd
  binary
  cd site/wwwroot
  del web.config
  cd lib
  mdel *
  cd ..
  rmdir lib
  mkdir lib
  mput web.config
  quit
EOF
else
  echo "The Files does not Exists";
  ftp -ipn $ftphost <<EOF
  user $username $pswd
  binary
  cd site/wwwroot
  mkdir lib
  mput web.config
  quit
EOF
fi

【问题讨论】:

    标签: shell ftp sh


    【解决方案1】:

    这是一个示例 shell 脚本,用于检查文件或目录是否存在,将文件和直接路径替换为实际路径。

    #!/bin/bash
    file="web.config"
    directory="lib"
    
    # check if file exist, if not then create
    if [ -f $file ];then echo "File exist"; else echo "File does not exist" && touch /path/$file;fi
    
    # check if directoy exist, if not then create
    if [ -d $directory ];then echo "File exist"; else echo "directory does not exist" && mkdir -p /path/$directory;fi
    

    【讨论】:

    • 我必须检查 FTP 上是否存在指定的文件/目录并根据它执行操作。情况1:如果文件/目录已经存在,则清理并上传新文件。情况2:如果文件不存在,则上传文件并创建目录而不进行清理。
    猜你喜欢
    • 2013-01-16
    • 1970-01-01
    • 2011-11-16
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    相关资源
    最近更新 更多