0

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

MPelletier
16.7k18 gold badges89 silver badges140 bronze badges
asked Aug 1, 2013 at 17:50
2
  • 1
    I believe you need spaces behind [ and before ]. And I'm not sure if the | or will work. Could you just try [ $line == y ]? Commented Aug 1, 2013 at 17:56
  • With the fix in either of the answers, this will still treat an input of "yes" or "Yes" as "no". Commented Aug 1, 2013 at 18:11

3 Answers 3

1

[ 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.)

answered Aug 1, 2013 at 18:00

1 Comment

THANK YOU! This works perfectly and I've learnt from this mistake, I appreciate the time.
0

The if part should be

if [ "$line" = "y" ] || [ "$line" = "Y" ]
then
 sudo rm $file
fi
answered Aug 1, 2013 at 18:00

Comments

0

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
answered Jun 25, 2014 at 13:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.