1

我需要最后 3 个字符说 002 并将其递减 1 001。我目前有一个问题,它将其转换为 int。所以 001 变为 1。我想做的是使用 printf 并将其转换回 001。但这似乎对我不起作用。我有以下内容:

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 "%020d" ${RET_VAL}`
oldFile="${oldFile}.${old_ext}"

#printf '%03d\n' 1

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

要查看输出,请单击以下链接Ideone当我应该得到 001 时,我似乎得到的是 1.1 而不是 1.1

4

1 回答 1

3

这将不起作用:

oldFile= `printf "%020d" ${RET_VAL}`

删除之后的空格=

于 2012-04-30T12:14:35.867 回答