Short takes

Random number between 5 and 19

echo $((RANDOM%15+5))

Add two integer variables i.e. a=1 b=2

a=1
b=2
echo $(expr $a + $b)
echo $(( a + b ))

File update time

echo $(date -r /bin/sh)		# date and time
echo $(date -r /bin/sh +%s)	# time in epoch seconds

Convert seconds to days, hours, minutes, seconds

age=$(( $(date +%s) - $(date -d '1941/06/28 01:43' +%s) )) # someones age in seconds including epoch correction
days=$(( age / 86400 )) 
hours=$(( age % 86400 / 3600 ))  
mins=$(( age % 86400 % 3600 / 60 ))    
secs=$(( age % 86400 % 3600 % 60 ))      
echo $days days $hours hours $mins minutes $secs seconds

Calculate # days in current month

m1=$(date +%m)				# Current month
y1=$(date +%Y)				# Current year
es1=$(date +%s -d $m1/01/$y1)		# Epoch seconds (1st of current month)
m2=$(( m1 %12 +1 ))			# Next month
y2=$y1					# Init year of next month
[ $m2  -eq 1 ] && ((y2+=1))		# Adjust year of next month
es2=$(date +%s -d $m2/01/$y2)		# Epoch seconds (1st of next month)
monlen=$(( (es2 - es1)/86400 ))		# Number of days

Send mail to the Webmaster

logo This site best viewed with a browser
Warning: This is a Debian centric site and MAY contain peanuts.
Many thanks to Debra Lynn and Ian Murdock for making Debian possible
First created ~ Last revised October 06, 2011

Valid XHTML 1.0 Strict Valid CSS!