Bash 脚本中的字符串连接
我正在编写这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西(当然未经测试):
Something like this (untested, of course):
像这样的东西吗?
Something like this?
=
运算符周围必须始终没有空格:(shell 编程与普通编程的最重要区别是,空格在您意想不到的地方很重要。这是其中之一他们。)
The
=
operator must always be written without spaces around it:(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.)