Bash 脚本中的字符串连接

发布于 2024-12-12 09:06:17 字数 726 浏览 0 评论 0原文

我正在编写这个 Bash 脚本:

count=0   
result

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

    if (( (count % 4) == 0 )); then
                result="abc $d"

                if (( count > 0 )); then
                    echo "$result;"
                fi

        else
            result="$result $d"
        fi

        (( count++ ))

done

if (( (count % 4) == 0 )); then
    echo $result
fi

该脚本的作用是将部分字符串连接成一个字符串,当值除以 4 并且它应该大于 0 时。

在 IMAGE_DIR 中,我有 8 个图像,

我得到如下输出:

abc et004.jpg
abc et008.jpg

但我会期望:

abc et001.jpg et002.jpg et003.jpg et004.jpg;
abc et005.jpg et006.jpg et007.jpg et008.jpg;

我该如何解决这个问题?

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="abc $d"

                if (( count > 0 )); then
                    echo "$result;"
                fi

        else
            result="$result $d"
        fi

        (( count++ ))

done

if (( (count % 4) == 0 )); then
    echo $result
fi

The script is to concate part strings into a string when the value is divided by 4 and it should be larger than 0.

In the IMAGE_DIR, I have 8 images,

I got outputs like this:

abc et004.jpg
abc et008.jpg

But I would expect to have:

abc et001.jpg et002.jpg et003.jpg et004.jpg;
abc et005.jpg et006.jpg et007.jpg et008.jpg;

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖树树初阳… 2024-12-19 09:06:23

像这样的东西(当然未经测试):

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

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
仙气飘飘 2024-12-19 09:06:22

像这样的东西吗?

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

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
野生奥特曼 2024-12-19 09:06:21

= 运算符周围必须始终没有空格:

result="$result $d"

(shell 编程与普通编程的最重要区别是,空格在您意想不到的地方很重要。这是其中之一他们。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文