• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Linux: a date program

B

Bretenn

Guest
#1
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 :_
 
M

MadHaXX()|2

Guest
#2
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.
 
B

Bretenn

Guest
#3
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
 
M

MadHaXX()|2

Guest
#4
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;
}
 
B

Bretenn

Guest
#5
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.....
 
M

MadHaXX()|2

Guest
#6
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.
 
M

MadHaXX()|2

Guest
#7
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,962
Messages
673,240
Members
89,015
Latest member
loxioalix