Find the answer to your Linux question:
Results 1 to 3 of 3
I'm looking for a bit of help with some BASH script i'm putting together to query an SQL database, to make the system more user frienly, and stop the SQL ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    2

    Error checking and correcting Variable Inputs

    I'm looking for a bit of help with some BASH script i'm putting together to query an SQL database, to make the system more user frienly, and stop the SQL server falling to its knee's with an incorrect query, i'm having to put large restrictions on values enterred.

    Here is where I am so far...

    echo
    echo "Please enter the month to query, format: 00, between 01 and 12"
    echo
    read START_MONTH
    echo

    if [ "$START_MONTH" -lt "10" ]
    then
    if [ "$START_MONTH" != "0" ]
    then
    START_MONTH="0$START_MONTH"
    else
    echo
    fi;
    fi

    echo "$START_MONTH"

    if [ "$START_MONTH" -ge "1" ]
    then
    if [ "$START_MONTH" -gt "12" ]
    then
    echo "Month must be between 01 and 12"
    echo
    exit 0;
    fi;
    else
    echo "Month must be between 01 and 12"
    echo
    exit 0;

    fi


    What I need, is a way of forcing any variable set below 10, with the number format not specified as starting with a 0, for example, 01, 02 etc. then I need the query to automatically place a 0 beforehand.

    I feel somewhat close as the code does that, however if the code already has a 0 in front, it will continue to add a 0

    Hope someone can give me a bit of advice, cheers.

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    The thing to do is check the length of the data. If its length is one then prefix a 0.

    Code:
    if [ ${#START_MONTH} = 1 ]; then START_MONTH="0$START_MONTH"; fi

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    2
    Perfect, just what I needed, many thanks.

Posting Permissions

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