Search code examples
bashshelluser-input

commands after read in shell script gives error


I am creating shell script to take the input path from user and copy a particular file to inputted path. Below is simple script to recover corrupted file and copy it to required path given by user

FILE=$1
bzip2recover $FILE
find . -name 'rec*bz2' -exec sh -c 'bzip2 -t "$0" 2>/dev/null || rm -f $0' {} \;
cat rec* > $FILE
rm -f rec*
read -p "Enter your path to copy :" PATH
cp $FILE $PATH
rm -f $FILE
lines after read gives error like cp : command not found and rm : command not found. What could be the problem ?


Solution

  • You are overwriting crucial system variable PATH with user's input. PATH normally points to folders with executables. After you overwrite it with user input, system is no longer able to find executables.

    Use some different variable name.