Results 1 to 9 of 9

Thread: Bash question

  1. #1
    Join Date
    Apr 2014
    Location
    "Great North Wet"
    Posts
    183

    Default Bash question

    I'm writing a bash script that runs once a month to use smartctl to check my drives.
    I want to wait for it to complete the tests before the script ends so I've got this:
    Note: ct and drives are set correctly so don't worry about those

    Code:
      echo "Waiting for all tests to complete"
      keyword="progress"
      for((i=0; i<ct; i++)) 
      do
         pg=$(smartctl -a /dev/${drives[i]} | grep -i -A 1 "progress")
         while [[ "$pg" == "*${keyword}*" ]]
         do
             sleep 120
         done
      done
    But it doesn't wait. What am I doing wrong?
    TIA
    Turkey Creek Jack Johnson: Nobody move!
    Doc Holliday: Nonsense. By all means, move.

  2. #2
    Join Date
    Sep 2012
    Posts
    6,256

    Default Re: Bash question

    Quote Originally Posted by erbenton View Post
    Code:
         while [[ "$pg" == "*${keyword}*" ]]
         do
             sleep 120
         done
    Neither variable is changed inside of loop, so if loop condition will ever be true once it will be true forever and loop never terminates.
    But it doesn't wait. What am I doing wrong?
    How should we know? Add debug print to examine actual value of variables or run script using "set -x" to trace its execution.

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    28,058

    Thumbs down Re: Bash question

    I am not well known with smartctl, but I see that you grep explicit ignoring case. And then you check with something all lower case. Thus "Progress" will pass the grep, but not detected on your comparison.

    I would try probably different (assuming that your smartctl command outputs the string "progress" (case insensitive or not)).
    Code:
    do
             while true
             do 
                    if smartctl -a /dev/${drives[i]} | grep -i -q "progress" 
                    then
                            sleep 120
                    else
                            break
                    fi
            done
    done
    I did not test it, but it would be my starting point.
    Henk van Velden

  4. #4
    Join Date
    Apr 2014
    Location
    "Great North Wet"
    Posts
    183

    Default Re: Bash question

    DOH!
    Thanks!
    Turkey Creek Jack Johnson: Nobody move!
    Doc Holliday: Nonsense. By all means, move.

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    28,058

    Default Re: Bash question

    And I will move it to Programming/Scripting. The scriptkiddies will be there
    Henk van Velden

  6. #6
    Join Date
    Apr 2014
    Location
    "Great North Wet"
    Posts
    183

    Default Re: Bash question

    Ack, it still doesn't wait, it never enters the while loop
    Code:
    # wait for all tests to complete
    echo "Waiting for all tests to complete"
    keyword="progress"
    for((i=0; i<ct; i++))
    do
    echo "ct: $ct, i $i"                                                                                                                                                                     
       pg=$(smartctl -a /dev/${drives[i]} | grep -i -A 1 "progress")
    echo "pg: $pg"
       while [[ "$pg" == "*${keyword}*" ]]
       do
    echo "pg in top while loop $pg"
           sleep 120
           pg=$(smartctl -a /dev/${drives[i]} | grep -i -A 1 "progress")
    echo "pg bottom of while loop: $pg"
       done
    done
    echo ""
    From cmd line when script exits:
    Code:
    # smartctl -a /dev/sda |grep -i -A 1 "progress"
    Self-test execution status:      ( 249) Self-test routine in progress...
                                            90% of test remaining.
    My first echo shows this:
    Code:
    pg: Self-test execution status:      ( 249)     Self-test routine in progress...
                                            90% of test remaining.
    Turkey Creek Jack Johnson: Nobody move!
    Doc Holliday: Nonsense. By all means, move.

  7. #7
    Join Date
    Apr 2014
    Location
    "Great North Wet"
    Posts
    183

    Default Re: Bash question

    Henk van Velden: i decided to use your method and it seems to work
    Thanks!
    Turkey Creek Jack Johnson: Nobody move!
    Doc Holliday: Nonsense. By all means, move.

  8. #8
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    28,058

    Default Re: Bash question

    You are welcome.

    I am glad that my quicky indeed works a bit like it should.
    Henk van Velden

  9. #9
    Join Date
    Sep 2012
    Posts
    6,256

    Default Re: Bash question

    Quote Originally Posted by erbenton View Post
    Code:
    keyword="progress"
       while [[ "$pg" == "*${keyword}*" ]]
    To be interpreted as pattern meta-characters must not be quoted. Your condition compares value of $pg with literal string *progress*, not with the pattern *progress*. The following would work
    Code:
    while [[ "$pg" == *"${keyword}"* ]]
    Same difference as in
    Code:
    ls *foo*
    vs
    Code:
    ls "*foo*"

Tags for this Thread

Posting Permissions

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