我正在尝试执行以下操作:
我做了以下事情:
set -x
substring()
{
VAR=$1
OFFSET=$2
LENGTH=$3
RET_VAL=${VAR:$OFFSET:$LENGTH}
}
# $1 filename
# Change to PF Gazetteer directory
cd $CARRIER_PF_UPDATES_DIR
LOG=log.txt
DownloadedFile=$1
OldFile=
DATE=$(date +"%Y-%m-%d-%H:%M:%S")
RET=0
if [ "${2+1}" == "1" ]
then
echo "Date: " $DATE > $LOG
echo "filename: " $DownloadedFile >> $LOG
fi
substring ${DownloadedFile} -3 3
extension=$RET_VAL
old_ext=$((${extension}+1))
substring ${DownloadedFile} 0 9
echo $RET_VAL
oldFile="${RET_VAL}${old_ext}"
#oldFile=`printf "%02d" ${RET_VAL}`
#printf -v oldFile %02d "$RET_VAL"
#printf -v oldFile %02d "$((10#$RET_VAL - 1))"
extension=${1##*.}
printf -v decr "%s.%0${#extension}d" "${1%.*}" "$((10#$extension - 1))" \
&& cat -- "$decr" "$1" > temp \
&& rm -- "$decr" \
&& mv -- temp "$1"
oldFile="${oldFile}${old_ext}"
echo $oldFile
# move any temporary files if they exist
mv "${DownloadedFile}~" ${DownloadedFile}
# check to see if we got a file
result=$(grep 226 run.ilog)
if [ "${2+1}" == "1" ]
then
echo "grep found: " $result >> $LOG
fi
if [ -e "${DownloadedFile}" ]
then
#add back onto filename
#store back onto var = $OldFile
#Check if OldFile exist
#Cat files together and - Cat oldFile DownloadedFile > tempFile
echo ""
fi
if [ "$result" == "" ]
then
# File not found
RET=100
else
RET=10
# START F4202
sorted=$(sort -t'+' -k7 ${fileName} > ${fileName}.sorted)
if [ -e "${fileName}.sorted" ]
then
mv "${fileName}" "unsorted/${fileName}.unsorted"
mv "${fileName}.sorted" ${fileName}
fi
# END F4202
fi
if [ "${2+1}" == "1" ]
then
echo "script ret: " $RET >> $LOG
fi
exit $RET
但是,当我运行此命令时,当我输入FileName.005 Pastie
时,我会在终端中收到以下内容应该发生什么事情 1)我输入一个文件名,即Filename.005,它检查它是否存在
2)从Filename.004
获取旧文件,即减少的文件3)然后将内容合并在一起,使其成为一个文件。
我是否正确完成了这项工作。请原谅我有任何错误,因为我是bash的新手。
答案 0 :(得分:0)
问题是${DownloadedFile}~
不存在,因此移动失败。你应该先移动前检查它是否存在。
[ -e "${DownloadedFile}~" ] && mv "${DownloadedFile}~" "${DownloadedFile}"