-1

could somebody please help me find the problem with this code? getting this error "mv: cannot stat '~Desktop/RecyclingBin/testtest' : No such file or directory. It does exist and it is in the location ~Desktop/RecyclingBin/testtest

fileName=$1
fileLocation='cat ~/Desktop/RecyclingBin/logs/$fileName
if [ -z "$1" ]
   then
       echo "please enter a valid filename"
   else
       echo "do you want to restore?"
   read ans

   if [ "$ans" =="y" ]
   then 
       mv "~/Desktop/RecyclingBin/$fileName" "$fileLocation"
   fi
fi 
user3437235
  • 92
  • 3
  • 9
  • You are quoting `~`, so it does not get expanded. See http://mywiki.wooledge.org/BashPitfalls#echo_.22.2BAH4.22 – fedorqui Nov 19 '14 at 23:18
  • yeah I've removed that so now I have mv ~/Desktop/RecyclingBin/"$fileName" $fileLocation but my $fileLocation is stored inside a text file with a tilde – user3437235 Nov 19 '14 at 23:27
  • Just say `fileLocation=~"/Desktop/RecyclingBin/logs/$fileName" ` – fedorqui Nov 19 '14 at 23:29
  • but im storing the cat of that file inside that variable since the text file contains the location I want to move the file to – user3437235 Nov 19 '14 at 23:33
  • If the path in the file contains `~` it won't expand if you quote it. Maybe you can say `fileLocation=$(cat ~/Desktop/.../$fileName)` – fedorqui Nov 19 '14 at 23:36

1 Answers1

0

The quotes prevent expansion of ~. Put it outside the quotes:

mv ~/Desktop/RecyclingBin/"$fileName" "$fileLocation"
Barmar
  • 741,623
  • 53
  • 500
  • 612