你在找这个吗?:
$ ls -1
INFO1 - INFO2 - INFO3.doc
INFO1 - INFO4 - INFO6.doc
l.sh
$ ./l.sh
<li>
<h5>INFO1</h5>
<p>INFO2</p>
</li>
<li>
<h5>INFO1</h5>
<p>INFO4</p>
</li>
剧本:
#!/bin/bash
for i in *.doc
do
# split file-name into array
IFS='- ' read -a array <<< "$i"
echo "<li>"
echo "<h5>${array[0]}</h5>"
echo "<p>${array[1]}</p>"
echo "</li>"
done
更新
如果文件名如下所示:
First name blabla - Second part bla boa - third part, unimportant.doc
改为将 IFS 参数更改为单个参数-
。
进行此更改:
IFS='-' read -a array <<< "$i"
输出:
$ ./l.sh
<li>
<h5>First name blabla </h5>
<p> Second part bla boa </p>
</li>
<li>
<h5>INFO1 </h5>
<p> INFO2 </p>
</li>
<li>
<h5>INFO1 </h5>
<p> INFO4 </p>
</li>
更新 2
#!/bin/bash
for i in *.doc
do
# split file-name into array
IFS='-' read -a array <<< "$i"
echo "<li>"
echo "<h5>${array[0]}</h5>"
echo "<p>${array[1]}</p>"
echo "</li>"
echo "<a href=\"files/${i}\">download</a>"
done
输出:
<li>
<h5>First name blabla </h5>
<p> Second part bla boa </p>
</li>
<a href="files/First name blabla - Second part bla boa - third part, unimportant.doc">download</a>
<li>
<h5>INFO1 </h5>
<p> INFO2 </p>
</li>
<a href="files/INFO1 - INFO2 - INFO3.doc">download</a>
<li>
<h5>INFO1 </h5>
<p> INFO4 </p>
</li>
<a href="files/INFO1 - INFO4 - INFO6.doc">download</a>