在 bash 脚本循环中打印 cat 语句的输出

发布于 2024-11-26 22:13:33 字数 292 浏览 2 评论 0原文

我正在尝试为来自 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 技术交流群。

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

发布评论

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

评论(5

人间不值得 2024-12-03 22:13:33

您使用了错误的引号类型。

您需要使用反引号而不是单引号来使参数成为一个正在运行的程序并将内容输出到 for 循环。

for tbl in `cat /tmp/tables` 
do 
    echo "$tbl"
done

另外,为了更好的可读性(如果您使用的是 bash),您可以将其写为

for tbl in $(cat /tmp/tables) 
do 
    echo "$tbl"
done

如果您的期望是获取每一行(上面的 for 循环将为您提供每个单词),那么您最好使用 xargs< /code>,像这样

cat /tmp/tables | xargs -L1 echo

或作为循环

cat /tmp/tables | while read line; do echo "$line"; done

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.

for tbl in `cat /tmp/tables` 
do 
    echo "$tbl"
done

Also for better readability (if you are using bash), you can write it as

for tbl in $(cat /tmp/tables) 
do 
    echo "$tbl"
done

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 this

cat /tmp/tables | xargs -L1 echo

or as a loop

cat /tmp/tables | while read line; do echo "$line"; done
贱人配狗天长地久 2024-12-03 22:13:33

单引号应该是反引号:

for tbl in `cat /etc/tables`

尽管如此,这不会让您按行输出/输入,而是按单词输出/输入。要逐行处理,您应该尝试以下操作:

cat /etc/tables | while read line
    echo $line
done

The single quotes should be backticks:

for tbl in `cat /etc/tables`

Although, this will not get you output/input by line, but by word. To process line by line, you should try something like:

cat /etc/tables | while read line
    echo $line
done
眼眸里的那抹悲凉 2024-12-03 22:13:33

使用 while 循环:

while read line
do
echo "$line"
done < "file"

With while loop:

while read line
do
echo "$line"
done < "file"
离鸿 2024-12-03 22:13:33
while IFS= read -r tbl; do echo "$tbl" ; done < /etc/tables

阅读

while IFS= read -r tbl; do echo "$tbl" ; done < /etc/tables

read this.

空心空情空意 2024-12-03 22:13:33

例如,您可以通过重新定义 IFS(输入字段分隔符)在 bash 中进行大量解析

IFS="\t\n"  # You must use double quotes for escape sequences. 
for tbl in `cat /tmp/tables` 
do 
    echo "$tbl"
done

You can do a lot of parsing in bash by redefining the IFS (Input Field Seperator), for example

IFS="\t\n"  # You must use double quotes for escape sequences. 
for tbl in `cat /tmp/tables` 
do 
    echo "$tbl"
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文