This script should copy all files of which the names are provided as arguments on the command line to the users home directory. If no files have been provided, the script should use read to ask for file names, and copy all file names provided in the answer to the users home directory.
if [ -z $1 ]
then
echo provide filenames
read $FILENAMES
else
FILENAMES="$@"
fi
echo the following filenames have been provided: $FILENAMES
for i in $FILENAMES
do
cp $i $HOME
done
If I provide arguments as string - it works. But if I provide as "read $FILENAMES" - it has not working.
Mentor shows same solution in his lessons but did not shows how it works.
UPDATE After I entered the filenames as arguments it gives empty strings and did not copy files to $HOME location
[dmytro@oc1726036122 ~]$ cd Desktop/
[dmytro@oc1726036122 Desktop]$ . totmp
provide filenames
one two
the following filenames have been provided:
the following filenames have been provided:
[dmytro@oc1726036122 Desktop]$
read FILENAMES
(no$
). Aside from the error, there are a number of other issues with the mentor's solution - it will fail for filenames that contain whitespace for examplereadline
issue,cp $i $HOME
should readcp "$i" "$HOME"
to make sure you handle whitespace correctly. Always try to quote Bash variables.for
loop will already have tokenized the$FILENAMES
string on whitespace