文件存在但没有显示此类文件或目录

时间:2017-05-22 10:16:11

标签: bash shell command-line

我正在尝试使用bash shell脚本执行一组终端操作。以下是我的代码

#!/bin/bash
FILE_DATE=`date '+%Y%m%d'`
ARCHIVE_DIR="/home/tanmay/backup/"
TAR_GZ=".tar.gz"
PATH=( "/home/tanmay/Downloads/apache-tomcat-7.0.69/logs" "/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps" )
FOLDER=("logs" "webapps" )
for number in {0..1..1}
do
    echo ${PATH[number]}
    echo ${FOLDER[number]}
    rsync -vrzh ${PATH[number]} ${ARCHIVE_DIR}
    tar -zcvf ${ARCHIVE_DIR}/${FOLDER[number]}${TAR_GZ} ${ARCHIVE_DIR}/${FOLDER[number]}
    rm -rf ${ARCHIVE_DIR}/${FOLDER[number]}
    if [ -f ${ARCHIVE_DIR}/${FOLDER[number]}${TAR_GZ} ]
    then
        mv ${ARCHIVE_DIR}${FOLDER[number]}${TAR_GZ} ${ARCHIVE_DIR}${FOLDER[number]}_${FILE_DATE}${TAR_GZ}
    fi    
done

当我运行此脚本时,两个回显都显示正确的值。但是操作(rsync,tar ..)返回的文件未找到。以下是输出

/home/tanmay/Downloads/apache-tomcat-7.0.69/logs
logs
./server_data_backup_updated.sh: line 11: rsync: No such file or directory
./server_data_backup_updated.sh: line 12: tar: No such file or directory
./server_data_backup_updated.sh: line 13: rm: No such file or directory
/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps
webapps
./server_data_backup_updated.sh: line 11: rsync: No such file or directory
./server_data_backup_updated.sh: line 12: tar: No such file or directory
./server_data_backup_updated.sh: line 13: rm: No such file or directory

更新1

使用一个数组而不是两个。它现在正在运作。

#!/bin/bash
FILE_DATE=`date '+%Y%m%d'`
ARCHIVE_DIR="/home/tanmay/backup/"
TAR_GZ=".tar.gz"
array=( "/home/tanmay/Downloads/apache-tomcat-7.0.69/logs" 
    "logs" 
    "/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps" 
    "webapps")

for number in {0..2..2}
do
    rsync -vrzh ${array[number]} ${ARCHIVE_DIR}
    tar -zcvf ${ARCHIVE_DIR}/${array[number+1]}${TAR_GZ} ${ARCHIVE_DIR}/${array[number+1]}
    rm -rf ${ARCHIVE_DIR}/${array[number+1]}
    if [ -f ${ARCHIVE_DIR}/${array[number+1]}${TAR_GZ} ]
    then
        mv ${ARCHIVE_DIR}${array[number+1]}${TAR_GZ} ${ARCHIVE_DIR}${array[number+1]}_${FILE_DATE}${TAR_GZ}
    fi    
done

1 个答案:

答案 0 :(得分:2)

重置PATH时,shell无法再找到可执行文件rsync。当shell读取单词rsync时,它会查找名为rsync的文件名为PATH的变量(它希望是以冒号分隔的目录列表,而不是数组)。同样适用于tarrm。您看到的错误消息只是告诉您在PATH中找不到这些命令。