循环结束后循环时如何将输入输入到bash中并保留变量

发布于 2025-02-04 09:47:37 字数 463 浏览 4 评论 0原文

BASH允许使用:cat<(echo“ $ filecontent”)

bash还允许使用:读取i时;做回声$ i;完成</etc/passwd

要结合上两个可以使用:echo $ filecontent |读书时;做回声$ i;完成

最后一个问题是它创建子壳,在while loop结束后变量i无法再访问。

我的问题是:

如何实现这样的事情:<代码>阅读i;做回声$ i;完成&lt;(echo“ $ filecontent”)或换句话说:我如何确定i在循环时生存?

请注意,我知道在语句中添加到{}中时封闭,但这不能解决问题(想象一下您想在函数中使用wire loop并返回i变量)

Bash allows to use: cat <(echo "$FILECONTENT")

Bash also allow to use: while read i; do echo $i; done </etc/passwd

to combine previous two this can be used: echo $FILECONTENT | while read i; do echo $i; done

The problem with last one is that it creates sub-shell and after the while loop ends variable i cannot be accessed any more.

My question is:

How to achieve something like this: while read i; do echo $i; done <(echo "$FILECONTENT") or in other words: How can I be sure that i survives while loop?

Please note that I am aware of enclosing while statement into {} but this does not solves the problem (imagine that you want use the while loop in function and return i variable)

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

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

发布评论

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

评论(5

笑咖 2025-02-11 09:47:41

最快的是:

while read -r ITEM; do
    OPERATION
done < <(COMMAND)

或一行:

while read -r ITEM; do OPERATION; done < <(COMMAND)

The fastest was:

while read -r ITEM; do
    OPERATION
done < <(COMMAND)

Or in one line:

while read -r ITEM; do OPERATION; done < <(COMMAND)
绳情 2025-02-11 09:47:41

Bash提供了VAR默认行为,因此:

while read -r line
do
  total=${total:-0}
  total=$((total + $(calculation code)))
done
echo $total

这允许循环轻松捕获脚本和功能的stdin。

Bash provides var default behavior so:

while read -r line
do
  total=${total:-0}
  total=$((total + $(calculation code)))
done
echo $total

This allows the loop to easily capture stdin, for scripts and functions.

惜醉颜 2025-02-11 09:47:40

“ http://www.gnu.org/software/software/bash/manual/manual/manual/bash.html#process-substitution”的正确符号

while read i; do echo $i; done < <(echo "$FILECONTENT")

i 在循环终止时可用。
另一种选择是:

echo $FILECONTENT | 
{
while read i; do echo $i; done
...do other things using $i here...
}

牙套是I/O分组操作,并且本身不会创建子壳。在这种情况下,它们是管道的一部分,因此作为子壳运行,但这是因为|而不是{...}。您在问题中提到了这一点。 afaik,您可以在功能内部从这些内部进行返回。


BASH还提供 shopt shopt < < /a>内置及其众多选择之一是:

LastPipe

如果设置,而作业控制不活动,则壳运行的是在当前Shell环境中未执行的管道的最后一个命令。

因此,在脚本中使用类似的内容在循环后可用的sum

FILECONTENT="12 Name
13 Number
14 Information"
shopt -s lastpipe   # Comment this out to see the alternative behaviour
sum=0
echo "$FILECONTENT" |
while read number name; do ((sum+=$number)); done
echo $sum

在命令行中进行此操作通常会犯规“工作控制不活动”(也就是说,在命令行中,作业控件处于活动状态)。在不使用脚本失败的情况下测试此操作。

另外,如 Gareth Rees 在他的答案,有时可以使用在这里字符串

while read i; do echo $i; done <<< "$FILECONTENT"

这不需要shopt;您可能可以使用它保存一个过程。

The correct notation for Process Substitution is:

while read i; do echo $i; done < <(echo "$FILECONTENT")

The last value of i assigned in the loop is then available when the loop terminates.
An alternative is:

echo $FILECONTENT | 
{
while read i; do echo $i; done
...do other things using $i here...
}

The braces are an I/O grouping operation and do not themselves create a subshell. In this context, they are part of a pipeline and are therefore run as a subshell, but it is because of the |, not the { ... }. You mention this in the question. AFAIK, you can do a return from within these inside a function.


Bash also provides the shopt builtin and one of its many options is:

lastpipe

If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.

Thus, using something like this in a script makes the modfied sum available after the loop:

FILECONTENT="12 Name
13 Number
14 Information"
shopt -s lastpipe   # Comment this out to see the alternative behaviour
sum=0
echo "$FILECONTENT" |
while read number name; do ((sum+=$number)); done
echo $sum

Doing this at the command line usually runs foul of 'job control is not active' (that is, at the command line, job control is active). Testing this without using a script failed.

Also, as noted by Gareth Rees in his answer, you can sometimes use a here string:

while read i; do echo $i; done <<< "$FILECONTENT"

This doesn't require shopt; you may be able to save a process using it.

挽清梦 2025-02-11 09:47:40

jonathan leffler解释如何使用 process替换 ,但另一种可能性是使用

while read i; do echo "$i"; done <<<"$FILECONTENT"

Jonathan Leffler explains how to do what you want using process substitution, but another possibility is to use a here string:

while read i; do echo "$i"; done <<<"$FILECONTENT"

This saves a process.

深居我梦 2025-02-11 09:47:40

此功能使重复$ num times jpg文件(bash)

function makeDups() {
NUM=$1
echo "Making $1 duplicates for $(ls -1 *.jpg|wc -l) files"
ls -1 *.jpg|sort|while read f
do
  COUNT=0
  while [ "$COUNT" -le "$NUM" ]
  do
    cp $f ${f//sm/${COUNT}sm}
    ((COUNT++))
  done
done
}

This function makes duplicates $NUM times of jpg files (bash)

function makeDups() {
NUM=$1
echo "Making $1 duplicates for $(ls -1 *.jpg|wc -l) files"
ls -1 *.jpg|sort|while read f
do
  COUNT=0
  while [ "$COUNT" -le "$NUM" ]
  do
    cp $f ${f//sm/${COUNT}sm}
    ((COUNT++))
  done
done
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文