免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3707 | 回复: 9
打印 上一主题 下一主题

发送一个编辑文本的脚本!  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-09-05 16:30 |只看该作者 |倒序浏览
这个脚本是偶自己写的,在redhat下调试成功。
说明:这个脚本是可以从一个文本的某一行开始,逐行编辑文本,在编辑中也可选q退出,最后提示是否保存更改,更改成功后写入到原文本文件中。
比如:文本文件file1第5行是“ni hao”
想改成“hello”
就可以用tiansgx file1
然后按照提示一步步更改即可。

#!/bin/bash
# Script name: tiansgx
function fun1 # 判断参数个数为1个
{
if [ $num -gt 1 ]
then
{
echo Warning:Too many parameter!
exit 0
}
elif [ $num -eq 0 ]
then
{
echo Error:No Filename!
exit 0
}
fi
}

function fun2 # 接受输入起始行号
{
while :
do
echo -n "Input start page number:"
read page
let page=$page+1-1
if [ $page -gt 0 ]
then
{
break
}
fi
done
}

rm -f tmp.tmp
let num=$#
fun1
cat -n $1
fun2
cou=1
cou2=1
exec < $1 # 打开文件
while read line # 逐行读入文件
do
if [ $cou -lt $page ]
then
{
echo $line
echo $line >> tmp.tmp
let cou=$cou+1
continue
}
else
{
echo $line
if [ $cou2 -eq 1 ]
then
{
echo -n "Do you want to edit the line($cou)? [Y/N/Q]: "
let cou=$cou+1
read answer < /dev/tty
case $answer in
[Yy])
echo "Input the new line: "
read newline < /dev/tty
echo $newline >> tmp.tmp
echo "The new linenewline"
continue
;;
[Nn])
echo $line >> tmp.tmp
continue
;;
[Qq])
let cou2=2
echo $line >> tmp.tmp
continue
;;
*)
echo $line >> tmp.tmp
continue
;;
esac
}
elif [ $cou2 -eq 2 ]
then
{
echo $line >> tmp.tmp
}
fi
}
fi
done
while : # 是否保存更改
do
echo -n "Do you want to save the changes?[Y/N]:"
read ans2 < /dev/tty
case $ans2 in
[Yy])
mv -f tmp.tmp $1
exit 0  
;;
[Nn])
rm -f tmp.tmp
exit 0
;;
esac
done

论坛徽章:
0
2 [报告]
发表于 2003-09-05 16:32 |只看该作者

发送一个编辑文本的脚本!

第一次写这么长的脚本,难免有不足之处,欢迎大家指教

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2003-09-05 16:44 |只看该作者

发送一个编辑文本的脚本!

顶!  
置精,鼓励原创!

15.gif (15.69 KB, 下载次数: 39)

15.gif

论坛徽章:
0
4 [报告]
发表于 2003-09-05 16:50 |只看该作者

发送一个编辑文本的脚本!

谢谢!还请老兄给我检查检查,提出一些建议,共同进步才是偶的目的,呵呵

论坛徽章:
0
5 [报告]
发表于 2003-09-05 16:57 |只看该作者

发送一个编辑文本的脚本!

  1.      if [ $cou -lt $page ]
  2. then
  3. {
  4. echo $line
  5. echo $line >> tmp.tmp
  6. let cou=$cou+1
  7. continue
  8. }
复制代码


这个语句我想最好改一改。
如果行数比较多,比如有500行,屏幕一直滚动不停,我就看不到具体要改哪一行。

如果能自己设定每显示多少行就停顿一下,那就更好了。

:wink:
[/code]

论坛徽章:
0
6 [报告]
发表于 2003-09-05 17:15 |只看该作者

发送一个编辑文本的脚本!

对了,应该将开始的cat -n $1改为cat -n $1 | more
谢谢yoursmile

论坛徽章:
0
7 [报告]
发表于 2003-09-05 22:18 |只看该作者

发送一个编辑文本的脚本!

编辑文本的脚本,好东西,得仔细瞧瞧........收藏!

论坛徽章:
0
8 [报告]
发表于 2003-09-08 00:41 |只看该作者

发送一个编辑文本的脚本!

hello guy,  very good ! a nice script
I suggest increase some codes to rm tmp file for non-normal script exit
trap " rm -f  /tmp/tmp$$"  1 2 3 9 15
do you think so?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
9 [报告]
发表于 2003-09-08 07:24 |只看该作者

发送一个编辑文本的脚本!

原帖由 "joe_conner" 发表:
hello guy,  very good ! a nice script
I suggest increase some codes to rm tmp file for non-normal script exit
trap " rm -f  /tmp/tmp$$"  1 2 3 9 15
do you think so?
  
Good idea!

论坛徽章:
0
10 [报告]
发表于 2003-09-08 08:48 |只看该作者

发送一个编辑文本的脚本!

原帖由 "joe_conner" 发表:
hello guy,  very good ! a nice script
I suggest increase some codes to rm tmp file for non-normal script exit
trap " rm -f  /tmp/tmp$$"  1 2 3 9 15
do you think so?
   
OK! Very good!
Thank you!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP