1
我需要最后3个字符说002,并减少它1 001。我目前有一个问题,它将它转换为一个int。所以001变成1.我试图做的是使用printf并将其转换回001.然而,这似乎不适用于我。我有以下几点:零填充Bash/Shell
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我似乎得到1.1而不是1.1的时候我应该得到001
这个工程,但现在我得到00000000000000000000.1 – David
那么这也难怪考虑为垫 – Hubro
格式化有什么问题?我仍然是一个初学者,所以请原谅我 – David