Operating System - HP-UX
1828423 Members
3378 Online
109977 Solutions
New Discussion

Re: Function within a script not working

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

Function within a script not working

Hello,

Attached is a script which I am trying to verify the date entered DDMM has been entered correctly but I cannot get the sytax correct.

please help

thnx
hello
10 REPLIES 10
lawrenzo
Trusted Contributor

Re: Function within a script not working

please note that I know there is a comment #

ty
hello
Sridhar Bhaskarla
Honored Contributor

Re: Function within a script not working

Hi,

'date +%d%m' inside sed will be treated as a string.

cat $i |sed s/'date +%d%m'/$SPFCHANGE/g >tmpfile

Modify the above as follows

DATE=$(date +%d%m)
sed 's/'${DATE}'/'$SPFCHANGE'/g' $i > tmpfile

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
lawrenzo
Trusted Contributor

Re: Function within a script not working

sorry,

I should have explained, the part that isn't working is the test to check the date (the commented line)

hello
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Function within a script not working

Hmmm.. I will use a case statement

case $SPFCHANGE in

[0-9][0-9][0-9][0-9]) call_your_function

;;

*) echo "There has been a problem changing the date, please check the date format" ; exit 1
;;

esac
You may be disappointed if you fail, but you are doomed if you don't try
Michael Schulte zur Sur
Honored Contributor

Re: Function within a script not working

Hi,

what about:
case $SPFCHANGE in
[0-9][0-9][0-9][0-9]) for i in ...
*) echo "There has been a problem changing the date, please check the date format" ;
exit;;
esac
Sridhar Bhaskarla
Honored Contributor

Re: Function within a script not working

And your sed is still incorrect. You will find it anyway while testing.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
lawrenzo
Trusted Contributor

Re: Function within a script not working

Thanks guys that's done it!
hello
A. Clay Stephenson
Acclaimed Contributor

Re: Function within a script not working

Here is one technique using awk's pattern matching

OK=0
while [[ ${OK} -eq 0 ]]
do
echo "Please enter the date of the file you wish to transfer (DDMM): \c"
read SPFCHANGE
echo ${SPFCHANGE} | awk '{if ($0 ~ /^[0-9]{4}$/) exit(1); else exit(0)}'
OK=${?}
if [[ ${OK} -eq 0 ]]
then
echo "\aBad format; re-enter"
fi
done


If it ain't broke, I can fix that.
lawrenzo
Trusted Contributor

Re: Function within a script not working

brilliant, that's much better thanks Clay.
hello
Arnold Hausmann
Occasional Advisor

Re: Function within a script not working

A modification to Clay's excellent script will do validation of the date.

OK=0
typeset -i DD
typeset -i MM
while [[ ${OK} -eq 0 ]]
do
echo "Please enter the date of the file you wish to transfer (DDMM): \c"
read SPFCHANGE
echo ${SPFCHANGE} | awk '{if ($0 ~ /^[0-3][0-9][0-1][0-9]$/) exit(1); else exit(0)}'
OK=${?}
if [[ ${OK} -eq 0 ]]
then
echo "\aBad format; re-enter"
else
DD=`echo $SPFCHANGE | cut -c 1-2`
MM=`echo $SPFCHANGE | cut -c 3-4`
if [ ${DD} -gt 31 -o ${MM} -gt 12 ]
then
echo "\aBad format; re-enter"
OK=0
else
OK=1
fi
fi
done

Regards
Stuff happens...that's why Jesus saves.