LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 782|回复: 9

请大家帮我写个LINUX下的脚本吧!!谢谢了

[复制链接]
发表于 2003-11-11 13:35:50 | 显示全部楼层 |阅读模式
比如说我有一个目录,目录下有上万个文件
我想在每个文件内容的开头都加入一行“this is a book"
然后回车!

我该怎么弄呢
发表于 2003-11-11 18:57:24 | 显示全部楼层
for file in *
do
(echo "this is a book" ; cat $file )>tmp
mv tmp $file
done
发表于 2003-11-12 15:02:43 | 显示全部楼层
  1. [/home/javalee/myshell]for i in *
  2. > do
  3. > if [[ -f $i ]]
  4. > then
  5. > sed '1i\
  6. > abc' $i>tmp$i;rm $i;mv tmp$i $i
  7. > fi
  8. > done
复制代码
发表于 2003-11-12 16:11:14 | 显示全部楼层
最初由 lordbyorn 发表
for file in *
do
(echo "this is a book" ; cat $file )>tmp
mv tmp $file
done


用(echo 'this is a book" && cat $file) >tmp行吗?
发表于 2003-11-13 23:19:15 | 显示全部楼层
实践是检验真理的唯一标准 :p
发表于 2003-11-13 23:22:25 | 显示全部楼层
最初由 lordbyorn 发表
实践是检验真理的唯一标准 :p


发表于 2003-11-14 00:29:21 | 显示全部楼层
最初由 javalee 发表

  1. [/home/javalee/myshell]for i in *
  2. > do
  3. > if [[ -f $i ]]
  4. > then
  5. > sed '1i\
  6. > abc' $i>tmp$i;rm $i;mv tmp$i $i
  7. > fi
  8. > done
复制代码

经测试(系统为gentoo shell为bash)该脚本对全空文件无效。

最初由 lordbyorn 发表
for file in *
do
(echo "this is a book" ; cat $file )>tmp
mv tmp $file
done

经测试如果当前目录中有子目录,则子目录下会出现tmp文件,内容为this is a book。个人建议加入判断if [[ -f $i ]],以过滤目录。
发表于 2003-11-14 00:33:14 | 显示全部楼层
最初由 r2007 发表
经测试(系统为gentoo shell为bash)该脚本对全空文件无效。


经测试如果当前目录中有子目录,则子目录下会出现tmp文件,内容为this is a book。个人建议加入判断if [[ -f $i ]],以过滤目录。

谢谢你的测试!
发表于 2003-11-14 15:49:18 | 显示全部楼层
其实,还应该判断有没有tmp文件或目录,不过不想写了。
发表于 2003-11-17 14:54:59 | 显示全部楼层

  1. for i in *; do echo -e '0a\nthis isdd book\n.\nw\nq\n' | ed "$i"  ; done
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表