Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help with Shell

  • 28-06-2011 12:26pm
    #1
    Registered Users, Registered Users 2 Posts: 1,920 ✭✭✭


    Hi all,

    I'm a bit of a shell noob trying to write a little script.

    I have a piece of SQL that gets the average time difference between 2 MySQL timestamps in a table.

    My Shell outputs the result to a file and then I read the result from the file into the a variable.

    Then I'm effectively want to say, if the variable less than or equal to 2, then output a message to that effect. Else, send a warning email.

    However, there seems to be some issue with Shell dealing with decimals. Anybody have a solution for me. Here is the portion of my if statement:
    export p=`cat /usr/local/tomcat/AvgMsgSendTime.out.$dt.$1 | tail -1`
    echo $p
    
    if [ $p < 2.00 ]
    then
    echo "Average Message sending time is less than 2 seconds."
    else
    mail -s "Average Message sending time greater than 2 seconds" glumsden@saadian.com < "/usr/local/tomcat/AvgMsgSendTime.out."$dt.$1
    echo "Average Message sending time greater than 2 seconds"
    echo `cat /usr/local/tomcat/AvgMsgSendTime.out.$dt.$1`
    fi
    


Comments

  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    yeah I don't think shell script handles floats very well.
    one work around I've seen is to pipe the calc to the bc calculator.

    Try...
    export p=`cat /usr/local/tomcat/AvgMsgSendTime.out.$dt.$1 | tail -1`
    echo $p
    ans=`echo "$p < 2.00" | bc`
    
    if [ $ans ]
    then
    echo "Average Message sending time is less than 2 seconds."
    else
    mail -s "Average Message sending time greater than 2 seconds" glumsden@saadian.com < "/usr/local/tomcat/AvgMsgSendTime.out."$dt.$1
    echo "Average Message sending time greater than 2 seconds"
    echo `cat /usr/local/tomcat/AvgMsgSendTime.out.$dt.$1`
    fi
    


  • Registered Users, Registered Users 2 Posts: 1,920 ✭✭✭GavMan


    Croo,

    Can you explain whats happening here:
    ans=`echo "$p < 2.00" | bc`
    
    if [ $ans ]
    

    from my limited knowledge thats just outputting the result of p < 2.00. Then its just saying
    if [ans]
    then do something

    there seems to be no logic to that if statement


  • Registered Users, Registered Users 2 Posts: 1,920 ✭✭✭GavMan


    Croo,

    Can you explain whats happening here:
    ans=`echo "$p < 2.00" | bc`
    
    if [ $ans ]
    

    from my limited knowledge thats just outputting the result of p < 2.00. Then its just saying
    if [ans]
    then do something

    there seems to be no logic to that if statement


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    writing shells scripts is often a question of how you use the considerable number of utilities that constitute the unix/liunux OS... just like you did with cat & tail -1 to extract the last time from the log file!

    In this case we use echo & bc...
    we use echo to create our calculation and then pipe that through to bc which does the calculation.

    In this case the answer to, is x < y, is true or false ... 1 or 0 is how this is represented here; 1= true, 0=false.

    you should read "[ $ans ] " as, if ans is true then.
    ans will be either 1 or 0 and you could check that too, i.e. [ $ans = 1 ] or [ $ans = 0 ], but the shell will evaluate 1 as true and 0 or false so why bother.

    I think you might have missed the | bc bit?

    It does work doesn't it?


  • Registered Users, Registered Users 2 Posts: 1,920 ✭✭✭GavMan


    Thanks croo.

    As I say I'm a real noob with shell. I didn't get thats what ans was.

    I do believe it works. We're testing it out a good bit but so far so good


  • Advertisement
  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    Hmmm... seems bash doesn't evaluate 1 & 0 as true & false which is a real surprise!

    I found this little script to test evaluations and the results are very surprising - to me anyway. But it's years since I did any serious scripting and then it was under unix with the korn shell.
    #!/bin/bash
    
    #  Tip:
    #  If you're unsure of how a certain condition would evaluate,
    #+ test it in an if-test.
    
    echo
    
    echo "Testing \"0\""
    if [ 0 ]      # zero
    then
      echo "0 is true."
    else          # Or else ...
      echo "0 is false."
    fi            # 0 is true.
    
    echo
    
    echo "Testing \"1\""
    if [ 1 ]      # one
    then
      echo "1 is true."
    else
      echo "1 is false."
    fi            # 1 is true.
    
    echo
    
    echo "Testing \"-1\""
    if [ -1 ]     # minus one
    then
      echo "-1 is true."
    else
      echo "-1 is false."
    fi            # -1 is true.
    
    echo
    
    echo "Testing \"NULL\""
    if [ ]        # NULL (empty condition)
    then
      echo "NULL is true."
    else
      echo "NULL is false."
    fi            # NULL is false.
    
    echo
    
    echo "Testing \"xyz\""
    if [ xyz ]    # string
    then
      echo "Random string is true."
    else
      echo "Random string is false."
    fi            # Random string is true.
    
    echo
    
    echo "Testing \"\$xyz\""
    if [ $xyz ]   # Tests if $xyz is null, but...
                  # it's only an uninitialized variable.
    then
      echo "Uninitialized variable is true."
    else
      echo "Uninitialized variable is false."
    fi            # Uninitialized variable is false.
    
    echo
    
    echo "Testing \"-n \$xyz\""
    if [ -n "$xyz" ]            # More pedantically correct.
    then
      echo "Uninitialized variable is true."
    else
      echo "Uninitialized variable is false."
    fi            # Uninitialized variable is false.
    
    echo
    
    
    xyz=          # Initialized, but set to null value.
    
    echo "Testing \"-n \$xyz\""
    if [ -n "$xyz" ]
    then
      echo "Null variable is true."
    else
      echo "Null variable is false."
    fi            # Null variable is false.
    
    
    echo
    
    
    # When is "false" true?
    
    echo "Testing \"false\""
    if [ "false" ]              #  It seems that "false" is just a string.
    then
      echo "\"false\" is true." #+ and it tests true.
    else
      echo "\"false\" is false."
    fi            # "false" is true.
    
    echo
    
    echo "Testing \"\$false\""  # Again, uninitialized variable.
    if [ "$false" ]
    then
      echo "\"\$false\" is true."
    else
      echo "\"\$false\" is false."
    fi            # "$false" is false.
                  # Now, we get the expected result.
    
    #  What would happen if we tested the uninitialized variable "$true"?
    
    echo
    
    exit 0
    

    So, anyway, it seems with bash we need to use [ $ans = 1 ]

    By the way, I used the $ans to make it easier to read/understand but you can drop it and use the the check directly
    if [ 1 = `echo "$p < 2.00" | bc` ]
    


  • Registered Users, Registered Users 2 Posts: 1,920 ✭✭✭GavMan


    Thanks croo.

    I noticed it wasn't quite right later in the day when we looked at our result files from our testing.
    if [ 1 = `echo "$p < 2.00" | bc` ]
    

    The above code works a treat so far


  • Registered Users, Registered Users 2 Posts: 1,920 ✭✭✭GavMan


    Thanks croo.

    I noticed it wasn't quite right later in the day when we looked at our result files from our testing.
    if [ 1 = `echo "$p < 2.00" | bc` ]
    

    The above code works a treat so far


Advertisement