我是bash腳本編程新手,我遇到了一個腳本問題,我想編寫一個25歲以下的驅動程序列表從一個充滿XML文件的文件夾中讀取他們的生日,並計算他們的年齡,一旦我確定他們在25歲以下,驅動程序數據的文件名就會保存到一個文本文件中,腳本一直工作到某一點,然後它,停止我得到的錯誤是:BASH錯誤:語法錯誤:期望的操作數(錯誤標記爲「)
gdate: extra operand ‘+%s’
Try 'gdate --help' for more information.
DriversUnder25.sh: line 24: (1471392000 - )/60/60/24 : syntax error: operand expected (error token is ")/60/60/24 ")
這裏是我的代碼:
#!/bin/bash
# define directory to search and current date
DIRECTORY="/*.xml"
CURRENT_DATE=$(date '+%Y%m%d')
# loop over files in a directory
for FILE in $DIRECTORY;
do
# grab user's birth date from XML file
BIRTH_DATE=$(sed -n '/Birthdate/{s/.*<Birthdate>//;s/<\/Birthdate.*//;p;}' $FILE)
# calculate the difference between the current date
# and the user's birth date (seconds)
DIFFERENCE=$((($(gdate -ud $CURRENT_DATE +'%s') - $(gdate -ud $BIRTH_DATE +'%s'))/60/60/24))
# calculate the number of years between
# the current date and the user's birth date
YEARS=$(($DIFFERENCE/365))
# if the user is under 25
if [ "$YEARS" -le 25 ]; then
# save file name only
FILENAME=`basename $FILE`
# output filename to text file
echo $FILENAME >> DriversUnder25.txt
fi
done
我不知道爲什麼它正確輸出前10個文件名,然後停止。任何想法,爲什麼這可能會發生?
檢查'$ BIRTH_DATE'的值。你可以將它回顯到stderr,用'echo birthdate查看它是\「$ {BIRTH_DATE} \」「1>&2' –
我得到的結果是'birthdate is」19920215「' – Kim
使用'set -x'來查看如何擴展變量值你應該在你的第10條記錄上看到數據正在發生變化你的XML文件可能有問題,日期格式不同或其他問題嗎?祝你好運 – shellter