mv: cannot stat error [closed]

不想你离开。 提交于 2019-12-12 03:09:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!