Passing Day in string

I need to pass days in past 3, 4, 5 day in strings.

Today is Feb 7, so past 3, 4, 5 days are:

day1="Feb 2"
day2="Feb 3"
day3="Feb 4"

I have set day1 day2 day3 as the variables. So, what I need is to have the day stings to be past 3, 4, 5 days every day.

If it Feb 8, they need to be:

day1="Feb 3"
day2="Feb 4"
day3="Feb 5"

If it Feb 9, they need to be:

day1="Feb 4"
day2="Feb 5"
day3="Feb 6"

and so on.

The format of string needs to "Feb 3" (Uppercase for the first letter, Jan, Feb, Mar, etc; space ; numeric for the day of the month).

Thank you so much for your help!

Please provide information on your attempts to resolve.

Until so, we will refrain from sharing any guidance.

The purpose of this Board is the assist users in solving their problems. We are not a coding service. Further, we are not a homework service - and sometimes posts appear to be an attempt to have someone solve a school assignment.

Finally, we urge all members of the Forum to NOT post a solution to this question until effort to resolve is demonstrated.

This is what I have so far, and I am kind of doing manual coding every day for day1, day2, day3
But, I like to automate it so that I can eliminate manual coding. Thank you!

print_cleanup_exec=$cer_log/print_cleanup_output.log
exec 1> $print_cleanup_exec
day1="Feb 2"
day2="Feb 3"
day3="Feb 4"

echo "-------------------------------------------------------" > ${print_cleanup}
echo "Header to print cleanup file :   \n"  >> ${print_cleanup}
echo "         Date : `date`  \n" >> ${print_cleanup}
echo " " >> ${print_cleanup}
prior=`lpstat -o |grep -v bytes |wc -l`
cleaned=`lpstat -o |grep -v bytes |sort -nkb1 |grep -v "${day1}" |grep -v "${day2}" |grep -v "${day3}" |wc -l `
....... snipped .........

Welcome to the forum.

Please get accustomed to providing system details like OS, shell, tools' versions, so people can taylor their proposals.

Do you have a date version that offers the -d (--date=STRING) ?

$ date +"%b %_d" -d"2 day ago"
Feb  5

Thank you, RudiC!

root:root> uname -a
HP-UX B.11.31 U ia64 0123365846 unlimited-user license

It is HP-UX, so it is kind of tough.. I am getting this error

root:> date +"%b %_d" -d "2 day ago"
date: bad format character - _

check LOCALE value for cal format, try:

date=$(date +"%d %m %Y")
#date="8 2 2019"

echo $date | read tday x

past_days="3,4,5"
max_past_day="${past_days##*,}"

echo "$date" | awk '
{
   m=$2; y=$3;
   if ($1 <= mpd) {
      mm=m; yy=y;
      if (mm-1 < 1) { mm=12; yy=y-1; } else { mm--; }
      print "cal ", mm, yy
   }
   print "cal ", m, y
}
' mpd=$max_past_day | sh | awk '
length($1) > 3 {month=substr($1, 1, 3)}
$1+=0 == $1  {for (i=1; i<=NF; i++) {days[c++]=month FS $i; daysa[d++]=$i;}}
END {
   sc=split(past_days, pdays, ",");
   for (j=1; j<=sc; j++) ppdays[pdays[j]]=pdays[j];
   for (i=c-1; i>=0; i--) {
      if (daysa==tday) ptr=1;
      if (ptr && (prtc++ in ppdays)) out[outc++]=days;
   }
   for (i=outc-1; i>=0; i--) print "day" (++k) "=\"" out "\"";
}
' tday=$tday past_days="$past_days"
1 Like

Everything my dear colleague RudiC said applies, but also, just to show you that posting your stuff is a rewarding activity:

Under NO circumstances you should use backticks any more! If you are not the rare exception who uses an original Bourne shell from the time computers ran on steam instead of electrical power you have a shell that can (and should) use "POSIX style" subshells:

prior=$(lpstat -o |grep -v bytes |wc -l)

The same applies here but in addition the grep can be straightened. The sort is superfluous because you want to know the number of lines at the end which is independent of the sorting:

cleaned=$( lpstat -o | grep -ve bytes -ve "${day1}" -ve "${day2}" -ve "${day3}" | wc -l )

I hope this helps.

bakunin

1 Like

Welcome.

The first thing I would try encountering that error message is relaunch the command without the _ and / or consult the man page. Having said that, I'm afraid that your date will fail on the -d option. So - rely on e.g. rdrtx1's proposal. Or - search these forums for "HP-UX" and "date" keywords.

1 Like

Thank you so much!!! I will modify my script and run it for 3 days to validate that everything runs ok.
Appreciate it your help! I will keep you posted!

I am new to shell programming, here is what I did.

#!/bin/sh

# Code is POSIX compliant


currentDate="$(date -R)"


counter=0

for word in $currentDate
do

  # We ignore the day of the week.
  if [ $counter -eq 0 ]
  then

    counter=$((counter+1))
    continue
  fi


  # We store the day of the month.
  if [ $counter -eq 1 ]
  then
    dayOfMonth=$word

    counter=$((counter+1))

    continue
  fi


# We store the name of the month.
  if [ $counter -eq 2 ]
  then
    month=$word

    break
  fi

done


echo "$month $dayOfMonth"


exit 0

It outputs:

Feb 08

From there, it remains to create the days you want, from this result.

1 Like

I completed the code:

#!/bin/sh

# Code is POSIX compliant

tput clear

currentDate="$(date -R)"


counter=0

for word in $currentDate
do

  # We ignore the day of the week.
  if [ $counter -eq 0 ]
  then

    counter=$((counter+1))
    continue
  fi


  # We store the day of the month.
  if [ $counter -eq 1 ]
  then
    dayOfMonth=$word

    counter=$((counter+1))

    continue
  fi

  # We store the name of the month.
  if [ $counter -eq 2 ]
  then
    month=$word

    break
  fi

done


dayOfMonth=$(echo "$dayOfMonth" | tr --delete 0)


day1="$month $((dayOfMonth-5))"

day2="$month $((dayOfMonth-4))"

day3="$month $((dayOfMonth-3))"


echo "day 1: $day1"
echo "day 2: $day2"
echo "day 3: $day3"


exit 0

It outputs:

day 1: Feb 3
day 2: Feb 4
day 3: Feb 5
[john@manjaromatepc Downloads]$ 
1 Like

I thought of a better way on this script (if it is possible)

lpstat -o |grep -v bytes |sort -nkb1

returns the outputs, i.e.

sld-8638            prod         priority 1  Feb 14 11:51
sld-8719            prod         priority 1  Feb  8 09:24
sld-8782            prod         priority 1  Feb 14 12:13
sld-8788            prod         priority 1  Feb 14 12:14
sld-8790            prod         priority 1  Feb 14 12:14
sld-8800            prod         priority 1  Feb 14 12:18
sld-8961            prod         priority 1  Feb 16 14:53
... <snipped> ....

If we can add

mtime +3

like:

lpstat -o |grep -v bytes |sort -nkb1 | ?? mtime +3 ?? |awk {'print $1'} > cleanuplist.txt 

, the script can be a whole lot simpler and better.

But, HP-UX doesn't have mtime..

root:> man mtime
No manual entry for mtime.

Is there a way that we can add the equivalent of "mtime +3" to

lpstat -o |grep -v bytes |sort -nkb1 | ?? mtime +3 ?? |awk {'print $1'} > cleanuplist.txt 

?

Thank you so much!