我正在使用shell脚本读取一个包含某些文件路径的属性文件。现在取决于这个文件路径,我想创建zip文件的名称。事情是这样的......我的属性文件内容::如何使用shell脚本在unix中创建所需的文件名
path=tmp/inputs/logs/abc
path=tmp/backup/inte/xyz
destpath=abc/xyz
现在我能够创建文件名为abc.zip和xyz.zip为:
paths=`grep path myfile.property |cut -d= -f2`
d_path=`grep destpath myfile.property |cut -d= -f2`
filename=$d_path/$(basename $paths).zip
其中创建abc.zip和xyz.zip。但是我想通过获取路径的最后三个参数来创建名称。事情是这样的......
- 为
abc.zip
应该inputs_logs_abc.zip
和 - 为
xyz.zip
应该backup_inte_xyz.zip
编辑
Paths=`grep path myfile.txt |cut -d= -f2`
d_Path=`grep destpath myfile.txt |cut -d= -f2`
for s_Path in $Paths
do
prefix=${Paths%%/*/*/*}
without_prefix=${Paths##${prefix}/}
slashes_to_underscores=${without_prefix//\//_}
zipFile=$d_Path/${slashes_to_underscores}.zip
find $s_Path -type f -name "*.log" | xargs zip -mT $zipFile [email protected]
done
以上就是我code.By使用这个我不能达到我的目标。 有人可以帮助我吗?
你正在编写哪个shell?这些片段中的'destpath'和'd_path'的相关性是什么? – Johnsyweb 2010-10-05 17:13:52
我正在使用bash.destpath是属性文件中存在的关键字,据此我读取此文件并将此路径存储在d_path变量中。 – MAS1 2010-10-05 17:52:46
然后我的答案应该可以解决你的问题。你可以在提示符下试一试,并在你去看它是如何工作的时候回显变量。 – Johnsyweb 2010-10-05 18:36:11