Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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.

  2. #2
    Linux 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.

  3. #3
    Just 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.

  4. #4
    Linux 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.

  5. #5
    Just 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

  6. #6
    Just 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

  7. #7
    Linux 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.

  8. #8
    Just Joined!
    Join Date
    Aug 2006
    Posts
    49
    Ok, here's the whole output:
    Attached Files Attached Files

  9. #9
    Linux 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.

  10. #10
    Just Joined!
    Join Date
    Aug 2006
    Posts
    49
    Tried it, same error message.

    Nuts.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...