简体   繁体   中英

Shell Script - What does this date expression mean

I'm trying to figure out what this expression means

expr \`date "+%j"\` % 2

I know it looks at the day of the year (example today 034) but what does the \\' % 2 do?

It is better to use the $() -syntax than the backticks, it's clearer and more flexible:

expr $(date +%j) % 2

outputs 0 and returns false when the day of the year is even, outputs 1 and returns true when it's odd. % is the expr modulo operator.

echo $((10#$(date +%j) % 2))

is an almost equivalent command which does not call the external expr command. The difference is that using echo you get always true as return code; the outputs are identical. 10# is needed because otherwise a string beginning with 0 (like today's 034 ) would be interpreted as an octal number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM