LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Script to remove single quote that begin and end file names (http://www.linuxquestions.org/questions/programming-9/script-to-remove-single-quote-that-begin-and-end-file-names-826019/)

SteveInTallyFL 08-13-2010 10:29 AM

Script to remove single quote that begin and end file names
 
Made the following bash script named trimsquote:
Code:

#!/bin/bash
IFS=$'\n'
for f in `find .`; do
file=$(echo $f | tr -d [='=])
[ e $f ] && [ ! -e $file ] && mv "$f" $file
done
unset IFS

getting errors:
/usr/bin/trimsquote: line 4 unexpected EOF while looking for matching `''
/usr/bin/trimsquote: line 8 syntax error: unexpected end of file

I got my code from this thread:
http://www.linuxquestions.org/questi...lename-309567/

Environment is RHEL 4 x86_64.

Can someone explain what I missed in this script?
Thanks.

grail 08-13-2010 10:39 AM

I would suggest 2 things:

1. Re-read where you got the script from as you have made some syntax errors

2. Test the first error on the command line to see what happens,eg. what is the output of:
Code:

echo "ran'dom" | tr -d [='=]

konsolebox 08-14-2010 01:08 AM

Try this script
Code:

#!/bin/bash
shopt -s extglob
find . | while read F1; do F2=${F1##+([\"\'])}; F2=${F2%%+([\"\'])}; mv "$F1" "$F2"

'find .' probably needs to be more specific like 'find -type f'?

----- edit -----

The above will not work as there will always be a prefix directory.

Code:

#!/bin/bash

shopt -s extglob

while read FULLPATH; do
        DIR=${FULLPATH%%+([!/])}
        FILE1=${FULLPATH##*/}
        FILE2=${FILE1##+([\"\'])} FILE2=${FILE2%%+([\"\'])}
        echo mv "$DIR$FILE1" "$DIR$FILE2"
done < <(find . -type f)

remove echo in 'echo mv ...' if you think that it's already correct.

SteveInTallyFL 08-16-2010 08:58 AM

Quote:

Originally Posted by grail (Post 4065298)
I would suggest 2 things:

1. Re-read where you got the script from as you have made some syntax errors

2. Test the first error on the command line to see what happens,eg. what is the output of:
Code:

echo "ran'dom" | tr -d [='=]

Had to enclose the target to delete in double quotes to make it work.

SteveInTallyFL 08-16-2010 09:02 AM

Final working code that executes in the current directory (does not recurse):
Code:

#!/bin/bash
for f in *\'*; do
    file=$(echo $f | tr -d "[='=]")
    echo $file
    [ ! -e $file ] && mv "$f" "$file"
done


grail 08-16-2010 09:20 AM

Well just using quotes seemed to work for me:
Code:

echo "ran'dom" | tr -d "'"
Also, if using bash 4+ you could also try:
Code:

#!/bin/bash
for f in *\'*
do
    [[ ! -e "${f/\'/}" ]] && mv -v "$f" "${f/\'/}"
done


konsolebox 08-16-2010 09:31 AM

I thought it was only about quotes that begin and end filenames... that the quotes inside are preserved.


All times are GMT -5. The time now is 03:27 PM.