我已经编写了一个bash脚本,可以通过命令行输入bash bash_file.sh input_file.txt output_file.txt
,这很好用。我已经使它可以正常工作了,它可以从input_file Hello 10
中读取行并将Hello
字输出10次到output_file.txt
。这么多工作。现在,我不知道如何使bash脚本处理这种类型的命令行:bash bash_file.sh < input_file.txt > output_file.txt
。它引发了各种各样的错误,我什至无法开始寻找解决方案-如果有人有解决方案,或者可以指出我的解决方案方向,那就太好了。
下面的代码适用于以下命令行命令bash bash_file.sh input_file.txt output_file.txt
。
string_word=$(<$1)
IFS=' '
read -ra ADDR <<< "$string_word"
num="${ARR[2]}"
str="${ARR[0]} ${ARR[1]}"
while [[ $num -ne 0 ]]; do
echo $num
echo "$str" >> $2
num=$(( num - 1 ))
done
现在,我正在尝试对其进行修改,因此以下命令行命令可以在其上运行:bash bash_file.sh < input_file.txt > output_file.txt
[忽略脚本中的错误,如阅读ADDR
但引用ARR
的方式一样,通常无需使用源即可从stdin读取而直接使用read
,而无需重定向以写入stderr即可使用echo
:] >
read -ra ARR num="${ARR[1]}" str="${ARR[0]}" while [[ $num -ne 0 ]]; do echo "$str" num=$(( num - 1 )) done
它是这样的:
$ echo 'Hello 10' > input_file.txt
$ bash bash_file.sh < input_file.txt > output_file.txt
$ cat output_file.txt
Hello
Hello
Hello
[...]