当前位置: 技术问答>linux和unix
expr使用的问题
来源: 互联网 发布时间:2015-01-11
本文导语: 本人编写一脚本: #!/bin/sh #filename:static #the total capacity of some directories TOTAL=0 echo "Input the year--month for static(YYMM) like 0212 :c" read YYMONTH echo "Input the capacity of your disk (/G):c" read DISKCAPACITY du -k |grep '$YYMONTH...
本人编写一脚本:
#!/bin/sh
#filename:static
#the total capacity of some directories
TOTAL=0
echo "Input the year--month for static(YYMM) like 0212 :c"
read YYMONTH
echo "Input the capacity of your disk (/G):c"
read DISKCAPACITY
du -k |grep '$YYMONTH'|while read LINE
do
CAPACITY=`awk '{printf("%d", $1)}'`
for DIRCAPACITY in $CAPACITY
do
TOTAL=`expr $TOTAL + $DIRCAPACITY`
done
done
TOTAL=`expr $TOTAL /1024 /1024`
echo "the total capacity of the $YYMONTH is $TOTAL G"
if ("$DISKCAPACITY" -lt "$TOTAL");then
echo "the capacity of the directories is greater than the capacity of your disk.n"
echo "please replace the bigger disk which is greater than $DISKCAPACITY"
else
echo "OK!The capacity of your disk is greater than the capacity of the directories."
fi
exit 0
可老是有以下问题出现:
Input the year--month for static(YYMM) like 0212 :0211
Input the capacity of your disk (/G):8
expr: Syntax error
the total capacity of the 0211 is G
./static.sh[22]: 8: not found.
OK!The capacity of your disk is greater than the capacity of the directories.
希望指点!
#!/bin/sh
#filename:static
#the total capacity of some directories
TOTAL=0
echo "Input the year--month for static(YYMM) like 0212 :c"
read YYMONTH
echo "Input the capacity of your disk (/G):c"
read DISKCAPACITY
du -k |grep '$YYMONTH'|while read LINE
do
CAPACITY=`awk '{printf("%d", $1)}'`
for DIRCAPACITY in $CAPACITY
do
TOTAL=`expr $TOTAL + $DIRCAPACITY`
done
done
TOTAL=`expr $TOTAL /1024 /1024`
echo "the total capacity of the $YYMONTH is $TOTAL G"
if ("$DISKCAPACITY" -lt "$TOTAL");then
echo "the capacity of the directories is greater than the capacity of your disk.n"
echo "please replace the bigger disk which is greater than $DISKCAPACITY"
else
echo "OK!The capacity of your disk is greater than the capacity of the directories."
fi
exit 0
可老是有以下问题出现:
Input the year--month for static(YYMM) like 0212 :0211
Input the capacity of your disk (/G):8
expr: Syntax error
the total capacity of the 0211 is G
./static.sh[22]: 8: not found.
OK!The capacity of your disk is greater than the capacity of the directories.
希望指点!
|
有两个地方错了:
1。
TOTAL=`expr $TOTAL /1024 /1024`
应该改为
TOTAL=`expr $TOTAL / 1024 / 1024`
/后面要空格
2。
if ("$DISKCAPACITY" -lt "$TOTAL")
应该改为
if [ $DISKCAPACITY -lt $TOTAL ]
注意[后面和]前面要空格
1。
TOTAL=`expr $TOTAL /1024 /1024`
应该改为
TOTAL=`expr $TOTAL / 1024 / 1024`
/后面要空格
2。
if ("$DISKCAPACITY" -lt "$TOTAL")
应该改为
if [ $DISKCAPACITY -lt $TOTAL ]
注意[后面和]前面要空格