LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-13-2010, 10:29 AM   #1
SteveInTallyFL
Member
 
Registered: May 2008
Location: Tallahassee, FL
Distribution: RHEL4, RHEL 5, OEL4, OEL5
Posts: 60

Rep: Reputation: 17
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.
 
Old 08-13-2010, 10:39 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
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 [='=]
 
1 members found this post helpful.
Old 08-14-2010, 01:08 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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.

Last edited by konsolebox; 08-16-2010 at 09:39 AM. Reason: forgot to remove maxdepth.. it was only for testing
 
Old 08-16-2010, 08:58 AM   #4
SteveInTallyFL
Member
 
Registered: May 2008
Location: Tallahassee, FL
Distribution: RHEL4, RHEL 5, OEL4, OEL5
Posts: 60

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by grail View Post
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.
 
1 members found this post helpful.
Old 08-16-2010, 09:02 AM   #5
SteveInTallyFL
Member
 
Registered: May 2008
Location: Tallahassee, FL
Distribution: RHEL4, RHEL 5, OEL4, OEL5
Posts: 60

Original Poster
Rep: Reputation: 17
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
 
Old 08-16-2010, 09:20 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
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
 
Old 08-16-2010, 09:31 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

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


Reply

Tags
rhel4, scripting, tr


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] escaping a single quote from a bash script atbrew Programming 8 07-21-2010 09:21 AM
MySQL imports csv file and appends quote marks - how to remove? tensigh Linux - Software 6 03-04-2010 05:37 PM
when to quote or not to quote variable names - mktemp problem glinux Linux - Newbie 3 02-20-2009 04:37 PM
HELP! Script to RENAME/REMOVE ( ) From File NAMES!! xberetta21 Linux - Newbie 4 01-29-2008 01:10 PM
insert a single quote is text file with sed eln01 Programming 7 05-10-2007 11:02 AM


All times are GMT -5. The time now is 03:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration