从文本文件中读取输入,Bash脚本

问题描述 投票:0回答:1

我不熟悉使用Bash编写脚本,我正在做一个分配清单系统的工作。我已经编写了所有脚本,并且它们都可以通过使用标准输入终端来工作。我现在正在寻找来自名为a1Input.txt的文本文件的输入,该文件的所有输入都换行。

r
9823
r
5430
c
7777
sml_widget
Small Widget (1 oz.)
6.99
15
50
Small, white, widget w/o packaging
r
7777
d
9823
r
9823
r
3293
u
3293


29.99
33
75


r
3293


我最初的bash脚本的代码是这个

#!/bin/bash

# assign1.bash

shopt -s nocasematch

option=""
until [ "$option" = "F" ]; do
echo "C - create a new item"
echo "R - read an existing item"
echo "U - update an existing item"
echo "D - delete an existing item"
echo "T - total price of an item"
echo "Choose a option"
read option
case $option in
C)
./create.bash
;;R)
./read.bash
;;U)
./update.bash
;;D)
./delete.bash
;;T)
./total.bash
;;*) 
echo "Invalid input"



esac
done

q: Command not found.

bash text-files stdin
1个回答
0
投票

我将文件中的输入注入到交互式脚本中的操作只是:

cat a1Input.txt | ./interactive_script.sh

例如,想象一下这个简单的脚本(在终端上复制粘贴创建):

cat << EOF > questions.sh
#!/bin/bash
echo "What's your name ?"
read name
echo "What is your favourite movie ?"
read movie
echo "Hi \$name, i also love '\$movie' movie !!"
EOF

以及这些输入:

cat << EOF > inputs.txt
Edu
Interstellar
EOF

然后,执行:

chmod a+x questions.sh
cat inputs.txt | ./questions.sh

如果脚本更复杂,请考虑使用“期望”,尽管这很复杂。

BRs

© www.soinside.com 2019 - 2024. All rights reserved.