Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160152
  • 博文数量: 35
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 294
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-11 14:55
个人简介

努力奋斗的骚年!

文章分类

全部博文(35)

文章存档

2014年(19)

2013年(16)

分类: LINUX

2013-09-02 16:03:58

很棒的shell script//转
说明:这个脚本是可以从一个文本的某一行开始,逐行编辑文本,在编辑中也可选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

阅读(1544) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~