Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 多次执行命令的shell脚本从输入文件读取值_Bash_Shell - Fatal编程技术网

Bash 多次执行命令的shell脚本从输入文件读取值

Bash 多次执行命令的shell脚本从输入文件读取值,bash,shell,Bash,Shell,我有一个输入文件input.txt,我想运行一个命令,从input.txt读取两个值。假设应该从输入中读取源名称和目标名称,并根据input.txt对同一命令进行数千次迭代 每个命令的输出也将存储在单独的日志中。这是一个单一的输入文件,还是我们需要使用两个文件作为源和目标?请求您提供用于实现此目的的shell脚本,因为我不擅长shell脚本。我试过下面的,但不起作用 while read i j; do command $i $j done > output.txt 当然。假设这是i

我有一个输入文件input.txt,我想运行一个命令,从input.txt读取两个值。假设应该从输入中读取源名称和目标名称,并根据input.txt对同一命令进行数千次迭代

每个命令的输出也将存储在单独的日志中。这是一个单一的输入文件,还是我们需要使用两个文件作为源和目标?请求您提供用于实现此目的的shell脚本,因为我不擅长shell脚本。我试过下面的,但不起作用

while read i j; do
  command $i $j
done > output.txt

当然。假设这是
input.txt

source1.txt dest1.txt
source2.txt dest2.txt
...
你想这样做:

command source1.txt dest1.txt
command source2.txt dest2.txt
...
这里有一个方法:

while read i o; do
    command $i $o
done < input.txt
或者,如果必须使用
命令$i>$o

awk '{printf "command %s > %s\n", $1, $2}' input.txt | sh
此方法从input.txt读取行,第一行打印
command source1.txt dest1.txt
,第二行打印
command source2.txt dest2.txt
,以此类推。。。然后将这些命令“管道”(
|
)传输到sh,由sh执行这些命令

有关
命令中的错误处理,请尝试:

while read i o; do
    command $i $o || command2 $i $o >> command2.log
done < input.txt 2> error.log
读取IO时
;做
命令$i$o | | command2$i$o>>command2.log
完成error.log
或:

doneerror.log 2>&1

(根据
command
command2
是否将错误打印到stdout(1)或stderr(2)中,其中一个会更好地工作。)

假设您希望在不同的文件中有不同的输出,然后在每个命令的日志文件和每个命令的一个错误文件中:

while read i o; do
  command $i $o 2>"$i$o.err" >"$i$o.log"
done < input.txt
您也可以在同一个文件
output.log

echo "" > output.log
while read i o; do
  command $i $o 2>&1 >> output.log
done < input.txt
echo”“>output.log
当我阅读时;做
命令$i$o 2>&1>>output.log
完成
谢谢韦伯,我试过了,它很管用。但我在一些命令中失败了,我们是否有任何方法可以检查结果,如果失败了,我们应该运行命令,并对其本身进行轻微修改。此外,在每个命令执行要加载到日志文件中的日志详细信息之后。读IO时,喜欢下面的内容;如果结果(良好)则执行命令$i$o下一次迭代否则命令2$i$o完成output log.txt
while read i o; do
  command $i $o 2>"$i$o.err" >"$i$o.log"
done < input.txt
while read i o; do
  command $i $o 2>&1 >"$i$o.log"
done < input.txt
echo "" > output.log
while read i o; do
  command $i $o 2>&1 >> output.log
done < input.txt