String concatenation in Bash script

后端 未结 3 1386
灰色年华
灰色年华 2021-01-23 18:18

I am writing this Bash script:

count=0   
result

for d in `ls -1 $IMAGE_DIR | egrep \"jpg$\"`
do

    if (( (count % 4) == 0 )); then
                result=\"a         


        
相关标签:
3条回答
  • 2021-01-23 19:00

    The = operator must always be written without spaces around it:

    result="$result $d"
    

    (Pretty much the most important difference in shell programming to normal programming is that whitespace matters in places where you wouldn't expect it. This is one of them.)

    0 讨论(0)
  • 2021-01-23 19:12

    Something like this?

    count=0   
    
    find $IMAGE_DIR -name "*.jpg" |
    while read f; do
            if (( (count % 4) == 0 )); then
                    result="abc $f"
    
                    if (( count > 0 )); then
                            echo $result
                    fi
    
            else
                    result="$result $d"
            fi
    
            (( count++ ))
    done
    
    0 讨论(0)
  • 2021-01-23 19:16

    Something like this (untested, of course):

    count=0 result=
    
    for d in "$IMAGE_DIR"/*jpg; do
       (( ++count % 4 == 0 )) &&
         result="abc $d"
       (( count > 0 )) &&
         printf '%s\n' "$result" ||
          result+=$d
    done
    
    0 讨论(0)
提交回复
热议问题