Solved

Linux Shell Scripting: Override mime values for specified extensions

Posted on 2013-11-11
9
300 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 78

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 78

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure 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 78

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 78

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

730 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