[已解决]如何替换一个文件的内容?

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
oppop007
帖子: 128
注册时间: 2010-12-28 19:12
来自: Nanjing, Jiangsu, China

[已解决]如何替换一个文件的内容?

#1

帖子 oppop007 » 2011-08-15 9:45

我写了一个自动重命名Http服务器目录的Shell script,为了防止未授权的下载和入侵。
可是,既然目录变了,那html文件里的超链接就也需要改变。据说用replace命令可以,可是我不知道怎么用…… :em20
先把我的Shell script贴上来……(我只是个初中生,所以在script中可能会犯一些低级错误,而且coding style可能也不是那么严谨,别见笑哈~) :em06

代码: 全选

#!/bin/sh

# rename-dirs - rename dirs in http server directory to prevent it from Thunder's resource-scan

# get new names
newname=`uuidgen`
newnamea=`uuidgen`

# prefix
prefix="/var/www/my/"

# get level 1 old name
oldname=`ls "$prefix"|cat`

echo "Pending operation on level 1..."

echo " Old main dir (full path) is" "$prefix""$oldname"
echo " New main dir (full path) is" "$prefix""$newname"

echo " Renaming..."

# rename level 1
mv "$prefix""$oldname" "$prefix""$newname"

prefixa=`echo "$prefix""$newname"`

# get level 2 old name
oldnamea=`ls "$prefixa"|cat`

echo "Pending operation on level 2..."

echo " Old main dir (full path) is" "$prefixa"/"$oldnamea"
echo " New main dir (full path) is" "$prefixa"/"$newnamea"

echo " Renaming..."

# rename level 2
mv "$prefixa"/"$oldnamea" "$prefixa"/"$newnamea"

# for safe, chmod......
chmod =x "$prefix" 
chmod =x "$prefix""$newname"
chmod =x "$prefixa"/"$newnamea"


# replace new names in html files

替换的内容就应该加在下面……
Who can help me?
在这里先谢谢大家了…… :em03
上次由 oppop007 在 2011-08-15 16:38,总共编辑 1 次。
# whoami
古风派;Linux爱好者;吟风弄月者;爱装可爱者;Geek;应试教育的受害者。

# cat /etc/issue
本人一般每周六才能玩电脑,所以平时会偷偷地用那台“云媒体电视”上论坛,不过操作很麻烦,就一个遥控器。有时无法换行,无法发表情&正常使用BBCode;浏览器很破,相当于Firefox<3.0,不支持Javascript等……(>_<)……So, 如果本人发的文字出现了格式错误,请多多包涵。谢啦~
头像
灰色小狼
帖子: 4585
注册时间: 2008-12-06 10:38
系统: Arch

Re: 如何替换一个文件的内容?

#2

帖子 灰色小狼 » 2011-08-15 9:58

REPLACE(1) MySQL Database System REPLACE(1)



NAME
replace - a string-replacement utility

SYNOPSIS
replace arguments

DESCRIPTION
The replace utility program changes strings in place in files or on the
standard input.

Invoke replace in one of the following ways:

shell> replace from to [from to] ... -- file_name [file_name] ...
shell> replace from to [from to] ... < file_name

from represents a string to look for and to represents its replacement.
There can be one or more pairs of strings.

Use the -- option to indicate where the string-replacement list ends
and the file names begin. In this case, any file named on the command
line is modified in place, so you may want to make a copy of the
original before converting it. replace prints a message indicating
which of the input files it actually modifies.

If the -- option is not given, replace reads the standard input and
writes to the standard output.

replace uses a finite state machine to match longer strings first. It
can be used to swap strings. For example, the following command swaps a
and b in the given files, file1 and file2:

shell> replace a b b a -- file1 file2 ...

The replace program is used by msql2mysql. See msql2mysql(1).

replace supports the following options.

路 -?, -I

Display a help message and exit.

路 -#debug_options

Enable debugging.

路 -s

Silent mode. Print less information what the program does.

路 -v

Verbose mode. Print more information about what the program does.

路 -V

Display version information and exit.

COPYRIGHT
Copyright 漏 1997, 2010, Oracle and/or its affiliates. All rights
reserved.

This documentation is free software; you can redistribute it and/or
modify it only under the terms of the GNU General Public License as
published by the Free Software Foundation; version 2 of the License.

This documentation is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License along
with the program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, BosREPLACE(1) MySQL Database System REPLACE(1)



NAME
replace - a string-replacement utility

SYNOPSIS
replace arguments

DESCRIPTION
The replace utility program changes strings in place in files or on the
standard input.

Invoke replace in one of the following ways:

shell> replace from to [from to] ... -- file_name [file_name] ...
shell> replace from to [from to] ... < file_name

from represents a string to look for and to represents its replacement.
There can be one or more pairs of strings.

Use the -- option to indicate where the string-replacement list ends
and the file names begin. In this case, any file named on the command
line is modified in place, so you may want to make a copy of the
original before converting it. replace prints a message indicating
which of the input files it actually modifies.

If the -- option is not given, replace reads the standard input and
writes to the standard output.

replace uses a finite state machine to match longer strings first. It
can be used to swap strings. For example, the following command swaps a
and b in the given files, file1 and file2:

shell> replace a b b a -- file1 file2 ...

The replace program is used by msql2mysql. See msql2mysql(1).

replace supports the following options.

路 -?, -I

Display a help message and exit.

路 -#debug_options

Enable debugging.

路 -s

Silent mode. Print less information what the program does.

路 -v

Verbose mode. Print more information about what the program does.

路 -V

Display version information and exit.

COPYRIGHT
Copyright 漏 1997, 2010, Oracle and/or its affiliates. All rights
reserved.

This documentation is free software; you can redistribute it and/or
modify it only under the terms of the GNU General Public License as
published by the Free Software Foundation; version 2 of the License.

This documentation is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License along
with the program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or see
http://www.gnu.org/licenses/.


SEE ALSO
For more information, please refer to the MySQL Reference Manual, which
may already be installed locally and which is also available online at
http://dev.mysql.com/doc/.

