Announcement

Collapse
No announcement yet.

Trouble Parsing file Names in BASH

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Trouble Parsing file Names in BASH

    I have a couple of bash scripts that are pretty much identical aside from one or two variables. Technically the script works fine

    Code:
    #!/bin/bash
    demux-audio() {
        input="${1}"
        #extension="${input##*.}"
        filename="${input%.*}"
        #output="${filename}"
        output="${input}.aac"
        echo "${input}"
        #echo "${extension}"
        echo "${filename}"
        echo "${output}.aac"
           
        if avconv -i "${input}" -acodec copy "${output}"; then
    	    kdialog --passivepopup "Converting from ${input} complete." 60
    	else
    	    kdialog --passivepopup "Error converting files to AAC." 60
    	return 1  #Change to exit 1 to stop the script on first failure
       fi
    }
    
    for file in ${@} ; do
        demux-audio ${file}
    done
    I have debugged this to the point that I can see file names are not being passed properly. For example, if a file is named test.mp4 and I want the .aac audio, the script works as designed. However, if a file is named Test 1.mp4 it will parse the part of the file name labelled "Test" and fail to parse the space, or if there are symbols in the name, it will fail to parse then. Can some one see any reason this is not passing the entire file name?

    Here is the line from the .desktop file:
    Code:
    Exec=konsole --hold --workdir %F -e demux-aac.sh %U
    OS: Kubuntu 12.10/Windows 8
    CPU: Intel Core i7 2600K
    Motherboard: Gigabyte GA-Z77X-UD5H
    Memory: 2x4GB Corsair Dominator
    Graphics Card: MSI R7770
    Monitor: Dell 2208WFP
    Mouse: Mionix NAOS 5000
    PSU: Corsair 520HX
    Case: Thermaltake Mozart TX
    Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
    Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

    #2
    Certainly not an elegant solution, but see Sysadmin Sunday: parse strings with spaces using shell script for a way to deal with spaces in filenames you need to parse.
    Using Kubuntu Linux since March 23, 2007
    "It is a capital mistake to theorize before one has data." - Sherlock Holmes

    Comment


      #3
      Thanks snowhog, now I glanced over this briefly, so if I misread this let me know and I will try to look through it in more detail shortly. The problem with that solution is the removing of spaces in file names. I want to retain the spaces. Additionally, the problem goes beyond spaces. It included symbols like ! or ( or ), and some times the file names lack a closing ).
      OS: Kubuntu 12.10/Windows 8
      CPU: Intel Core i7 2600K
      Motherboard: Gigabyte GA-Z77X-UD5H
      Memory: 2x4GB Corsair Dominator
      Graphics Card: MSI R7770
      Monitor: Dell 2208WFP
      Mouse: Mionix NAOS 5000
      PSU: Corsair 520HX
      Case: Thermaltake Mozart TX
      Cooling: Thermalright TRUE Black Ultra-120 eXtreme CPU Heatsink Rev C
      Hard Drives: 1x180 GB Intel 330 SSD - 1xWD 1 TB Caviar Black - 1xWD 2 TB Caviar Green - 2xWD 3 TB Caviar Green

      Comment


        #4
        Maybe this will be of assistance: 3.5.3 Shell Parameter Expansion
        Using Kubuntu Linux since March 23, 2007
        "It is a capital mistake to theorize before one has data." - Sherlock Holmes

        Comment


          #5
          You need double quotes. You've got them in the function, but not on the for loop or the call:
          Code:
          for file in "${@}" ; do
              demux-audio "${file}"
          done
          (Note that with double quotes, \ (backslash), ` (backtick)[/b] and $ (dollar sign) can still give trouble, but usually only when entering commands or using eval in scripts; once they're in shell variables, they're ok.)

          I think your desktop file is ok, though they can be a slightly obscure art.

          Regards, John Little
          Regards, John Little

          Comment


            #6
            This might be using a sledgehammer to drive a nail but...

            What about changing IFS during the script run? I've used this method to properly parse a huge text file listing.

            From memory (meaning check it first!):
            IFS=$' \n'

            will cause is to split only on newlines. skipping spaces and tabs.

            IFS=$' \t\n'
            will return it to default. You could just add these to your script.

            Feel free to curse me if I remembered wrong!

            Please Read Me

            Comment

            Working...
            X