Linux: a date program

B

Bretenn

Guest
i wan to create a linux program...... and 1 of the option is to enter a valid date.... below is the code for the date

while :
do
printf "Date Commissioned (dd/mm/yy): "
read date

if test -z "$date"
then
echo "ERROR: field cannot be blank"
else
break
fi
done

this code only check for null........ how can i check or detect if the user type in character instead of numeric..... and then print a message.....
and the length is only 6...
what can i add into the code if want to enable this error..... i only wan some simple coding....... and if can please add the code to the date code.....10q
thanks in advance....... hope u guys can help me :_
 
what shell/scripting language are you wanting to write it in?

you need to use a typeset on your variables (unless you're using C then just define your var type) and write an algorithm that iterates through (foreach) each letter in the string passed into your variable [if $var='a' || $var='Z' ....] or check for invalid entries...and so on.

if you tell me what language/shell you want to write it in i would be better help. like i said before, using perl is about the best thing for writing scripts in unix/linux. bourne(family) shells and c(family) shells suck to write things like that in. if this is for school or something then i guess you have to wirte them in shell. otherwise use perl.
 
bourne shell.... 10q....................
and btw how to auto-generate id number in a file in the 1st field($1)
e.g.
1:software:asda:asda:asda:asd:asda
2:hardware:sdf:sdgfs:sdgsg:sdgsd:sdg
 
here's a random number bit (cut & paste)
~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/sh

lb=1
hb=10
r_max=32767
b_num=$(( $lb + ($hb * $RANDOM) / ($r_max + 1) ))

echo $b_num
~~~~~~~~~~~~~~~~~~~~~~~
this will give you a random number between 1-10, to change the range edit the lb & hb var's, or just reference $RANDOM to give you a general random # with no limits.

okay i didnt read your post right, dont use this cause you could get reaccuring keys...

you want to keep a consistent counting through out the data entry area, wrap the process in a for loop and use the iterator var as the id number or use a while loop that starts by incrementing a var and use that var as your id (fill one id per loop, until all are filled)
ex (psudo code):

num_of_files=#;
for i=0, $i <= $num_of_files, $i++
{
code...fill 1st feild=$i;
}

or

i=0;
while true
{
$i++;
code...fill 1st feild=$i;
}
 
10q... i will try now, and btw how to do the validation for the date that i mention earlier?..... because if i my current code..... it will only check if it is null. and i cant check if it only contains 6 number which is ddmmyy...... or more complicated is dd/mm/yy and to check whether the user key in numeric number or not.........

how can i do that kind of validation?? i really appreciate ur help 10q.....
 
for date matching ou need to check

if [[ $date='1.9' || $date='-' || $date='\\' || $date='/' ]]
then
.....code
fi

its been a while since i've done any sh scripting so i think thats how it'd look. but basically i'm doing a range check with the 1.9 (one through nine) characters are allowable as are the '-' '\\' '/' characters. if that is true then you have entered a valid sequence. you can also do a not equal a.Z so it does a character matcing for lowercase 'a' through uppercase 'Z' and all the alphabet inbetween.
 
here's a better way using sed

Code:
while true
sed -e '/0.9/' $date & break
sed -e '/0.9/' $date | echo 'invalid input: format=mmddyy'
sed -e '/a.Z/' $date & echo 'invalid input: format=mmddyy'

sed is used as a regular expressions utility and will search for a match of characters. the above uses the '&' and will break if the previous statement returns no errors, the next line will echo "invalid input: format=mmddyy" if the previous statement returns with any errors.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back