AUTHOR
Oracle Corporation (http://dev.mysql.com/).



MySQL 5.1 11/29/2010 REPLACE(1)ton, MA 02110-1301 USA or see
http://www.gnu.org/licenses/.


SEE ALSO
For more information, please refer to the MySQL Reference Manual, which
may already be installed locally and which is also available online at
http://dev.mysql.com/doc/.

AUTHOR
Oracle Corporation (http://dev.mysql.com/).



MySQL 5.1 11/29/2010 REPLACE(1)
头像
oppop007
帖子: 128
注册时间: 2010-12-28 19:12
来自: Nanjing, Jiangsu, China

Re: 如何替换一个文件的内容?

#3

帖子 oppop007 » 2011-08-15 10:08

不就是man文档吗,我看过,还是不会啊~~ :em96
# whoami
古风派;Linux爱好者;吟风弄月者;爱装可爱者;Geek;应试教育的受害者。

# cat /etc/issue
本人一般每周六才能玩电脑,所以平时会偷偷地用那台“云媒体电视”上论坛,不过操作很麻烦,就一个遥控器。有时无法换行,无法发表情&正常使用BBCode;浏览器很破,相当于Firefox<3.0,不支持Javascript等……(>_<)……So, 如果本人发的文字出现了格式错误,请多多包涵。谢啦~
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 如何替换一个文件的内容?

#4

帖子 eexpress » 2011-08-15 11:16

cd 进入之前的目录可以不。
或者使用环境变量设置路径。
● 鸣学
头像
我就是我2
帖子: 1215
注册时间: 2008-12-13 10:55
来自: the Earth
联系:

Re: 如何替换一个文件的内容?

#5

帖子 我就是我2 » 2011-08-15 11:24

超链接
可以考虑相对路径。
在抓电子书时,我经常这样干。

代码: 全选

wget -nc -rk -np http://foo.com/foo/foobar
苏东坡问佛印道:“人人皆念观世音菩萨,观世音菩萨念谁?”
佛印答道:“念观世音菩萨。”
苏东坡诧异:“为何亦念观世音菩萨?”
曰:“求人不如求己。”


dropbox 誰好奇,誰來點
勞碌一生又如何,虛度一生又如何,你過得開心嗎?人活著就要開心嘛!
头像
oppop007
帖子: 128
注册时间: 2010-12-28 19:12
来自: Nanjing, Jiangsu, China

Re: 如何替换一个文件的内容?

#6

帖子 oppop007 » 2011-08-15 14:40

我就是我2 写了:
超链接
可以考虑相对路径。
在抓电子书时,我经常这样干。

代码: 全选

wget -nc -rk -np http://foo.com/foo/foobar
我是想通过replace来自动修改http根目录的index.html,所以不能用相对路径吧……
# whoami
古风派;Linux爱好者;吟风弄月者;爱装可爱者;Geek;应试教育的受害者。

# cat /etc/issue
本人一般每周六才能玩电脑,所以平时会偷偷地用那台“云媒体电视”上论坛,不过操作很麻烦,就一个遥控器。有时无法换行,无法发表情&正常使用BBCode;浏览器很破,相当于Firefox<3.0,不支持Javascript等……(>_<)……So, 如果本人发的文字出现了格式错误,请多多包涵。谢啦~
头像
cjxgm
帖子: 1952
注册时间: 2010-04-23 20:40
系统: Arch Linux
来自: 浙江·杭州
联系:

Re: 如何替换一个文件的内容?

#7

帖子 cjxgm » 2011-08-15 15:41

sed -i?
Clanjor Prods. | Develop for Developers. (C++, Lua) | 作曲编曲 | 实时渲染引擎
头像
cjxgm
帖子: 1952
注册时间: 2010-04-23 20:40
系统: Arch Linux
来自: 浙江·杭州
联系:

Re: 如何替换一个文件的内容?

#8

帖子 cjxgm » 2011-08-15 16:04

希望我没有理解错你的意思
[bash]#!/bin/bash

# rename-dirs - rename dirs in http server directory to protect them from Thunder's resource-scanner

# get new names
newname=`uuidgen`
newnamea=`uuidgen`

# prefix
prefix="/var/www/my/"

# get level 1 old name
oldname=`ls "$prefix"`

echo "Pending operation on level 1..."

echo " Old main dir (full path) is $prefix/$oldname"
echo " New main dir (full path) is $prefix/$newname"

echo " Renaming..."

# rename level 1
mv "$prefix/$oldname" "$prefix/$newname"

prefixa="$prefix/$newname"

# get level 2 old name
oldnamea=`ls "$prefixa"`

echo "Pending operation on level 2..."

echo " Old main dir (full path) is $prefixa/$oldnamea"
echo " New main dir (full path) is $prefixa/$newnamea"

echo " Renaming..."

# rename level 2
mv "$prefixa/$oldnamea" "$prefixa/$newnamea"

# for safety, chmod......
# !!! for dirs, 'x' means 'accessable', not 'executable' !!!
# If you disabled 'x' for dirs, you can not even list/ls them!
# So, do NOT chmod like that.

# replace new names in html files
sed -i "s/$oldname/$newname/g; s/$oldnamea/$newnamea/g" index.html[/bash]
Clanjor Prods. | Develop for Developers. (C++, Lua) | 作曲编曲 | 实时渲染引擎
头像
oppop007
帖子: 128
注册时间: 2010-12-28 19:12
来自: Nanjing, Jiangsu, China

Re: 如何替换一个文件的内容?

#9

帖子 oppop007 » 2011-08-15 16:37

Thank you very much~~ :em11 :em11 :em32
# whoami
古风派;Linux爱好者;吟风弄月者;爱装可爱者;Geek;应试教育的受害者。

# cat /etc/issue
本人一般每周六才能玩电脑,所以平时会偷偷地用那台“云媒体电视”上论坛,不过操作很麻烦,就一个遥控器。有时无法换行,无法发表情&正常使用BBCode;浏览器很破,相当于Firefox<3.0,不支持Javascript等……(>_<)……So, 如果本人发的文字出现了格式错误,请多多包涵。谢啦~
头像
oppop007
帖子: 128
注册时间: 2010-12-28 19:12
来自: Nanjing, Jiangsu, China

Re: [已解决]如何替换一个文件的内容?

#10

帖子 oppop007 » 2011-08-15 16:45

说实话,我就是不想让别人列出目录~ :em04
# whoami
古风派;Linux爱好者;吟风弄月者;爱装可爱者;Geek;应试教育的受害者。

# cat /etc/issue
本人一般每周六才能玩电脑,所以平时会偷偷地用那台“云媒体电视”上论坛,不过操作很麻烦,就一个遥控器。有时无法换行,无法发表情&正常使用BBCode;浏览器很破,相当于Firefox<3.0,不支持Javascript等……(>_<)……So, 如果本人发的文字出现了格式错误,请多多包涵。谢啦~
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: [已解决]如何替换一个文件的内容?

#11

帖子 tusooa » 2011-08-16 13:34

为啥

代码: 全选

ls "$prefix"|cat

代码: 全选

] ls -ld //
MaskRay
帖子: 61
注册时间: 2010-04-29 22:00
系统: Gentoo Linux ~amd64
联系:

Re: [已解决]如何替换一个文件的内容?

#12

帖子 MaskRay » 2011-08-16 21:34

coreutils 的源码表明,stdout 非终端设备时默认 -1(可以被选项覆盖)
回复