Chinaunix首页 | 论坛 | 博客
  • 博客访问: 91431190
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类:

2008-04-18 21:24:05

作者:tiansgx   
 这个脚本是偶自己写的,在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 line:$newline" 
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 
 
阅读(461) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~