Shell Script awk problem , please advice

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DannyMc
    New Member
    • Aug 2007
    • 20

    Shell Script awk problem , please advice

    Hi, i am currently writing a script to delete the directory named with number.

    Example:
    in /usr/
    i have
    /usr/101
    /usr/102 ...... /usr/200

    This is the command i type and it works, i got the directory listed where the number is <150

    Code:
    ls -l /usr/ | grep ^d | awk '{ if ( $9 < 150) print $9 }'
    So i applied it to the script, and it is not working especially at awk if else comparison, screen printed ALL number (101 -200). I am suspecting the error is in awk command there. Please help.

    Code:
    #!/bin/bash -xv
    COPYKEEP="10"
    LATEST="200"
    KEEPFROM=`expr $LATEST - $COPYKEEP`
    
    if [ $KEEPFROM -gt 0 ]
    then
    
    # Find which to delete.
    ls -l /usr/ | grep ^d |  awk '{ if ( $9 < $KEEPFROM ) print $9 }'
    fi
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Code:
    #!/bin/bash -xv
    COPYKEEP="10"
    LATEST="200"
    KEEPFROM=`expr $LATEST - $COPYKEEP`
    echo $KEEPFROM
    if [ $KEEPFROM -gt 0 ]
    then
    # Find which to delete.
    ls -l /usr/ | grep ^d |  awk -v p=$KEEPFROM '{ if ( $9 < p ) print $9 }'
    fi
    check the above script...
    you can not use shell variables in awk directly..
    it needs to be assigned to awk variable and then use it...
    in Example we'r assigning it to 'p'

    Comment

    • DannyMc
      New Member
      • Aug 2007
      • 20

      #3
      It works!!

      Thank you so much!

      Comment

      • DannyMc
        New Member
        • Aug 2007
        • 20

        #4
        Hi again,

        i have tried to delete the file in awk using method found via google.

        Code:
        ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }'
        This is working , however when i apply this to the script

        Code:
        echo " ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }' "
        The quoting is very confusing, can anyone help?

        Comment

        • ashitpro
          Recognized Expert Contributor
          • Aug 2007
          • 542

          #5
          Originally posted by DannyMc
          Hi again,

          i have tried to delete the file in awk using method found via google.

          Code:
          ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }'
          This is working , however when i apply this to the script

          Code:
          echo " ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }' "
          The quoting is very confusing, can anyone help?


          I didn't need any complex quoting...
          Next statement worked for me in shell script as well as on command line...

          ls -l /usr/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf /usr/" $9) }'

          Comment

          • DannyMc
            New Member
            • Aug 2007
            • 20

            #6
            Again, Thank you. It works :)

            Comment

            • ghostdog74
              Recognized Expert Contributor
              • Apr 2006
              • 511

              #7
              there's actually another less cumbersome way to do it.
              Code:
              rm -rf /usr/[0-1][0-9][0-9]
              rm -rf /usr/200

              Comment

              Working...