Solved

Linux Shell Scripting: Override mime values for specified extensions

Posted on 2013-11-11
9
295 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 76

Expert Comment

by:arnold
Comment Utility
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
Comment Utility
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 76

Expert Comment

by:arnold
Comment Utility
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
 
LVL 16

Author Comment

by:hankknight
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 76

Assisted Solution

by:arnold
arnold earned 200 total points
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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 76

Expert Comment

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

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

Expert Comment

by:simon3270
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Utilizing an array to gracefully append to a list of EmailAddresses
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 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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now