问题
mv: cannot stat error : No such file or directory error
this is my error
and this is my code
#!/bin/sh
printf "change word from read \n"
read a
read b
mv ${a} ${b}
printf "${b}\n"
How can I solve it?
回答1:
The file you are trying to mv does not exist.
You can check if it exists with:
if [ ! -f "$a" ]
then
echo "File $a does not exist"
exit
fi
回答2:
I rewrote your script a bit to check that the file exists and that the directory path will be created if need be. You are receiving your error because a file does not exist. "mv" will not create directories, so anything you enter in there has to be a directory path that exists.
So, instead of your original version:
#!/bin/sh
printf "change word from read \n"
read a
read b
mv ${a} ${b}
printf "${b}\n"
I'd suggest the following:
#!/bin/bash
printf "change word from read \n"
read -r a
read -r b
if [[ ! -f ${a} ]] ; then
echo "Unable to locate ${a}...Exiting"
exit 2
fi
### If you are renaming a file, remove "if" statement below ###
if [[ ! -d ${b} ]] ; then
echo "Directory does not exist...Creating ${b}"
mkdir -p "${b}"
fi
mv "${a}" "${b}"
exit
来源:https://stackoverflow.com/questions/34091346/mv-cannot-stat-error