分页: 1 / 1

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

发表于 : 2011-08-15 9:45
oppop007
我写了一个自动重命名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

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

发表于 : 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)

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

发表于 : 2011-08-15 10:08
oppop007
不就是man文档吗,我看过,还是不会啊~~ :em96

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

发表于 : 2011-08-15 11:16
eexpress
cd 进入之前的目录可以不。
或者使用环境变量设置路径。

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

发表于 : 2011-08-15 11:24
我就是我2
超链接
可以考虑相对路径。
在抓电子书时,我经常这样干。

代码: 全选

wget -nc -rk -np http://foo.com/foo/foobar

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

发表于 : 2011-08-15 14:40
oppop007
我就是我2 写了:
超链接
可以考虑相对路径。
在抓电子书时,我经常这样干。

代码: 全选

wget -nc -rk -np http://foo.com/foo/foobar
我是想通过replace来自动修改http根目录的index.html,所以不能用相对路径吧……

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

发表于 : 2011-08-15 15:41
cjxgm
sed -i?

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

发表于 : 2011-08-15 16:04
cjxgm
希望我没有理解错你的意思
[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]

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

发表于 : 2011-08-15 16:37
oppop007
Thank you very much~~ :em11 :em11 :em32

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

发表于 : 2011-08-15 16:45
oppop007
说实话,我就是不想让别人列出目录~ :em04

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

发表于 : 2011-08-16 13:34
tusooa
为啥

代码: 全选

ls "$prefix"|cat

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

发表于 : 2011-08-16 21:34
MaskRay
coreutils 的源码表明,stdout 非终端设备时默认 -1(可以被选项覆盖)