如何使用 shell 脚本使用来自标准输入(键盘)的信息更新文件?
基本上我要做的是:
我有一个包含学生姓名及其部分成绩的文件:
(firstname lastname grade)
我要做的就是用他们的考试成绩和最终成绩更新该文件:
(partial+exam)/2
考试成绩是通过键盘引入的。因此,我必须回显学生的姓名并从键盘读取他的考试成绩,并更新文件中的行,然后转到下一个,当我全部完成后,将其排序在最终成绩列之后。
我只能从键盘上读取考试成绩。我尝试过使用某种形式的 awk
和 sed
但无法让它工作(无法从键盘获取信息),我什至不确定是否是这样必须这样做。
请帮忙。
提前, 班多
编辑: (用于编辑,我对此有点陌生)
输入看起来像这样:
Tom Green 7
Jenny Patricks 6
Andrew Gibbs 3
Collin Matthews 10
它必须从脚本运行。运行时应该将学生的名字和姓氏输出到标准输出,并询问他的成绩。一旦给定,它就会将文件编辑为如下所示:
Tom Green 7 9 8
然后转向下一个学生。
Basically what I have to do is this:
I have a file containing names of students and their partial grades:
(firstname lastname grade)
And what I have to do is update that file with their exam grades and their final grades:
(partial+exam)/2
The exam grades are introduced via the keyboard. So I have to echo a student's name and read his exam grade from the keyboard, and update his line within the file, then move on to the next one, and when I'm all done sort it after the final grade column.
I'm stuck at reading the exam grade from the keyboard. I've tried using some form of awk
and sed
but cant get it to work (cant get the info from the keyboard) and I'm not even sure if thats how it has to be done.
Please help.
ty in advance,
bando
edit:
(ty for editing, im a bit new to this)
input looks like this:
Tom Green 7
Jenny Patricks 6
Andrew Gibbs 3
Collin Matthews 10
it has to run from a script. while running it should put to standard output the first and last name of a student and ask for his grade. once given it edits the file to look like:
Tom Green 7 9 8
and then steps to the next student.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该脚本执行与您想要的类似操作:
但 shell 算术仅是整数。对于除法,您有两个运算符:
/
(整数除法)和%
(提醒)。我认为您应该考虑使用其他语言来完成该任务,例如python
或perl
。编辑:
您可能需要使用
apcalc
中的calc
命令。请参阅上面更正的脚本。您必须调整 calc 命令选项以防止它接管 STDIN。抱歉,我必须把这个留给你了——我现在没有时间。This script does something similar you want:
But the shell arithmetic is integer only. For dividing you have two operators:
/
(integer division) and%
(reminder). I think you should consider other language for the task likepython
orperl
.Edit:
You may want to use the
calc
command from theapcalc
. See the corrected script above. You'll have to tweak thecalc
command options to prevent it from taking over STDIN. Sorry I have to leave you with that - I have no time right now.cat >
将从键盘读取到您喜欢的任何位置 - 按 ctrl+D 关闭输入。cat >
will read from keyboard to wherever you like - press ctrl+D to close input.您可以使用
read
命令在shell脚本中从用户处获取用户输入。例如读取数据
。回显$数据
You can use the
read
command to get user input from the user in shell script. For exampleread data
.echo $data