views:

34

answers:

3

Hello, I am trying to make a shell script to remove special characters, like {}()!,' etc. So far I have referenced a past question I asked here, however I get a strange error message:

-bash-3.2$ ./test2.sh
./test2.sh: line 7: unexpected EOF while looking for matching `"'
./test2.sh: line 10: syntax error: unexpected end of file

test2.sh

#!/bin/bash
shopt -s nullglob
for file in *
do
        if [ -f "$file" ]; then
        newfile="`echo $file | tr -d '[{}(),\!]' | tr -d "\'" | sed 's/_-_/_/g'`"
        mv "$file" "$newfile"
        fi
done

Not sure where I am going wrong on this one, the files are named like:

Folder - 01
Folder 02!
Folder(03)
Folder Four_Three

The desired output would be

Folder 01
Folder 02
Folder 03
Folder Four Three

Thank you in advance.

A: 

You're missing a double-quote in the if test.

Jim Garrison
Thanks for pointing that out! However when I added it and re-ran it, it is still giving me the unexpected EOF error.
eddylol
correction, I got farther, however its not 100% working yet..
eddylol
Worked for me in Cygwin bash. However, the double-quotes surrounding the definition of `newfile` are superfluous and may be conflicting with the double-quotes around the string argument to the second `tr`. Try removing the outer double-quotes.
Jim Garrison
Its strange, it runs, but it doesn't fix any of the folders that are named Folder(03), Folder 02!, etc
eddylol
+1  A: 
shopt -s extglob
shopt -s nullglob
for i in *; do echo "mv $i ${i//[[:punct:]]/}"; done
This is the closest answer so far, it runs the script, appears to run, but when I run ls -la it doesn't show the changes as made.
eddylol
Thank you I got it to work, and it works like a charm!
eddylol
A: 

You are testing whether $file is a file, but you apparently want to rename directories (unless you have non-directory files named "Folder*"). Change your test to:

if [ -d "$file" ]; then
Dennis Williamson