Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

Linux Shell Scripting: Override mime values for specified extensions

I use this to get the mime type and character set of a file:
for f in *
do 
  mime=`file -ib $f`
  echo $mime
done

Open in new window

I want to be able to override the mime type but not the character set for files with the following extension:

.xml (application/xm)
.js (application/javascript)
.json (application/json)
.jsonp (application/javascript)
.css (text/css)

Files with all other extensions should use the value deturmined by "mime=`file -ib $f`".
Avatar of arnold
arnold
Flag of United States of America image

Check the file extension and based on that either query using file, or create the process.

Which shell is the script for sh, bash, ksh, zsh?

There are many pattern matching examples for the various shells that you can determine the extension of the filename and act according to your spec as well as use the mime response to alter its info
I.e if test "$mime" = "text/xml"; then
         mime="application/xml"
fi
Avatar of hankknight

ASKER

How can I do this with multiple if/else statements?  This does not work:
for f in *
do 
if test ${f##*.} = "css"; then
         mime="text/css"
else if test ${f##*.} = "js"; then
         mime="application/javascript"
else 
         mime=`file -ib $f`
fi
done

Open in new window

you can update the mime reference for the file system wide. /etc/mime.types usually. Note changes here may impact other applications running on the system if the alterations you want are "unique" to your needs rather than the standard.

you could use an array
array['xml']="application/xm"
array['js']="application/javascript"
array['json']="application/json"
array['jsonp']="application/javascript"
array['css']="text/css"

 
suffix=${f##*.}
if (test ( "$suffix" = "css" ) -o "$suffix"="xml" -o "$suffix"="js" -o "$suffix"="json" -o "$suffix"="css" -o "$suffix"="jsonp"; then
      mime="$array[$suffix]"
fi
Thanks but I still don't know how to implement the "else".  I need to use the standard mime as a fallback.  This does not work:
array['xml']="application/xm"
array['js']="application/javascript"
array['json']="application/json"
array['jsonp']="application/javascript"
array['css']="text/css" 
for f in *
do 
suffix=${f##*.}
mime=`file -ib $f`
if (test ( "$suffix" = "css" ) -o "$suffix"="xml" -o "$suffix"="js" -o "$suffix"="json" -o "$suffix"="css" -o "$suffix"="jsonp"; then
      mime="$array[$suffix]"

fi
done

Open in new window

SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You don't need a complex if/elif/else test sequence.  If you try to use a non-existent element of an array in bash, you get an empty string back.  So, just add the suffixes you want to be replaced to the array, and if you have a different suffix, you will get the empty string.  You do need to tell the shell that you are using an associative array (declare -A), and you need curly braces round the name when getting values from the array.

We also need to add the "charset" value into the manually-set mime values.

#!/bin/bash

declare -A array
array['xml']="application/xml"
array['js']="application/javascript"
array['json']="application/json"
array['jsonp']="application/javascript"
array['css']="text/css"

for f in *
do
  suffix=${f##*.}
  echo $suffix
  mime=${array[$suffix]}
  if [ "$mime" = "" ]; then
    # Mime type not one of the manually set ones - use normal value
    mime=`file -ib $f`
  else
    # Mime type manually set - now add charset value to the string
    mime="${mime}$(file -ib $f | sed 's/^[^;]*//')"
  fi
  echo $f $mime
done

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, the default action in the simon3270 can be mime=$(file -ib $f)

*) mime=$(file -ib $f) ;;
Hi @arnold - If you put mime=$(file -ib $f) as the default action, there is no easy way to tell whether you have the "; charset=XXX" bit in the mimetype.  I intended this case statement to replaces lines 12, 13 and 14 in my first example, to set the mime variable for known filetypes, then lines 15 to 21 would either add the charset string to those known values, or to call "mime -ib" to get the full mimetype for other file extensions.

Edit: Well, there are many "easy" ways, but none quite as easy as "$mime" = ""   :-)