Results 1 to 8 of 8

Thread: bash script - multiple condition if statement problem

  1. #1
    Join Date
    Jan 2014
    Beans
    22

    bash script - multiple condition if statement problem

    I'm stuck on yet another scripting problem.

    I have files in a folder named DailyDetail<date> and WeeklyDetail<date>. I have a variable that gets assigned the relative file path to those files ($activefile and another that is "base" name of the file ($1) provided as a parameter.

    If the file is the weekly variant I need to do some error checking to make sure the file is populated to the minimum valid file size. I'm not having any luck.

    I've tried lots of variations of the below but the main help I have found on the internet lists && and -a as the two ways of doing multiple condition ifs. These are the only 2 variants that don't immediately throw an error, but they don't actually seem to trigger if the file is less than 24510 bytes either. I know each of the 2 components are correct because they each run correctly when only 1 at a time is tested for.

    I've tried this:
    Code:
            if [[ $1 = "WeeklyDetail" && `wc -c $activefile | cut -f 1 -d ' '` < 24510 ]]; then
                echo "File is $1 and it is too damn small"
            else
                echo "if condition for $1 didn't run properly"
            fi
    and
    Code:
            if [ $1 = "WeeklyDetail" && `wc -c $activefile | cut -f 1 -d ' '` < 24510 ]; then
                echo "File is $1 and it is too damn small"
            else
                echo "if condition for $1 didn't run properly"
            fi

  2. #2
    Join Date
    Apr 2012
    Beans
    7,095

    Re: bash script - multiple condition if statement problem

    I think the issue is that it's doing a lexical (string) comparison

    Code:
    $ if [[ true && 11111 < 2345 ]]; then echo "less than"; else echo "not less than"; fi
    less than
    To do a numeric comparison, use (( )) or -lt

    Code:
    $ if [[ true ]] && (( 11111 < 2345 )); then echo "less than"; else echo "not less than"; fi
    not less than
    
    $ if [ true -a 11111 -lt 2345 ]; then echo "less than"; else echo "not less than"; fi
    not less than
    EDIT: btw have you considered using

    Code:
    stat -c '%s' file
    for the size in bytes (instead of wc + cut)?
    Last edited by steeldriver; August 22nd, 2014 at 07:53 PM.

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: bash script - multiple condition if statement problem

    Quote Originally Posted by steeldriver View Post

    EDIT: btw have you considered using

    Code:
    stat -c '%s' file
    for the size in bytes (instead of wc + cut)?
    +1 to that, avoids reading the file...

    I'm also wondering if the logic couldn't be changed using a 'find' to list only the files above/below the threshold size.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  4. #4
    Join Date
    Apr 2012
    Beans
    7,095

    Re: bash script - multiple condition if statement problem

    ... although I got my knuckles rapped for suggesting the same over on U&L because the %s format specifier is a GNU extension (i.e. not POSIX compliant) apparently

    I can't really justify it, but stylistically if it was me I'd probably be thinking about a case statement for the filename test, with a simple size test inside that i.e. something like

    Code:
    case "$1" in
      "somefile") 
        (( "$(stat -c '%s' "$1")" < 100 )) && echo "too small" || echo "just right"
        ;;
        
      "otherfile")
        echo "do something with $1"
        ;;
       
      *)
        echo "not a file I know about"
        ;;
    esac
    As you say, depending on context a 'find -name "somefile" -size +12345' might work better

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

    Re: bash script - multiple condition if statement problem

    POSIX compliance is overrated. If i was paid top dollar for admining some ancient, obscure unix i'd care but i don't so i don't. Life's too short for strict POSIX compliance
    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

  6. #6
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: bash script - multiple condition if statement problem

    Quote Originally Posted by Vaphell View Post
    POSIX compliance is overrated. If i was paid top dollar for admining some ancient, obscure unix i'd care but i don't so i don't. Life's too short for strict POSIX compliance
    After the grammar nazis, the Posix nazis

    Back to stat, it appears that the BSD stat indeed uses different format specifiers. But then bash may have its built-in implementation of stat, so things get complicated.

    OTOH real Posix scripts are written for sh, not bash, so as soon as you use Bash the Posix argument loses a lot of its validity.

    PS: personally I have more problems with ancient Linux... I sometimes have to write scripts that run on a 2010 RedHat, which appears to have frozen its source in 2005. And the 2005 coreutils are missing many things compared to those of my 2012 Kubuntu.
    Last edited by ofnuts; August 25th, 2014 at 09:59 AM.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

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

    Re: bash script - multiple condition if statement problem

    damn, RedHat sure loves 'stability'. 9 years of progress, bugfixes and goodies. Which version of bash would that be? I'd like to know what features i would miss
    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

  8. #8
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: bash script - multiple condition if statement problem

    I'm away from work so cannot check actual versions. IIRC Bash is mostly OK, it's the "coreutils" that are missing useful stuff, like, from memory, the '--executable' flag in find, or the %m format specifier in stat. So the scripts I write on my PC don't always work on the servers...
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

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
  •