在 bash 脚本循环中打印 cat 语句的输出
我正在尝试为来自 cat 命令的每一行执行命令。我基于从供应商那里获得的示例代码。
这是脚本:
for tbl in 'cat /tmp/tables'
do
echo $tbl
done
所以我期望输出是文件中的每一行。相反,我得到的是:
cat
/tmp/tables
这显然不是我想要的。
我将用与数据库交互的实际命令替换 echo。
任何帮助解决这个问题的帮助将不胜感激。
I'm trying to execute a command for each line coming from a cat command. I'm basing this on sample code I got from a vendor.
Here's the script:
for tbl in 'cat /tmp/tables'
do
echo $tbl
done
So I was expecting the output to be each line in the file. Instead I'm getting this:
cat
/tmp/tables
That's obviously not what I wanted.
I'm going to replace the echo with an actual command that interfaces with a database.
Any help in straightening this out would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您使用了错误的引号类型。
您需要使用反引号而不是单引号来使参数成为一个正在运行的程序并将内容输出到 for 循环。
另外,为了更好的可读性(如果您使用的是 bash),您可以将其写为
如果您的期望是获取每一行(上面的 for 循环将为您提供每个单词),那么您最好使用 xargs< /code>,像这样
或作为循环
You are using the wrong type of quotes.
You need to use the back-quotes rather than the single quote to make the argument being a program running and piping out the content to the forloop.
Also for better readability (if you are using bash), you can write it as
If your expectations are to get each line (The for-loops above will give you each word), then you may be better off using
xargs
, like thisor as a loop
单引号应该是反引号:
尽管如此,这不会让您按行输出/输入,而是按单词输出/输入。要逐行处理,您应该尝试以下操作:
The single quotes should be backticks:
Although, this will not get you output/input by line, but by word. To process line by line, you should try something like:
使用 while 循环:
With while loop:
阅读此。
read this.
例如,您可以通过重新定义 IFS(输入字段分隔符)在 bash 中进行大量解析
You can do a lot of parsing in bash by redefining the IFS (Input Field Seperator), for example