Skip to main content
added 75 characters in body
Source Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262

You could do something likeget the date for next Sunday, add N days and print the day name for that particular date:

getdayname () {
date -d "$(( $nextplus=$(date -d "$(( 7-$(date '+%u') ))"next days"Sunday '+%u')+$1 )days")
date days"-d "${nextplus}" '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n "${days[$1-1]}" (as indexing starts at 0), and quotes are needed to avoid split+glob (also with yash).

You could do something like

getdayname () {
date -d "$(( $(date -d "$(( 7-$(date '+%u') )) days" '+%u')+$1 )) days"  '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n "${days[$1-1]}" (as indexing starts at 0), and quotes are needed to avoid split+glob (also with yash).

You could get the date for next Sunday, add N days and print the day name for that particular date:

getdayname () {
nextplus=$(date -d "next Sunday +$1 days")
date -d "${nextplus}" '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n "${days[$1-1]}" (as indexing starts at 0), and quotes are needed to avoid split+glob (also with yash).

added 65 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

You could do something like

getdayname () {
date -d "$(( $(date -d "$(( 7-$(date '+%u') )) days" '+%u')+$1 )) days"  '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n $"${days[$1-1]}" (as indexing starts at 0), and quotes are needed to avoid split+glob (also with yash).

You could do something like

getdayname () {
date -d "$(( $(date -d "$(( 7-$(date '+%u') )) days" '+%u')+$1 )) days"  '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n ${days[$1-1]} (as indexing starts at 0)

You could do something like

getdayname () {
date -d "$(( $(date -d "$(( 7-$(date '+%u') )) days" '+%u')+$1 )) days"  '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n "${days[$1-1]}" (as indexing starts at 0), and quotes are needed to avoid split+glob (also with yash).

replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

You could do something like

getdayname () {
date -d "$(( $(date -d "$(( 7-$(date '+%u') )) days" '+%u')+$1 )) days"  '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n ${days[$1-1]} (as indexing starts at 0starts at 0)

You could do something like

getdayname () {
date -d "$(( $(date -d "$(( 7-$(date '+%u') )) days" '+%u')+$1 )) days"  '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n ${days[$1-1]} (as indexing starts at 0)

You could do something like

getdayname () {
date -d "$(( $(date -d "$(( 7-$(date '+%u') )) days" '+%u')+$1 )) days"  '+%A'
}

and run getdayname 2, getdayname 7 etc (assuming gnu date).
Or simply use an array and do without the date e.g.

getdayname () {
local days=( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
printf %s\\n ${days[$1]}
}

though with bash/ksh you need printf %s\\n ${days[$1-1]} (as indexing starts at 0)

added 104 characters in body
Source Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262
Loading
Source Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262
Loading