Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux Shell Scripting: Override mime values for specified extensions

Posted on 2013-11-11
9
Medium Priority
?
311 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 80

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 80

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 80

Assisted Solution

by:arnold
arnold earned 800 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 20

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 20

Accepted Solution

by:
simon3270 earned 1200 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 80

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 20

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

885 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