Shell 脚本中的 For 循环 - 在 csv 文件中添加断线

发布于 2024-07-14 11:50:20 字数 601 浏览 7 评论 0原文

我正在尝试在 shell 脚本中使用 for 循环。

我正在从文本文件执行命令。 我希望执行每个命令 10 次并将一些统计信息插入到 csv 文件中。 完成该命令后,我想开始下一个命令,但在执行了 10 次的第一个命令之后在 CSV 文件中添加一个换行符。

以下是否正确:

#Function processLine
processLine(){
line="$@"
for i in 1 2 3 4 5 6 7 8 9 10
do
START=$(date +%s.%N)
echo "$line"
eval $line > /dev/null 2>&1

END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)

echo "$line, $START, $END, $DIFF" >> file.csv 2>&1
echo "It took $DIFF seconds"
echo $line
done
}

感谢大家的帮助

UPDATE

它正在正确执行循环,但我无法让它在每个命令执行 10 次后添加换行符。

I'm trying to use a for loop in a shell script.

I am executing a command from a text file. I wish to execute each command 10 times and insert some stats into a csv file. After that command has been done, I want to start the next BUT put a line break in the CSV file after the first command that was done 10 times.

Is the following correct:

#Function processLine
processLine(){
line="$@"
for i in 1 2 3 4 5 6 7 8 9 10
do
START=$(date +%s.%N)
echo "$line"
eval $line > /dev/null 2>&1

END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)

echo "$line, $START, $END, $DIFF" >> file.csv 2>&1
echo "It took $DIFF seconds"
echo $line
done
}

Thanks all for any help

UPDATE

It is doing the loop correctly, but I can't get it to add a line break after each command is executed 10 times.

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

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

发布评论

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

评论(3

治碍 2024-07-21 11:50:21
processLine()
{
    line="$@"

    echo $line >> test_file
    for ((i = 1; i <= 10 ; i++))
    do
        # do not move to the next line
        echo -n "$i," >> test_file
    done

    # move to the next line: add the break
    echo >> test_file
}

echo -n > test_file

processLine 'ls'
processLine 'find . -name "*"'
processLine()
{
    line="$@"

    echo $line >> test_file
    for ((i = 1; i <= 10 ; i++))
    do
        # do not move to the next line
        echo -n "$i," >> test_file
    done

    # move to the next line: add the break
    echo >> test_file
}

echo -n > test_file

processLine 'ls'
processLine 'find . -name "*"'
执笏见 2024-07-21 11:50:21

done之后添加一行“echo >> file.csv”怎么样? 或者您只想在每个 10 块之间有一个空行? 然后您可以执行以下操作:

FIRST=1
processline()
{
  if (( FIRST )) then
    FIRST = 0
  else
    echo >> file.csv
  fi
  ...rest of code...
}

否则您可能想要给出所需输出和您现在获得的输出的示例。

How about just adding a line "echo >> file.csv" after done? Or do you only want an empty line between each block of 10? Then you could do the following:

FIRST=1
processline()
{
  if (( FIRST )) then
    FIRST = 0
  else
    echo >> file.csv
  fi
  ...rest of code...
}

Otherwise you might want to give an example of the desired output and the output you are getting now.

厌味 2024-07-21 11:50:21

看起来很合理。 它做你想做的事吗?

您可以简化一些事情,例如,

DIFF=$(echo "$END - $START" | bc)

如果 END 和 START 是整数,则可以简化

DIFF=$((END - START))

,并且如果您只想使用它们一次,则无需将它们放入变量中。

如果它没有做您想要的事情,请编辑问题来描述问题(您看到它在做什么以及您希望它做什么)。

It looks reasonable. Does it do what you want it to?

You could simplify some things, e.g.

DIFF=$(echo "$END - $START" | bc)

could be just

DIFF=$((END - START))

if END and START are integers, and there's no need to put things in variables if you're only going to use them once.

If it's not doing what you want, edit the question to describe the problem (what you see it doing and what you'd rather have it do).

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