如何使用 shell 脚本使用来自标准输入(键盘)的信息更新文件?

发布于 2024-11-15 07:54:57 字数 658 浏览 0 评论 0原文

基本上我要做的是:

我有一个包含学生姓名及其部分成绩的文件:

(firstname lastname grade)

我要做的就是用他们的考试成绩和最终成绩更新该文件:

(partial+exam)/2

考试成绩是通过键盘引入的。因此,我必须回显学生的姓名并从键盘读取他的考试成绩,并更新文件中的行,然后转到下一个,当我全部完成后,将其排序在最终成绩列之后。

我只能从键盘上读取考试成绩。我尝试过使用某种形式的 awksed 但无法让它工作(无法从键盘获取信息),我什至不确定是否是这样必须这样做。

请帮忙。

提前, 班多

编辑: (用于编辑,我对此有点陌生)

输入看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断肠人 2024-11-22 07:54:57

该脚本执行与您想要的类似操作:

#!/bin/bash

# $1 input file
# $2 output file (or equal to $1 if ommitted)

if [ -z "$2" ]; then
    OUTFILE=$1
else
    OUTFILE=$2
fi

cat $1 | while read -u 0 FIRST LAST PART ; do
    echo "firstname: '$FIRST'"
    echo "lastname: '$LAST'"
    echo "grade: '$PART'"
    read -p "Exam grade: " -u 1 EXAM

    #Edit
    # was   let FINAL=(PART+EXAM)/2
    # should be:
    FINAL=$(calc \($PART+$EXAM\)/2)

    echo "$FIRST $LAST $PART $EXAM $FINAL" >> tmp
    echo "final grade: '$FINAL'"
    echo -e "-----------\n"
done

mv tmp $OUTFILE

但 shell 算术仅是整数。对于除法,您有两个运算符:/(整数除法)和%(提醒)。我认为您应该考虑使用其他语言来完成该任务,例如 pythonperl


编辑:
您可能需要使用 apcalc 中的 calc 命令。请参阅上面更正的脚本。您必须调整 calc 命令选项以防止它接管 STDIN。抱歉,我必须把这个留给你了——我现在没有时间。

This script does something similar you want:

#!/bin/bash

# $1 input file
# $2 output file (or equal to $1 if ommitted)

if [ -z "$2" ]; then
    OUTFILE=$1
else
    OUTFILE=$2
fi

cat $1 | while read -u 0 FIRST LAST PART ; do
    echo "firstname: '$FIRST'"
    echo "lastname: '$LAST'"
    echo "grade: '$PART'"
    read -p "Exam grade: " -u 1 EXAM

    #Edit
    # was   let FINAL=(PART+EXAM)/2
    # should be:
    FINAL=$(calc \($PART+$EXAM\)/2)

    echo "$FIRST $LAST $PART $EXAM $FINAL" >> tmp
    echo "final grade: '$FINAL'"
    echo -e "-----------\n"
done

mv tmp $OUTFILE

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 like python or perl.


Edit:
You may want to use the calc command from the apcalc. See the corrected script above. You'll have to tweak the calc command options to prevent it from taking over STDIN. Sorry I have to leave you with that - I have no time right now.

剪不断理还乱 2024-11-22 07:54:57

cat > 将从键盘读取到您喜欢的任何位置 - 按 ctrl+D 关闭输入。

cat > will read from keyboard to wherever you like - press ctrl+D to close input.

梦行七里 2024-11-22 07:54:57

您可以使用read命令在shell脚本中从用户处获取用户输入。例如
读取数据
回显$数据

You can use the read command to get user input from the user in shell script. For example
read data
echo $data
.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文