Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Help with if statement in bash

  1. #1
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Help with if statement in bash

    Hi there,

    I'm having trouble using numbers in an if statement in bash...

    Code:
    for (( i=0; i<=3; i++  )) ; do
       for (( j=0; j<=9; j++ )) ; do
    
    #       awk '!/^(0([6-9])+|[1-2]+([0-9])+)h([ |0-9|a-z|A-Z|\-|.])*$/' 00h00_${i}${j}-12-2011 > tmp_file1
    
          if [ ${i} == 0 ] && [ ${j} == 0 ] ; then
             # DO NOTHING
          echo 'a' 
          elif [ ${i} < 1 ] ; then
    
            # k=$(( i + 1 ))
             echo 'k'        
    #          awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*(0([2-9])+|[1-3]+([0-9])+)-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
    
          else
          echo 'b'
          fi
    
       done
    done
    it seams that there is some problem with the elif [ ${i} < 1 ] ; then

    line 11: 1: No such file or directory

    How can I say to bash that if i is less than #one to execute a task?

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Help with if statement in bash

    [ ] uses -lt (less than), -gt (greater than), -eq (equal), -ne (not equal)
    if you want <, > ,== for integer operations you can put your condition in (( ))

    do you really need that double for-loop? for i in {00..39} would do the same, though i see it is used in date and that range doesn't really fit. In case you wanted to get a single digit - ${i:0:1} or ${i:1:1}

    out of curiosity, what is that huge awk expression supposed to do?
    Last edited by Vaphell; December 13th, 2012 at 05:04 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with if statement in bash

    Quote Originally Posted by Vaphell View Post
    [ ] uses -lt (less than), -gt (greater than), -eq (equal), -ne (not equal)
    if you want <, > ,== for integer operations you can put your condition in (( ))

    do you really need that double for-loop? for i in {00..39} would do the same, though i see it is used in date and that range doesn't really fit. In case you wanted to get a single digit - ${i:0:1} or ${i:1:1}

    out of curiosity, what is that huge awk expression supposed to do?
    Thanks... with (()) it works fine =)

    the huge regular expression in awk is used to remove unwanted lines that match certain characteristics (dates) in big txt files....

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Help with if statement in bash

    what about the number range? your loops produce 0-39
    i wouldn't say it fits 00h00_${i}${j}-12-2011
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with if statement in bash

    Quote Originally Posted by Vaphell View Post
    what about the number range? your loops produce 0-39
    Indeed, the 2 loops will slowdown the script... but to be able to remove all the unwanted lines I need to created several different regular expression... that depend from the two digits of the loop separately

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Help with if statement in bash

    you seem to skip 0 and i am not convinced 39 is a proper value either

    wouldn't this be better?
    Code:
    for i in {01..30}
    do
      echo ${i:0:1} ${i:1:1}  # separate digits
      if [ ${i:0:1} = 0 ]
      then
        ...
      else
        ...
      fi
    done
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with if statement in bash

    Quote Originally Posted by Vaphell View Post
    you seem to skip 0 and i am not convinced 39 is a proper value either

    wouldn't this be better?
    Code:
    for i in {01..30}
    do
      echo ${i:0:1} ${i:1:1}  # separate digits
      if [ ${i:0:1} = 0 ]
      then
        ...
      else
        ...
      fi
    done
    Cool... That is a very nice way to do it.... thank you =)

  8. #8
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with if statement in bash

    I have other question if you don't mind....

    This loop needs to be embed in another loop that only contains 4 indices and they are independent... is it possible to make it to run in parallel with bash?

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Help with if statement in bash

    what problem do you want to solve exactly that it requires parallel execution?
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #10
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with if statement in bash

    Quote Originally Posted by Vaphell View Post
    what problem do you want to solve exactly that it requires parallel execution?
    This is the code util now

    Code:
    #!/bin/bash
    
    #for ij in {01..31}; do
    for ij in 01; do
       i=${ij:0:1}; j=${ij:1:1}
    
       awk '!/(0([6-9])+|[1-2]+([0-9])+)h([ |0-9|a-z|A-Z|\-|.])*/' "00h00_${ij}-12-2011" > tmp_file1
    
       if [[ ${i} == 0 ]] && [[ ${j} != 9  ]]; then
    
          k=(${j} + 1); l=(${i} + 1)
          awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*('${i}'(['${k}'-9])+|['${l}'-3]+([0-9])+)-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
          
       elif [[ ${i} == 0 ]] && [[ ${j} == 9  ]]; then
    
          k=(${j} + 1); l=(${i} + 1)
          awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*('${l}'([0-9])+|['${l}'-3]+([0-9])+)-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
    
       elif [[ ${i} != 0 ]] && [[ ${j} != 9  ]]; then
    
          k=(${j} + 1); l=(${i} + 1); m=(${i} - 1)
          awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*('${i}'(['${k}'-9])+|['${l}'-3]+([0-9])+|'${m}'([0-9])+)-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
          
       elif [[ ${i} != 0 ]] && [[ ${j} == 9  ]]; then
    
          k=(${j} + 1); l=(${i} + 1); m=(${i} - 1)
          awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*('${l}'([0-9])+|['${l}'-3]+([0-9])+|'${m}'([0-9])+)-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
    
       elif [[ ${j} == 0  ]]; then
    
          k=(${j} + 1); l=(${i} + 1); m=(${i} - 1)
          awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*('${i}'([0-9])+|['${i}'-3]+(['${l}'-9])+|'${m}'([0-9])+)-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
    
       else
          echo '!!! CAUTION !!! Something doesnt match...'
       fi
    
       mv tmp_file2 "00h00_${ij}-12-2011"
    #   rm tmp_file1
    
    done
    
    
    
    ## FOR 00 h FILES
    # awk '!/^(0([6-9])+|[1-2]+([0-9])+)h([ |0-9|a-z|A-Z|\-|.])*$/' 00h00_01-12-2011 > tmp_file1
    # awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*(0([2-9])+|[1-3]+([0-9])+|0([0-9]))-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
    # awk '!/(06h([1-9])+)([0-9a-zA-Z|\-|.| ])+/' tmp_file2 > tmp_file3
    
    # mv tmp_file3 00h00_01-12-2011
    # rm tmp_file*
    
    ## FOR 06 h FILES
    # awk '!/^(1([2-9])+|[2]+([0-9])+)h([ |0-9|a-z|A-Z|\-|.])*$/' 06h00_01-12-2011 > tmp_file1
    # awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*(0([2-9])+|[1-3]+([0-9])+)-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
    # awk '!/(12h([1-9])+)([0-9a-zA-Z|\-|.| ])+/' tmp_file2 > tmp_file3
    
    # mv tmp_file3 06h00_01-12-2011
    # rm tmp_file*
    
    ## FOR 12 h FILES
    # awk '!/^(1([8-9])+|[2]+([0-9])+)h([ |0-9|a-z|A-Z|\-|.])*$/' 12h00_01-12-2011 > tmp_file1
    # awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*(0([2-9])+|[1-3]+([0-9])+)-([0-9|\-|.| ])*/' tmp_file1 > tmp_file2
    # awk '!/(18h([1-9])+)([0-9a-zA-Z|\-|.| ])+/' tmp_file2 > tmp_file3
    
    # mv tmp_file3 12h00_01-12-2011
    # rm tmp_file*
    
    ## FOR 18 h FILES
    # awk '!/([0-9])+h([0-9)+( )([a-zA-Z0-9])+( )*(0([2-9])+|[1-3]+([0-9])+)-([0-9|\-|.| ])*/' 18h00_01-12-2011 > tmp_file3
    
    # mv tmp_file3 18h00_01-12-2011

    I have to repeat this for cycle for other files that start with 06*, 12* and 18*.... the parallel programming it was just if it was feasible with "low end" in bash....

    However this script does not work =( the variables i j k l or m are not going to may awk code has numbers.... =( any suggestion?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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