1

I am having trouble to run this code, Anyone have a thought

#! /bin/bash
while :
do
   echo "Enter file name along with absolute path : "
   read -r inputFile
   echo "Enter path you would like to save copy of the file : "
   read  -r pathinput
   path=$pathinput


if ((-f "$inputFile" ; -d "$pathinput" ))
then
    cp -p $inputFile $path
    break
else
    echo "File does not exist. Try again."
fi
done
echo  " Job Done"

1 Answer 1

0

2 things catch my attention:

  • if ((-f "$inputFile" ; -d "$pathinput" )) seems a mix between bash arithmetic and tests. What you are trying to do is : if [[ -f $inputFile && -d $pathinput ]]
  • cp -p $inputFile $path VS cp -p "$inputFile" "$path" Use more quotes !

Learn how to quote properly in shell, it's very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

Think of using https://www.shellcheck.net/ at a first glance when you have a shell issue

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.