Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Better solution? 1

Status
Not open for further replies.

Einstein47

Programmer
Nov 29, 2001
737
US
Hey all,

I have a script that generates a report based on the month that a user enters. Is there a better way to do this than I have here? I'm sure there must be something more simple, and I am always wanting to learn.
Code:
#!/bin/ksh

ACTION=$(expr ${1} )
if [ -z "$ACTION" ];then
  echo "Enter month to report (enter for current) : \c"
  read tmp
else
  tmp=`echo " " | awk -v MONTH=$ACTION '{ if ( index( "123456789101112", MONTH ) != 0 ) { print MONTH } }'`
fi

year=$(date +%Y)
tmonth=$(date +%m)
if [ "$tmp" = "" ];then
  month=$tmonth
else
  month=$(echo "0$tmp"|awk '{ print substr($1,length($1)-1) }' )
  if [ $month -gt $tmonth ];then
    (( year = year - 1 ))
  fi
fi
Which at the end I have two variables: month and year. This code is really ugly and if someone has something more direct or simple, I'd be willing to learn (and give stars).

Thanks, Einstein47
("For every expert, there is an equal and opposite expert." - Arthur C. Clarke)
 
Hi Einstein:

These sort of problems are messy because you have to check whether the data is valid, and then check again because, the user has a second chance to mess up.

Mines mess also, but I'm eliminating the call to awk to strip off the preceding 0. Performing Korn shell arithmetic handles the preceding zeros. Hey, if your is working, do you really want to change:

Regards,

Ed


#!/bin/ksh
typeset ACTION i=0

# is_digit: This function matches an all digits regular expression
# against an argument and returns 1 if the argument is digits else 0
# if isn't.
# Note the use of nawk.
function is_digit
{
echo $1|nawk ' { if ($0 ~ /^[0-9]+$/)
print 1
else
print 0 } '
} # end is_digit

if [ $(is_digit $1) -eq 1 ]
then
ACTION=$1 # it's a digit
else
ACTION=0
fi

year=$(date +%Y)
tmonth=$(date +%m)

if [ $ACTION -eq 0 ]
then
echo "Enter month to report (enter for current) : \c"
read ACTION
# do the numeric check again
if [ $(is_digit $ACTION) -eq 0 ]
then
ACTION=$tmonth # not good, default
fi
fi

if ((ACTION < 1 || ACTION > 12))
then # is it a good month?
$ACTION=$month # No
fi
if [ $ACTION -gt $tmonth ]
then
((year = year - 1))
fi

echo $ACTION
echo $year

 
Einstein:

I'm sorry, but the above had a syntax when the command line argument was invalid (i.e. > 12)

#!/bin/ksh
typeset ACTION i=0

# is_digit: This function matches an all digits regular expression
# against an argument and returns 1 if the argument is digits else 0
# if isn't.
# Note the use of nawk.
function is_digit
{
echo $1|nawk ' { if ($0 ~ /^[0-9]+$/)
print 1
else
print 0 } '
} # end is_digit

if [ $(is_digit $1) -eq 1 ]
then
ACTION=$1
else
ACTION=0
fi

year=$(date +%Y)
tmonth=$(date +%m)
#echo $year
#echo $tmonth

if [ $ACTION -eq 0 ]
then
echo &quot;RIGHT&quot;
echo &quot;Enter month to report (enter for current) : \c&quot;
read ACTION
# do the numeric check again
if [ $(is_digit $ACTION) -eq 0 ]
then
ACTION=$tmonth
fi
fi

if ((ACTION < 1 || ACTION > 12))
then # is it a good month?
ACTION=$tmonth
fi
if (( ACTION > tmonth ))
then
((year = year - 1))
fi

echo $ACTION
echo $year
 

30 line code to compute 2 digits, is this a programm ?? or like geman people says: 'selbstbefriedigung'
why not simply:

while .
do case x${mon:=$1} in x[1-9]|x1[0-2]) break
;; x) echo &quot;enter a value between 1 and 12 \c&quot;; read mon
;; *) echo wrong value entered exiting ; exit 1
;; esac
done

actyear=`date +%Y`
actmon=`date +%m`

[ $mon -gt $actmon ] && actyear=`expr $actyear - 1`
 
Thanks jamisar, that did what I was looking for. Much more simple and easier to debug.

Great! Einstein47
(&quot;For every expert, there is an equal and opposite expert.&quot; - Arthur C. Clarke)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top