Results 1 to 10 of 17
I am new with bash scripts, but I managed to take a filename and replace the characters I do not like (in a variable).
At the end of the script ...
- 07-03-2007 #1Just Joined!
- Join Date
- Aug 2006
- Posts
- 49
Bash script to rename files
I am new with bash scripts, but I managed to take a filename and replace the characters I do not like (in a variable).
At the end of the script when I try:
/bin/mv $file1 $file2
I get an error...
If I try:
echo /bin/mv $file1 $file2
I can paste that output to my terminal, and WOW, it works.
The input files do contain spaces (which is one reason I'm renaming them
), and the error msg is about the '\' characters I had to include.
Any help would be greatly appreciated.
- 07-03-2007 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
What's the error you're getting?
EDIT: It sounds like you have hidden characters that causes the command to fail but don't show up in the output. That would be why copy/paste would work.
- 07-03-2007 #3Just Joined!
- Join Date
- Aug 2006
- Posts
- 49
Sorry, my error message:
/bin/mv: invalid option -- \
Try `/bin/mv --help' for more information.
That's all.
- 07-03-2007 #4Linux User
- Join Date
- Jun 2007
- Posts
- 318
My mistake as well. I should have said to list the command being executed and the error message.
The message '/bin/mv: invalid option -- \' indicates that mv somehow thinks the backslash is an option. Make sure you have this as the 1st line in the script:
#!/bin/bash -vx
The '-vx' will cause bash to list everything. Then post the command and error message.
- 07-03-2007 #5Just Joined!
- Join Date
- Aug 2006
- Posts
- 49
Wow, that's a lot of output...
/bin/mv ${str1}.mp3 ${str2}.mp3
+ /bin/mv 'Tool\' '-\' '10\,000\' 'days\' '\[01\]\' vicarious.mp3 Tool_-_10-000_days_01_vicarious.mp3
/bin/mv: invalid option -- \
Try `/bin/mv --help' for more information.
Hope that's enough
- 07-03-2007 #6Just Joined!
- Join Date
- Aug 2006
- Posts
- 49
This is from the output too:
str1='Tool\ -\ 10\,000\ days\ \[01\]\ vicarious'
str2=Tool_-_10-000_days_01_vicarious
I don't get why str1 has quotes around it, and str2 doesn't
- 07-03-2007 #7Linux User
- Join Date
- Jun 2007
- Posts
- 318
How big is the total output. If not too big then post it or at least the command that assigns str1, the before and after expansion. Your post of the mv command shows the before and after (lines starting with + is after expansion):
/bin/mv ${str1}.mp3 ${str2}.mp3
+ /bin/mv 'Tool\' '-\' '10\,000\' 'days\' '\[01\]\' vicarious.mp3 Tool_-_10-000_days_01_vicarious.mp3
Or post it as an attachment. The output will need a file extension of .txt.
- 07-03-2007 #8Just Joined!
- Join Date
- Aug 2006
- Posts
- 49
Ok, here's the whole output:
- 07-03-2007 #9Linux User
- Join Date
- Jun 2007
- Posts
- 318
OK, I see your adding backslashes to the 1st string by parsing the string 1 char. at a time. If you look at this segment you'll see:
+ temp=l
+ '[' l == ' ' ']'
+ '[' l == , ']'
+ '[' l == '[' ']'
+ '[' l == ']' ']'
+ str=Tool
+ let count=count+1
+ '[' 4 '!=' 33 ']'
+ temp=' '
+ '[' ' ' == ' ' ']'
+ temp='\ '
+ '[' '\ ' == , ']'
+ '[' '\ ' == '[' ']'
+ '[' '\ ' == ']' ']'
+ str='Tool\ '
In otherwords, when it comes across a space and tries to replace it with '\ ', that's when the quotes are added to the string.
Try enclosing the command that appends the character with double quotes:
str="${str}$temp"
See if that helps. I can try to explain why this is happening later.
- 07-03-2007 #10Just Joined!
- Join Date
- Aug 2006
- Posts
- 49
Tried it, same error message.
Nuts.


Reply With Quote