【发布时间】:2022-01-19 02:17:25
【问题描述】:
我不知道如何在文件中添加一行,然后只用行号打印该行。这是我当前打印所有行的代码:
printf "Would you like to add an entry to the file? Type yes or y? "
read addline
input=$addline
if [ $input = 'yes' ] || [ $input = 'y' ]
then
printf "Please enter a new name: First Last: "
read newname
printf "Please enter a new telephone number: xxx-xxx-xxxx: "
read phone
printf "Please enter a street address only: xxx Example Street: "
read street
printf "Please enter your birthdate: xx-xx-xxxx: "
read birth
printf "Please enter salary: xxxxxxx: "
read salary
echo -e "$newname:$phone:$street:$birth:$salary" >> datafile.txt
else
printf "\nYou did not type yes or y. Run the program again.\n"
exit
fi
printf "\nYour entry has been saved.\n"
sort -k2 $file | nl
【问题讨论】:
-
为什么需要
input=$addline?为什么不只是read input? -
您为什么要使用
-e选项来代替echo?用户通常不会输入带有转义序列的数据。 -
echo -e - 不确定,我删除了它,它可以工作。
-
input = $addline - 谢谢。