Shell script blog -OR- Give me tips on how to write better scripts!

Hi folks,
I don't write a lot of shell scripts, and the ones that I do write are usually quite short, so as a learning exercise I made a shell/CGI blog script.. And I was hoping that some of the shell scripting experts in the community could give me some pointers on form, style, etc! The script works just fine, but maybe I haven't coded it very elegantly. Or maybe in some places I haven't necessarily used the right (or most appropriate?) commands to accomplish what I want.
It's a simple blog. It can either:
- Display a blog post (by default the newest, unless the name of a post is given)
- Display a listing of posts for a certain tag
Posts are saved in normal files in markdown format. Tagging is accomplished by putting the name of the tag(s) into the filename, separated by hyphens. For example: the file "wonderful_blog_post-whining-computers-archlinux.md" would be a post named "wonderful blog post", and it would have three tags ("whining", "computers", "archlinux"). Maybe it's not the best idea in the world... but I went with it anyhow
I put the script on pastebin for you all to see: http://pastebin.com/L8nMpUjT
Please do take a look and let me know if I've done something badly! Hopefully I can learn a thing or two from your comments.
And there's a working example of it here: http://www.ynnhoj.net/thedump/s.cgi

falconindy wrote:
To sort files by modification time (safely) requires GNU stat and some bash:
declare -a files
while IFS=$'\t' read -r -d '' ts file; do
files+=("$file")
done < <(stat --printf '%Y\t%n\0' * | sort -zn)
printf "%s\n" "${files[@]}"
The array is of course unnecessary as you could print inside the loop, but I see no way around the usage of null delimiters and bashisms (read's -d flag in particular) to be 100% safe.
Thanks for the example, though I'm trying to avoid bash and write my script in posix shell. So after some tinkering, this is what I've come up with to make a simple list of blog post file names:
#!/bin/sh
cd posts
while IFS=$'\t' read -r ts file; do
ALLPOSTS="$ALLPOSTS $file"
done << EOF
$(stat --printf '%Y\t%n\n' *.md | sort -rn)
EOF
echo $ALLPOSTS
So I suppose that it isn't 100% safe - but could it be acceptable? Is there anything else I could do?
Edit: as it turns out, my server is running some flavour of BSD, I think FreeBSD. So the above examples don't work with FreeBSD's stat. Ah well..
Last edited by upsidaisium (2011-02-23 08:26:29)

Similar Messages

  • Tips on how to write efficient  java code for java mapping

    hi
    I do not have much knowledge in Java
    Can anybody tell me some tips on how to write efficient and optimised java code to be used in java mapping
    Thanks,
    Loveena

    hi D'za,
    JAVA in xi
    A very important place where you will use JAVA in XI is while doing your Mapping. There will be cases when JAVA MAPPING is the best solution to go for. There are 2 types of Parsers available for JAVA Mapping. DOM Parser and SAX parser. Just got through the following links to understand more on Java Mapping and the APIs available.http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/package-summary.html http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Document.html http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/package-frame.html /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-i
    /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-ii /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-iii
    JAVA mapping -
    /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-i /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-ii /people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-iii /people/ravikumar.allampallam/blog/2005/06/24/convert-any-flat-file-to-any-idoc-java-mapping /people/amol.joshi2/blog/2006/03/10/think-objects-when-creating-java-mappings /people/sameer.shadab/blog/2005/09/29/testing-abap-mapping
    sample code for java mapping
    Re: Example code DOM PARSER API -
    http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/package-frame.html DOM --- /people/thorsten.nordholmsbirk/blog/2006/08/10/using-jaxp-to-both-parse-and-emit-xml-in-xi-java-mapping-programs tutorial sax and dom
    For a tutorial on the methods of SAX and DOM http://java.sun.com/webservices/docs/1.1/tutorial/doc/
    SAX AND dom PARSER ( BY thorsten) -
    example /people/thorsten.nordholmsbirk/blog/2006/08/10/using-jaxp-to-both-parse-and-emit-xml-in-xi-java-mapping-programs java mapping example ( testing and debugging) /people/stefan.grube/blog/2006/10/23/testing-and-debugging-java-mapping-in-developer-studio
    regards
    biplab
    Use a Good Subject Line, One Question Per Posting - Award Points

  • I am new in indesign scripting, please tell me how to write a script to get  content of a element in xml and then sort all the content

    I am new in indesign scripting, please tell me how to write a script to get  content of a element in xml and then sort all the content

    Hi,
    May the below code is useful for you, but I dont know how to sort.
    Edit the tag as per your job request.
    alert(app.activeDocument.xmlElements[0].contents)
    //Second alert
    var xe = app.activeDocument.xmlElements[0].evaluateXPathExpression("//marginalnote");
    alert(xe.length)
    for(i=0; i<xe.length; i++)
        alert(xe[i].texts[0].contents)
    Regards
    Siraj

  • How to write Java Scripts in Apex

    Hi All,
    Can any on tell me how to write Java Scripts in Apex.
    Thanks in Advance,
    Mukund

    If you want to link your custom JavaScripts to an Apex page, you have several options:
    1. Add an HTML Region on Page 0 (Script will be written to every page) called something like "JavaScript & CSS". In this region add your Script to the region source like this:
    <script language="JavaScript">
    function hell ()
    alert ("Hello World");
    </script>
    2. Add a similar region to the page you are working on (can only be accessed on this page)
    3. Create an external JavaScript file and either load it into the WORKSPACE or into the images (/i/) directory on the server. You can then link the JS into you page via modifying the Page Template.
    Hope that makes sense. Let me know if you want any further help with option 3.
    Duncs

  • How to write the script to watch InDesign actions?

    Hi All,
    I'd like to write a script or a .Net tool to watch the InDesign actions and alert the user for any deviations like the option 'Live Preflight' in InDesign CS4? Pl. suggest.
    Thanks,
    Praveen

    Hi Alex,
    Nice reply, I need one more clarification. when installing OFT 8.5 software do we require any other software or any other set or funda for that. I am able to add some vbscript in test scrplet but when execution comes to that point node its getting halted and result is getting failed.
    can i take any precautions or any hierarchy or guidelines i need maintaing before writing the script on the scriplet.
    please give me the procedure. sorry for repeatedly asking as it's very important i have been raising the same question from couple of days.
    code i have writte over there
    msgbox("anjani")
    dim a=10
    dim b=20
    dim c=a+b
    msgbox c
    pls pass your mail id we can discuss later also my id is [email protected] plssssssssss
    anjani

  • How to write a script to ask the streaming video (FLV) to do something after it is finised playing...

    How do I write a script to ask a streaming video to go to
    play a next movie clip or to do something after it is finised
    playing automatically?

    follow the link here to get the fla example which I have just
    embeded a movie file straight on the root timeline and then I add
    another layer for scripting. look at the last frame of the timeline
    where I have add a very simple gotoAndPlay script.
    hope ths is what u looking for. otherwise you really need to
    get flash 8. or hopefully someone coming along on this forum and
    solve the MX problem for you.
    good luck.
    http://www.lauthiamkok.net/scripting_languages/ActionScript/Flash_8/video%20object/
    get this file - embeded video.fla

  • Need some help being able to stream movies.  I use to be able to  watch Netflicks - streaming.  However, I now cannot.  The message I get is:  plug in failure.  Can anyone give a tip on how to correct this?

    Up until recently I was able to watch Netflicks movies thru streaming.  I now received a error message:  plug in failure.  Can anyone suggest how I can correct this?

    I had the same problem.  I checked with Netflix and they told me to uninstall and reinstall the silverlight? plug in.  We went through several versions of this.  Long story short, nothing worked, but it DID run just fine on other users on the same machine.  a couple of months later, BAM it suddenly works perfectly again.  Netflix blames Apple, Apple blames netflix... who knows...
    Here's my advice: download the Silverlight or whatever it's called plugin and do the uninstall, then reinstall.  If that works, great!
    If not, make up a new user and see if netflix plays from that account.  If so, you have a workaround that is tedious, but at least lets you watch your movies!  Someday in the future,it might just start working again.

  • How to write the script in E-tester

    Hi i am new to e-tester. we are doing some R&D on empirix e-tester tool. We are familiar with QTP tool. we are able to record and play the script but we are unable to enhance the script.
    But when i went through this threads i came to know that we can use VBA script or VB script or etc for enhancing the script. current we are using "oracle Functional Testing for web applications 8.50.0260" version.
    Can any body please guide me and explain the procedure for enhancing the script and what is the use of open script.
    Is it required for e-load or e-tester. pls specify me its very urgent
    thanks inadvance...........pls

    Hi Alex,
    Nice reply, I need one more clarification. when installing OFT 8.5 software do we require any other software or any other set or funda for that. I am able to add some vbscript in test scrplet but when execution comes to that point node its getting halted and result is getting failed.
    can i take any precautions or any hierarchy or guidelines i need maintaing before writing the script on the scriplet.
    please give me the procedure. sorry for repeatedly asking as it's very important i have been raising the same question from couple of days.
    code i have writte over there
    msgbox("anjani")
    dim a=10
    dim b=20
    dim c=a+b
    msgbox c
    pls pass your mail id we can discuss later also my id is [email protected] plssssssssss
    anjani

  • How to write Test Script for Rossettanet PIP message to test in RSTK tool.

    Hi All,
    I am new to RSTK i came to know that we need to prepare a test script and place in a Tomcat Server folder, to test any PIP message. Please let me know how we can prepare this Test Script.
    Thanks alot in advance.
    Srinivas.
    Edited by: sri_rambo on Oct 27, 2009 8:08 AM
    Edited by: sri_rambo on Oct 27, 2009 11:34 AM

    Hi,
       Did you check this blog?
    http://www.sdn.sap.com/irj/scn/weblogs;jsessionid=(J2EE3417300)ID1594031950DB00395255272980057387End?blog=/pub/wlg/3826
    and this:
    /people/shabarish.vijayakumar/blog/2008/04/28/the-rosettanet-stk-and-how-to-use-it-to-your-advantage
    Regards,
    ravi

  • How to write Java script for refering the 1st page text field from 2nd page?

    Hello I am developing an VISITOR info interactive form with webdynpro ABAP, its with 2 pages. Their heirarchy is,
    VISITOR_form
        |
        |
        |------Page_1
        |            |
        |            |
        |            |------Text_Field_A_1
        |            |
        |            |------Text_Field_A_2
        |
        |------Page_2
                     |
                     |
                     |------Text_Field_B_1
                     |
                     |------Text_Field_B_2
    Now, i have selected the 'Text_Field_B_1' of page_2 and writing the Java script, my requirement is; If Page_1's 'Text_Field_A_2' has a value of "Value_ABCD", THEN the 'Text_Field_B_1' of page_2 should be 'readOnly'. Pls let me know Wht is the Java script for this requirement?

    if (xfa.event.newText=="ABCD")
    VISITOR_form.Page_2.Text_FieldB_1.access ="readOnly";
    place script on change event of Text_FieldA_2

  • How to write this script?

    on release of up_btn I need to do the following.
    got to contents_mc > .color_mc2 >.gotoAndStop (frame 5)
    How do I script it?
    * The up_btn is inside "contents_mc" and I need to goto _root > contents_mc > color_mc2 and goto FRAME 5 by pressing the "up_btn".
    Hope this makes scence.

    up_btn.onRelease=function(){
    color_mc2.gotoAndStop(5);

  • How to write SAP Script for AP/AR?

    Hi,
    I would like to know how to prepare a SAP Script for AP/AR. Can anybody help.
    Thanks
    ST

    Hi,
    I would like to know how to prepare a SAP Script for AP/AR. Can anybody help.
    Thanks
    ST

  • How to write a script to cycle through a list of search terms and download PDF files

    I need to download ~2500 PDF files.
    Each one is accessible by entering a search term on a website and clicking a link in the search result to download the PDF.
    How can I create an automated process to do the following?
    (1) select the first search term from a list in Excel
    (2) Enter it into the search field on a specified website
    (3) Download a PDF file from a link in the search results.
    (4) Repeat using the next search term in the list

    I need to download ~2500 PDF files.
    Each one is accessible by entering a search term on a website and clicking a link in the search result to download the PDF.
    How can I create an automated process to do the following?
    (1) select the first search term from a list in Excel
    (2) Enter it into the search field on a specified website
    (3) Download a PDF file from a link in the search results.
    (4) Repeat using the next search term in the list

  • How to write a script for adding a percentage?

    I have a simple form where I need a cell to automattically add a percentage to a different cell.
    For example in Excel my expression is: =SUM(E9*0.014)  [90*1.4%= 1.26] where as Cell E9 will be the amount I need the percentage to added to. 
    Header 1
    Header 2
    Number
    Surcharge
    90
    =sum(90*.014)
    Thanks,

    Moving this discussion to the Javascript since USA Fire's other inquiry was located in this forum.

  • Can I write a script to automatically clean out mail messages on server?

    I have a serious mail.app difficulty where the program does not successfully erase all messages on my mail server.
    Can anyone give me a hand on how to write a script that would automatically delete all already downloaded messages from two POP servers?
    best regards.

    The issue is knowing what's already been downloaded - the mail server doesn't keep track of that, only the mail client.
    That said, it should be possible to do - at least to delete all mail, but it might not be necessary. In most cases I've seen the mail client fails to delete messages because your scheduled check is too short - i.e. if you're set to check once per minute but you have so much mail on the server it takes 2 minutes to delete them all, the mail client fails. If that's the case just turn down your schedule to check less often (e.g. every 10 or 15 minutes) so that it has time to delete the messages.

Maybe you are looking for

  • Oracle 9.2.0.6 and kernel 7.0 ?

    Can Oracle 9.2.0.6 run with kernel 7.0? Or does only Oracle 10g run with kernel 7.0? Can I upgrade the kernel from 640 to 7.0, then later upgrade Oracel from 9.2.0.6 to 10g?

  • I went back to Outlook to change some files, but they didn't change in TBird. How do I update w/o making the same changes in TBird?

    I've been using TBird for a month to test it out for our company. Late Friday, I then went back to my Outlook account and changed some files--moved some files into another existing folder and then deleted the old folder. When I went back to TBird aft

  • Should I remove listeners?

    I don't understand if it is my responsibility to remove every listeners associated to a variable, before I set it to null. JTable aTable = new JTable( new SomeTableModel() ); aTable = new JTable( new SomeTableModel() );Even if this example isn't very

  • XMII Portal Login Page

    Hi , I am unable to view the login screen for the xMII portal. I tried to access the login page from the system where the xmII server is installed. When i type in the url -- http://<server>/Lighthammer/Login.jsp it directs me to the menu page without

  • Toolbar button for a calculator?

    My client says that on her last computer, she had a button on the Acrobat toolbar that would open a calculator.  It may have been Acrobat X or Xi.  She is now using Xi, and I can't find an option for that.  Does anyone know how to make that happen?