Advertisement
rccharles

Untitled

Jan 8th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.99 KB | None | 0 0
  1.     #!/bin/bash  
  2.      
  3.    # so anyway, output from the echo statement is sent to the terminal.  The operands < and >
  4.    # allows you to change where it is going.
  5.    
  6.     # default redirection  
  7.     # 0 stdin  -- standard input
  8.     # 1 stdout -- standard output, non-error output
  9.     # 2 stderr  -- standard output for error messages.
  10.      
  11.     # had not used the exec statement before, the concept of the exec was
  12.     # readily apparent to me.
  13.    
  14.     # of course, I ripped this off.  Turns out you want to preserve where input stream 0 is
  15.     # coming from. The hack moves input stream 0 to input stream 6.  Pick any number
  16.     # that is allowed.
  17.    
  18.     # save standard input in file descriptor 6  
  19.     exec 6<&0  
  20.    
  21.     # now we say where we want input stream 0 to come from.  
  22.     # bash has an unknown to me number of commands that read from input stream 0
  23.     # via redirection we can change were stream 0 is coming from.
  24.     # redirect standard input to be from the passed file  
  25.     exec 0<${1}  
  26.      
  27.     count=0  
  28.     while read fileName  
  29.     do {  
  30.      
  31.          # bash syntax is bizarre.    
  32.          # The (( )) says it's a math expression  
  33.          (( count++ ))  
  34.          echo
  35.          echo "${count}: ${fileName}"
  36.      
  37.    
  38.          #  Pick output file name.
  39.            baseName="${fileName##*/}"
  40.            
  41.            # a directory name needs to have a / in it.
  42.            # wild stuff
  43.            # see: http://stackoverflow.com/questions/229551/string-contains-in-bash by Adam Bellaire
  44.            if [[ ${fileName} == *"/"* ]] ; then
  45.            echo "It's there!";
  46.            directoryName="${fileName%/*}/"
  47.          else
  48.            directoryName=""
  49.          fi
  50.          
  51.            # possible extension
  52.            # Why didn't I use the wild stuff?  Because I copied the code I wrote before.
  53.            extension="${baseName##*.}"
  54.                
  55.                # Was an extension found?
  56.                if [ "${extension}" != "${baseName}" ] ; then
  57.                     # extension found, since a period was in the data
  58.                     name="${baseName%.*}"
  59.                else
  60.                     name="${baseName}"
  61.                fi    
  62.                
  63.          echo "directoryName is '${directoryName}'"
  64.          echo "baseName is '${baseName}'"
  65.          echo "extension is '${extension}'"
  66.          
  67.         # what we all have been waiting for.
  68.         echo "ffmpeg -i" "${fileName}" "${directoryName}${name}.ext"
  69.  
  70.  
  71.  
  72.         }  
  73.     done  
  74.      
  75.     # put the input streams back to where they were before.  
  76.     # Nice, but when the script quits they will all be put back to where they were.
  77.     # Of course, I copied this stuff.  Syntax isn't the obvious.
  78.     # close input.  May not need.  My innovation.  Seems nice.
  79.     exec 0<&-  
  80.     # restore file descriptor 0 should be standard in  
  81.     exec 0<&6  
  82.     # close file descriptor 6 for reuse.  
  83.     exec 6<&-  
  84.      
  85.     echo  
  86.     echo "Total lines were ${count}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement