简体   繁体   English

Bash逐行读取文件,按选项卡拆分,发送到Java应用程序

[英]Bash read file line by line, split by tab, send to java application

I want to read from a file line by line, then construct a args string and then use this string to start an java application. 我想逐行读取文件,然后构造一个args字符串,然后使用该字符串启动Java应用程序。

The file test.txt contains for example this lines, columns are tab separated: 例如,文件test.txt包含以下行,各列用制表符分隔:

abc def ghj kln abc def ghj kln

asd ss fdf twe asd ss fdf twe

#!/bin/bash
IFS=$'\n'
while read k d a m s
do
    echo java -jar test.jar -k $k -d $d -a $a -m $m -s $s
done < test.txt

unfortunatly it does not work. 不幸的是,它不起作用。 bash output is broken: bash输出损坏:

-k abc def ghj kln -d -a -m -s -k abc def ghj kln -d -a -m -s

You say the columns are tab separated, so you should use \\t for IFS instead of \\n which means newline: 您说各列之间用制表符分隔,因此对于IFS应该使用\\t而不是\\n ,这意味着换行符:

IFS=$'\t'

(Assuming that each line of the input contains the values for k , d , a , m , s separated by tabs). (假设输入的每一行都包含用制表符分隔的kdams的值)。

You just have to change your IFS to separate on tabs \\t rather then new line \\n : 您只需要更改IFS以在选项卡\\t上分开,而不是在新行\\n上分开即可:

#!/bin/bash
while IFS=$'\t' read k d a m s
do
    echo java -jar test.jar -k "$k" -d "$d" -a "$a" -m "$m" -s "$s"
done < test.txt

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

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