& is a "bitwise and" operator. Let A=15
A = 1111 (binary)
8 = 1000 (binary)
A & B = 1000 (binary)
Question
HOwdie do,
I've searched all over Google and I have yet to find what this line does:
smartstat=$(($? & 8))
To put it in perspective, I'm attempting to directly access a the 3rd bit of an exit status.
SmartCTL by default uses 8 bits. When an error has occurred such as failed disk, it will make the third bit a 1 and send an error to the logs.
smartstat=$(($? & 8)) that someone directly accesses the third bit. Directly from the manpage of smartctl:
smartstat=$(($? & 8)) This looks at only at bit 3 of the exit status $? (since 8=2^3). The shell variable $smartstat will be nonzero if SMART status check returned "disk failing" and zero otherwise.
So, can anyone tell me what the & is doing in that line of code?
Solution
& is a "bitwise and" operator. Let A=15
A = 1111 (binary)
8 = 1000 (binary)
A & B = 1000 (binary)