Solved

Linux Shell Scripting: Override mime values for specified extensions

Posted on 2013-11-11
9
304 Views
Last Modified: 2013-11-12
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`".
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 79

Expert Comment

by:arnold
ID: 39638837
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
0
 
LVL 16

Author Comment

by:hankknight
ID: 39639144
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

0
 
LVL 79

Expert Comment

by:arnold
ID: 39639197
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
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 16

Author Comment

by:hankknight
ID: 39639225
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

0
 
LVL 79

Assisted Solution

by:arnold
arnold earned 200 total points
ID: 39640412
#!/bin/bash

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##*.}
echo $suffix
mime=`file -ib $f`
if /usr/bin/test  "$suffix" = 'css' ; then
     mime="$array[$suffix]"
elif /usr/bin/test  "$suffix" = 'xml'; then
     mime="$array[$suffix]"
elif /usr/bin/test  "$suffix" = 'js' ; then
     mime="$array[$suffix]"
elif  /usr/bin/test "$suffix"='json' ; then
     mime="$array[$suffix]"
elif  "$suffix"='css' ; then
     mime="$array[$suffix]"
elif "$suffix"='jsonp' ; then
      mime="$array[$suffix]"
else
   echo -n ""

fi

echo "$f $mime"
done

Open in new window


this should do what you want.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39640990
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

0
 
LVL 19

Accepted Solution

by:
simon3270 earned 300 total points
ID: 39641011
Another approach would be to use a case statement:
suffix=${f##*.}
mime=""
case $suffix in
    xml) mime="application/xml";;
    js|jsonp) mime="application/javascript";;
    json) mime="application/json";;
    css) mime="text/css";;
    *) : default actions, but nothing needed here;;
esac

Open in new window

then do the 'if [ "$mime = "" ]' bit
0
 
LVL 79

Expert Comment

by:arnold
ID: 39643723
Thanks, the default action in the simon3270 can be mime=$(file -ib $f)

*) mime=$(file -ib $f) ;;
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39643808
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" = ""   :-)
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question