将命令生成的每行作为脚本中的一行

发布于 2025-01-23 22:21:03 字数 283 浏览 1 评论 0原文

我有一个简单的脚本:

#!/bin/bash

dates_and_PID=$(ps -eo lstart,pid)

echo ${dates_and_PID::24}

我希望每行都在第24个字符上切割。但是,它将变量dates_and_pid视为一行,因此我只有生成的一行。而我希望每行将其切割。

我正在练习,但最终目标是让脚本从2020年11月11日星期一更改为11/11/20

I have this simple script :

#!/bin/bash

dates_and_PID=$(ps -eo lstart,pid)

echo ${dates_and_PID::24}

And I would like each line to be cut at the 24th character. Nevertheless, it considers the variable dates_and_PID as a single line, so I only have one line that is generated. Whereas I would like it to be cut for each line.

I am practicing but the final goal would be to have the script change the dates from Mon Nov 11 2020 to 11/11/20.

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

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

发布评论

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

评论(2

乙白 2025-01-30 22:21:03

您需要逐行迭代输入,而不是将所有内容读取为单个变量。这样的事情应该解决问题:

#!/bin/bash

while read line; do
  echo "${line::24}"
done < <(ps -eo lstart,pid)

You need to iterate over the input line by line, instead of reading everything into a single variable. Something like this should do the trick:

#!/bin/bash

while read line; do
  echo "${line::24}"
done < <(ps -eo lstart,pid)
甜味超标? 2025-01-30 22:21:03

您可以将stdout送到循环:

ps -eo lstart,pid | while read line; do
  echo "${line::24}"
done

You can pipe stdout to while loop:

ps -eo lstart,pid | while read line; do
  echo "${line::24}"
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文