#!/bin/bash
echo "Content-type: text/html"
echo
echo "<h1>You Rolled 9 dice</h1>"
dice=( /images/die1.png /images/die2.png /images/die3.png /images/die4.png /images/die5.png /images/die6.png)
index=$RANDOM
let "index = index % 6"
href="${dice[$index]}"
echo "<a href=$href><img src=$href></a>"
这是我到目前为止所拥有的。我希望页面保留text / html,因为我将在其上写更多内容。它一直告诉我在这台服务器上找不到请求的URL /images/die5.png。
答案 0 :(得分:0)
您只需从图像文件名中删除前导/
字符:
#!/bin/bash
dice=(images/die1.png images/die2.png images/die3.png images/die4.png images/die5.png images/die6.png)
index=$RANDOM
let "index = index % 6"
href="${dice[$index]}"
echo '<!DOCTYPE html>'
echo '<html>'
echo ' <head>'
echo ' <meta charset="utf-8">'
echo ' <title>Example</title>'
echo ' </head>'
echo ' <body>'
echo ' <h1>You Rolled 9 dice</h1>'
echo " <a href='$href'><img src='$href' alt='image'></a>"
echo ' </body>'
echo '</html>'
像这样运行脚本后:
thomas@yozu-thomas:~/Programming/bashbrowser$ ./makehtm.sh > out.htm
我得到的输出是:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<h1>You Rolled 9 dice</h1>
<a href='images/die3.png'><img src='images/die3.png' alt='image'></a>
</body>
</html>
......看起来像这样:
注意:我假设文件结构是这样的:
makehtm.sh
out.htm
images/
die1.png
die2.png
die3.png
die4.png
die5.png
die6.png
此外,您不需要在HTML页面本身内echo "Content-type: text/html"
;这是HTTP header field,将由托管HTML文件的Web服务器处理。