简体   繁体   中英

Convert .html file to .txt using cp | error: cp: cannot stat '/*.html': No such file or directory

#!/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

ERROR: cp: cannot stat '/ .html': No such file or directory cp: failed to access 'index.html/ .txt': Not a directory

The $1 in the below statement is the first command line parameter.

$1/*.html

In your code, it is expecting the parent directory name containing the HTML files. Suppose, you parent directory is /home/user/my_html_files , then if you pass this as the command line parameter, then all the HTML files inside this directory will be considered.

# ./convert_html_to_txt.sh /home/user/my_html_files

The above will result into /home/user/my_html_files/*.html in your code. If your HTML file is in current directory, just pass . as the command line parameter ( . denotes current directory)

Firstly you need to pass a directory name(with complete path) to script and second since you are looping through only HTML files in directory so you need not to do check for it again rather you could put check condition on its copy either it copied as .txt successfully or not. I believe most probably you are looking for this kind of solution.

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

Then run the script.ksh as script.ksh "/directory_name/with_full_path" . Also I have put echo before cp command so once you see command is printing correct by above script you could remove it then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM