Find the answer to your Linux question:
Results 1 to 3 of 3

Thread: shell script

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    shell script


    hi,

    please help me, i want to write a script such a way that i want to validate that input which is coming from "read" statement.

    if it starts from d,c or i only then it has to be print the message
    else it should throw an error.

    example:
    if we entry: d1234,c1235,idfgry,rttry

    it should check all the words separated by comma and throw an error for word "rttty" which is invalid entry...

    example script:
    Enter the user name starts with d/c/i

    d1234,c2345,i1246,t23565

    Error: invalid entry " t23565"


    Regards,
    Rajini.L

  2. #2
    Just Joined!
    Join Date
    Jul 2005
    Location
    Montreal Quebec Canada
    Posts
    35

    Splitting a string apart

    If bash has the scan function use it.
    Here is something I did with SED the stream editor
    Code:
    #################################################################
    # Functions used in this shell script                           #
    #################################################################
    function prologue()
    {
    #    date "+%Y %m %d %H %M %S" 
    #    a=(`date "+%Y %m %d %H %M %S" | sed -e 's/x/ /g'`)
    #    year=${a[0]}
    #    month=${a[1]}
    #    day=${a[2]}
    #    hour=${a[3]}
    #    minute=${a[4]}
    #    sec=${a[5]}
    #    echo "year=$year,month=$month,day=$day,hour=$hour,minute=$minute,second=$sec"
        getdate
        echo -e "<!-- background for many jpg and pngs -->\n"       >$xml
        echo "<background>"                                         >>$xml
        echo " <starttime>"                                         >>$xml
        echo "  <year>$year</year>"                                 >>$xml
        echo "  <month>$month</month>"                              >>$xml
        echo "  <day>$day</day>"                                    >>$xml
        echo "  <hour>$hour</hour>"                                 >>$xml
        echo "  <minute>$minute</minute>"                           >>$xml
        echo "  <second>$sec</second>"                              >>$xml
        echo " </starttime>"                                        >>$xml
        echo ""                                                     >>$xml
        return
    }
    The sed editor split the date string into an array a of bash variables
    a[0] is the first item, a[1] the second, etc.
    If your input is always comma terminated, you could let sed create the array and then compare the array entry for valid acceptance

  3. #3
    Linux Guru
    Join Date
    Dec 2013
    Posts
    2,747
    Quote Originally Posted by rajinil View Post
    please help me, i want to write a script such a way that blah blah
    to me this reads like "please write it for me" - which i won't do.
    so, what did you write, how does it work and how does it fail?
    show us the script, sample input and output.
    use code tags for code.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •