如何构建脚本以将多个文件名插入到 (html) 文件中?

如何构建脚本以将多个文件名插入到 (html) 文件中?

我很确定这个问题已经在某个地方得到了回答,但我不知道如何向谷歌表达我的问题,以便找到它!我通常的方法是找到一个类似的脚本并对其进行改编,但我在开始使用这个脚本时遇到了困难。不过,我知道 sed 会参与其中。

我有一组可变(名称和数量)的视频文件;例如

2020_12_15-14_20_56_Event23.mkv
2020_12_15-14_24_28_Event24.mkv
2020_12_15-15_09_11_Event25.mkv
2020_12_15-15_15_26_Event26.mkv
2020_12_15-15_18_36_Event27.mkv
2020_12_15-15_42_16_Event28.mkv
2020_12_15-15_46_15_Event29.mkv
2020_12_15-15_49_48_Event30.mkv
2020_12_15-16_15_03_Event31.mkv
2020_12_15-16_20_05_Event32.mkv

我想运行一个脚本来生成一个显示它们的 html 页面;例如

<!DOCTYPE html>

<html>
<body>

<h1>Test</h1>

<video width="1280" height="720" controls>
  <source src="2020_12_04-13_46_45_Event23.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_04-13_46_45_Event24.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="INSERT NEXT FILE IN LIST HERE" type="video/webm">
  Your browser does not support the video tag.
</video>

(...etc)

</body>
</html>

我希望仅使用 bash 就可以做到这一点,因为我希望将其保留为纯 html(没有 php、javascript 等),如果可以的话:运行脚本,选取目录中的所有文件并生成一堆 html多于。

提前致谢。

答案1

这非常简单。只需将页眉和页脚放入变量中,并对仅根据文件名更改的文本块执行相同的操作。然后迭代您的文件名,并打印每个文件名。像这样的东西:

#!/bin/bash

read -r -d '' header <<EoF
<!DOCTYPE html>

<html>
<body>

<h1>Test</h1>

EoF

read -r -d '' footer <<EoF
</body>
</html>
EoF

read -r -d '' printfFormatString <<EoF
<video width="1280" height="720" controls>
  <source src="%s" type="video/webm">
  Your browser does not support the video tag.
</video>\n\n
EoF

printf "%s\n" "$header"
for file in "$@"; do
    printf "$printfFormatString" "$file"
done
printf "%s\n" "$footer"
  

然后,您可以使用文件名作为参数运行脚本:

$ foo.sh *mkv
<!DOCTYPE html>

<html>
<body>

<h1>Test</h1>
<video width="1280" height="720" controls>
  <source src="2020_12_15-14_20_56_Event23.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-14_24_28_Event24.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-15_09_11_Event25.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-15_15_26_Event26.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-15_18_36_Event27.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-15_42_16_Event28.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-15_46_15_Event29.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-15_49_48_Event30.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-16_15_03_Event31.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

<video width="1280" height="720" controls>
  <source src="2020_12_15-16_20_05_Event32.mkv" type="video/webm">
  Your browser does not support the video tag.
</video>

</body>
</html>

相关内容