简体   繁体   English

Bash脚本+按行+ Ambiguos重定向读取行?

[英]Bash Script + Read Line by Line + Ambiguos Redirect?

I have this simple script: 我有这个简单的脚本:

#!/bin/bash
cmd="file.txt"

while read line
do
    command $line > $line

done < $cmd

And this .txt file: 这个.txt文件:

./cmd var1 var2 var3
./cmd var1 var2 var3
./cmd var1 var2 var3
./cmd var1 var2 var3

My goal is to read each line and execute the command, but I keep getting this error: 我的目标是读取每一行并执行命令,但我不断收到此错误:

 line x: $line: ambiguos redirect

I am new to BASH and I have no idea what this error means and while researching it, dozens of various explanations came up. 我是BASH的新手,我不知道这个错误意味着什么,在研究它时,出现了几十种不同的解释。 Does any1 have an idea what I could be doing wrong? 有没有人知道我可能做错了什么?

What you're executing is essentially: 你正在执行的是:

command var1 var2 var3 > var1 var2 var3

The shell can't figure out which file you want the output to redirect to: var1 , var2 , or var3 shell无法确定要将输出重定向到哪个文件: var1var2var3

I'm not certain what you're trying to do, but if you want to output to, say var1 , then you could do: 我不确定你要做什么,但是如果要输出到,比如var1 ,那么你可以这样做:

while read firstVar line; do
  command $firstVar $line > $firstVar
done < file.txt

However, if--as your post says--you only want to execute the command, then you don't need redirection at all. 但是,如果 - 如您的帖子所说 - 您只想执行命令,那么您根本不需要重定向。 Simply do: 简单地说:

while read line; do
  command $line
done < file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM