Bash script, Illegal number: 08

匿名 (未验证) 提交于 2019-12-03 01:40:02

问题:

I'm running a pretty simple bash script in ubuntu but have come up with a problem. If needed I'll post the whole script, but I've narrowed down the problem. Basically, I want to run some code every 15 seconds, so I started with this:

time=`date +%S`  time2=$((time%15))  if [ $time2 -eq 0 ]  then 

etc, etc, etc....

The problem comes up when time is 08 seconds. The script terminates with Illegal number: 08. Adding to that, when using:

time2=$(($time%15)) 

instead of the illegal number error, it would terminate with Arithmetic expression: expecting EOF: "08%15"

I'm guessing 08 isn't interpreted as a number. Or there's some base issue, like it thinks it's in octal or something. Any help?

回答1:

Shortest solution:

time2=$(( ${time#0} % 15 )) 

${var#glob} means "$var with glob removed from the beginning if present".



回答2:

Try using the following flags instead

date +%-S 

It says given the -, it won't pad. It has problems with the base, interpreting it as an octal integer.

Anyway, if you want to do something every 15 seconds, i find this one easier to follow:

while ((1)); do      echo do something now...     sleep 15s done 


回答3:

Force Bash to interpret the number in decimal, no matter how many padded zeros:

time2=$((10#$time % 15)) 


回答4:

you're right, it was interpreting it as octal. bourne shells do that for any number with a leading 0 in an Arithmetic Substition:

#~ $ echo $(( 010 )) 8 #~ $ echo $(( 0100 )) 64 #~ $ echo $(( 10#0100 )) 100 #~ $ echo $(( 40#lolwut )) 2213236429  

look in the manpage for 'base#' to see all the details about this '#-forcing' thing. you can get pretty ridiculous with it if you want to



回答5:

Since you're only interested in "every fifteen seconds" rather than running things on the minute exactly you could use date +%s (lowercase s) which will give you the number of seconds since the start of the epoch. This won't have a leading 0 so your script should run fine.

However, I would wonder about your code in a wider context. If the system is running very slow for some reason it could be possible for the script only be run second 14 and then second 16 meaning it will miss an execution.

It might be worth touching a file when you do whatever it is the script does and then performing your action when then last modified date of the file is 15 or more seconds ago.



回答6:

That does look like it's interpreting it as octal.

Try date +%S | sed -e 's/^0//'



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!