I think my script does what its meant to do, at first I just had
#!/bin/bash
for file in /home/parallels/Desktop/trashcan/*
do
echo "Would you like to delete - " $file
done
I then wanted to add the obvious missing functionality so I now have
#!/bin/bash
for file in /home/parallels/Desktop/trashcan/*
do
echo "Would you like to delete - " $file
read line
if [$line == y|Y]
sudo rm $file
fi
done
Thats where I'm at now, I did at first try to use a case statement instead of the if as I have a working script with the case statement I'd need but simply copying it over gives me the same error - syntax error near unexpeted token, I get this for fi and done
3 Answers 3
[
is a command, so it must be separated by whitespace from its first argument:
if [ "$line" = y ] || [ "$line" = Y ]; then
sudo rm "$file"
fi
If you are using bash
, you can replace the standard usage shown above with the more concise
if [[ $line = [yY] ]]; then
sudo rm "$file"
fi
As Keith Thompson pointed out, only an answer of exactly 'y' or 'Y' will allow the file to be removed. To allow 'yes' or 'Yes' as well, you can use
shopt -s extglob
if [[ $line = [yY]?(es) ]]
(The shopt
line is only required for earlier versions of bash
. Starting with version 4, extended patterns are the default in a conditional expression.)
The if part should be
if [ "$line" = "y" ] || [ "$line" = "Y" ]
then
sudo rm $file
fi
Comments
I faced similar problem. I had opened the .sh file in windows and Windows has added CRLF at the end of every line.
I found this out by running
cat --show-nonprinting filename.extension
E.g.
cat --show-nonprinting test.sh
Then, I converted it using
dos2unix filename.extension
E.g.
dos2unix myfile.txt
dos2unix myshell.sh
[
and before]
. And I'm not sure if the|
or will work. Could you just try[ $line == y ]
?