Bash Script If THEN Loop

File"LstTmp1"
account,subID,.......
disney01,2c97927269c72f2c0169e2e9fb216f00,/disney01-test/tdsna/inbox,true
disney01,2c97927269c72f2c0169e2ea258a6f15,/disney01-test/tdseur/inbox,true
disney02,2c97927269a0039e0169a02628f32de4,/disney02-test/inbox,true
etc..
File"SubsConfig"
disney01,2c97927269c72f2c0169e2e9fb216f00,/disney01-test/tdsna/inbox,true
disney01,2c97927269c72f2c0169e2ea258a6f15,/disney01-test/tdseur/inbox,true
disney02,2c97927269a0039e0169a02628f32de4,/disney02-test/inbox,true
etc..
File"TransConfig"
2c97927269c72f2c0169e2e9fb216f00,2c97927269c72f2c0169e2ea06756f06,OUT,undefined
2c97927269c72f2c0169e2e9fb216f00,2c97927269c72f2c0169e2ea06796f07,IN,ArchiveCompleted-peter01-tdsna
etc.....
exec 5<${LstTmp1}
while read -u5 line
do
    accID='echo '$line' | cut -d ',' -f 1'
    subID='echo '$line' | cut -d ',' -f 2'
    LstTmp2=`cat ${TransConfig} | grep ${subID} | grep "OUT" | cut -d ',' -f 4`
    LstTmp3=`cat ${SubsConfig} | grep ${subID} | grep -vE "outbox|Arch" | cut -d ',' -f 4`

    if [${LstTmp2} == 'undefined' && ${LstTmp3} == 'true'] || [${LstTmp2} == 'undefined' && ${LstTmp3} == 'undefined'] || [ ${LstTmp2} != 'undefined' &&  ${LstTmp3} == 'undefined']; then
    echo ${accID} > ${SkipList}.tmp
fi

I am trying script to filter out the following

TransConfig and SubsConfig contains attributes of the accounts

Finally only accID will be write to tmp files

Error Messages

grep: invalid argument ‘,’ for ‘--directories’
Valid arguments are:
  - ‘read’
  - ‘recurse’
  - ‘skip’
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
grep: invalid argument ‘,’ for ‘--directories’
Valid arguments are:
  - ‘read’
  - ‘recurse’
  - ‘skip’
Usage: grep [OPTION]... PATTERN [FILE]...

Is if statement got any problem?

1 answer

  • answered 2020-09-28 11:05 Léa Gris

    Fixed your script, but without reproducible example and without sample data it cannot be warranted it does what it its intended to do.

    #!/usr/bin/env bash
    
    LstTmp1='unassigned'
    TransConfig='unassigned'
    SubsConfig='unassigned'
    SkipList='unassigned'
    
    exec 5<"$LstTmp1"
    while IFS=',' read -r -u5 accID subID _; do
      LstTmp2=$(grep "$subID" <"$TransConfig" | grep "OUT" | cut -d ',' -f 4)
      LstTmp3=$(grep "$accID" <"$SubsConfig" | grep -vE "outbox|Arch" | cut -d ',' -f 4)
    
      if [ "$LstTmp2" = 'undefined' ] && [ "$LstTmp3" = 'true' ] \
        || [ "$LstTmp2" = 'undefined' ] && [ "$LstTmp3" = 'undefined' ] \
        || [ "$LstTmp2" != 'undefined' ] && [ "$LstTmp3" = 'undefined' ]; then
        echo "$accID" >>"$SkipList.tmp"
      fi
    done
    

    The if condition can, and should be factored:

    Lets define:

    • "$LstTmp2" = 'undefined' as condition A
    • "$LstTmp3" = 'true' as condition B
    • "$LstTmp3" = 'undefined' as condition C
    • [ "$LstTmp2" != 'undefined' ] is condition !A

    What you actually wrote is:

    A && B || A && C || !A && C

    Which translates into boolean arithmetic:

    A × B + A × C + !A × C

    A × C + !A × C is factored to C as: C × ( A + !A ) and since ( A + !A ) cancels itself, there is only C left to represent the whole A × C + !A × C.

    Remaining totally factored expression: A × B + C

    Which translates back into shell expression:

    {
      [ "$LstTmp2" = 'undefined' ] && [ "$LstTmp3" = 'true' ]
    } || [ "$LstTmp3" = 'undefined' ]
    

    ( thank to KamilCuc ) Which can be shortened since bash evaluates expressions from left to right, as:

    if [ "$LstTmp3" = 'undefined' ] ||
       [ "$LstTmp2" = 'undefined' ] && [ "$LstTmp3" = 'true' ]; then
      echo "$accID" >>"$SkipList.tmp"
    fi
    

    If it is not the intended logic, then it needs be fixed as well.