自己写的小东西,也是课后实验。
直接拷贝可能无法运行,会出现未知字符错误,这个小错误浪费了我好多时间。
#! /bin/sh
n=1
function insert(){
read str
sed -i '1i\'"$str" score.txt //插入输入变量str到文件的第一行(文件无内容可能要自己在文件添加记录,否则这条命令无效)
}
function modify(){
read str
read str1
read str2
sed -i "/$str/ s/$str1/$str2/" score.txt //第一个参数是包含str的行,用str2替换str1
}
function display(){
sed -n '1,$p' score.txt //显示所有行
}
function statics(){
awk '{print $0,$1,$3 | "sort -r -k 3"}' score.txt |head -10 //对第三个字段排序,取前10
}
function search(){
read str
awk "/$str/" score.txt //查找包含str的行
}
function delete(){
read str
sed -i "/^$str/d" score.txt //删除包含str参数的行
}
function total(){
awk '{ total=$3+$4 | print $0 total | head -20 }' score.txt
}
while [ "$n" > 0 ];
do
echo "=========================================="
echo "Welcome to Score Information Management"
echo "input number to excute"
echo "1-Insert data (Format:id name subjectname score score1 state:(final or makeup))"
echo "2-Modify data"
echo "3-Display data"
echo "4-Statics show"
echo "5-Search"
echo "6-Delete"
echo "7-Sort by total"
echo "8-Exit"
echo "=========================================="
read i
case "$i" in
1) insert ;;
2) modify ;;
3) display ;;
4) statics ;;
5) search ;;
6) delete ;;
7) total ;;
8) exit ;;
esac
done
这篇博客分享了作者如何使用Shell语句进行文件的增删改查操作,特别提醒直接复制脚本可能会遇到未知字符错误的问题,这个小问题可能导致一些困扰。

1448

被折叠的 条评论
为什么被折叠?



