Linux: a date program

Discussion in 'Linux & BSD' started by Bretenn, Nov 23, 2002.

  1. Bretenn

    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 :
    printf "Date Commissioned (dd/mm/yy): "
    read date

    if test -z "$date"
    echo "ERROR: field cannot be blank"

    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 :_
  2. MadHaXX()|2

    MadHaXX()|2 Guest

    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.
  3. Bretenn

    Bretenn Guest

    bourne shell.... 10q....................
    and btw how to auto-generate id number in a file in the 1st field($1)
  4. MadHaXX()|2

    MadHaXX()|2 Guest

    here's a random number bit (cut & paste)

    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):

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


    while true
    code...fill 1st feild=$i;
  5. Bretenn

    Bretenn Guest

    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.....
  6. MadHaXX()|2

    MadHaXX()|2 Guest

    for date matching ou need to check

    if [[ $date='1.9' || $date='-' || $date='\\' || $date='/' ]]

    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.
  7. MadHaXX()|2

    MadHaXX()|2 Guest

    here's a better way using sed

    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.