#!/bin/bash
# Make a txt copy of any html files
for value in $1/*.html
do
if [[ $value == *.html ]]; then
cp $value $1/$( basename -s .html $value ).txt
fi
done
错误:cp:无法统计'/.html':没有这样的文件或目录cp:无法访问'index.html / .txt':不是目录
以下语句中的$1
是第一个命令行参数。
$1/*.html
在您的代码中,它期望包含HTML文件的父目录名称。假设您的父目录是/home/user/my_html_files
,那么如果您将其作为命令行参数传递,则将考虑此目录中的所有HTML文件。
# ./convert_html_to_txt.sh /home/user/my_html_files
以上将导致您的代码中的/home/user/my_html_files/*.html
。如果你的HTML文件在当前目录中,只需将.
作为命令行参数传递(.
表示当前目录)
首先,你需要将一个目录名称(带有完整路径)传递给脚本,然后再循环通过目录中的HTML文件,这样你就不需要再次检查它而不是你可以将复制条件复制为副本.txt
成功与否。我相信你很可能正在寻找这种解决方案。
cat script.ksh
for value in $1/*.html
do
temp=${value%.*}
echo cp "$value" "$1/$temp.txt"
if [[ $? -eq 0 ]]
then
echo "File named $value copied successfully to "$value" "$1/$temp.txt"
else
echo "Please check file named $value NOT copied to "$value" "$1/$temp.txt"
fi
done
然后运行script.ksh
作为script.ksh "/directory_name/with_full_path"
。另外我在echo
命令之前放了cp
所以一旦你看到命令通过上面的脚本打印正确,你可以删除它。