Thursday, 6 December 2012

Printing from the Linux command line

To print a file from the Linux command line, use the simple command:
 lpr FILENAME  
It's that easy!

I came across this while trying to print all pdf files in a directory. To do this, the simple bash script you need is:
 #!/bin/bash  
 for file in *.pdf; do  
   /usr/bin/lpr "$file"  
 done  

Save this into a file - I called mine print-all-pdfs.sh. Then make it executable using
 chmod +x print-all-pdfs.sh  

Copy it to the directory you want to run it in, then execute with
 ./print-all-pdfs.sh  

For an additional challenge, can anyone adapt the above script to print all .pdfs from directories recursively? Answers in the comments below!


[References] Bash script adapted from a LinuxForums post.

Wednesday, 5 December 2012

Notes on using Heroku

These are a few common things I find myself looking up when working with Heroku - I update this post if I find myself repeated searching for how to do the same tasks.

Running commands from a folder not linked to Heroku

Simply append --app APP_NAME to any relevant Heroku command:
HEROKU_COMMAND --app APP_NAME  

Deployment

If trying to push to Heroku:
 git push heroku master  
but get a publickey error:
 Permission denied (publickey).  
 fatal: The remote end hung up unexpectedly  
You need to add your public key to Heroku.
 heroku keys:add path_to_publickey  
If you don't have a public key, Heroku can prompt and generate one for you - simply use the command:
 heroku keys:add  

Resetting the application database

 heroku pg:reset DATABASE_URL  
To reset the database without a validation prompt, use:
 heroku pg:reset DATABASE_URL --confirm APP_NAME  

Rake

To run a rake task on Heroku, create the task in app/lib/tasks (Rails), test locally then deploy. To run, use:
 heroku run rake TASK_NAME


If you have any useful commands you regularly use while working with Heroku, let me know in the comments below!

Tuesday, 4 December 2012

How to add a menu item for SPSS in Linux

I keep forgetting where SPSS installs itself - this .desktop file may be of help to others too.

Create .desktop file for SPSS:
 sudo leafpad /usr/share/applications/spss.desktop  

Add this text to that file:
 [Desktop Entry]  
 Name=SPSS  
 GenericName=SPSS  
 Comment=Statistics program  
 Exec=/opt/IBM/SPSS/Statistics/19/bin/stats  
 Icon=gnome-monitor  
 Terminal=false  
 Type=Application  
 Categories=Application;Education;  

Save, then log out and back in again, or restart the desktop using
 xfdesktop --reload  
if using an XFCE desktop.

 SPSS will now be available under Applications -> Education.

Monday, 26 November 2012

Find all files and move to a target directory from the Linux command line

For this example, we're going to be moving all .mp3 files from the subdirectories of the current directory into current directory.

To do this, we need to combine two different commands. To find all the files in the current directory and its subdirectories, we use the find command:
 find -name \*.mp3  

The \ escapes the *, so it's interpreted as a wild card on the command line. The -name argument allows us to check only the file names.

To move files around on the command line, we use the mv command, and for our example, we add the -t argument, which lets us define the target directory:
 mv -t <target-directory> <file-to-move>
where target-directory is where we want to move the file to, and file-to-move is the file we want to move.

With a bit of command-line magic, we can combine these commands using the -exec switch on find, to give us our final command:
 find -name \*.mp3 -exec mv -t . {} \+  
-exec takes the command we want to execute, and {} \+ appends each find result to the command, which in our case is mv. So the above line will find all files end in .mp3 and move them to the current directory.

Monday, 19 November 2012

Fail Fast - A Lesson In Not Wasting Time


fail fast




Around a year ago, I had an idea for a small browser based application that had the potential to smooth and streamline a workflow within universities around the country. The concept was simple and wouldn't have taken a huge amount of time to develop. Given my position working in a university setting, I had the ideal environment to test and prototype, but due to constraints on my time I didn't take the concept any further.

Fast forward to last week. I'd finally found the inspiration, motivation, and time to solidify and explore this idea. It took me less than an hour to map out the specifics of my concept, and email a few academic contacts to see what they thought of the idea. Feedback was positive; I'd found a niche. Unfortunately, the very same week, the university have announced that they are trialling a new, much larger system which implements a substantial feature list, and includes the subset of functionality I was aiming to provide.

Why do I tell you this story? Well, there's lessons here. Despite my initial disappointment, this is a perfect example of failing fast and learning quickly. While there was a part of me that felt I shouldn't release my idea into the world until I had built the application and had a full, perfect implementation to demonstrate, had I done this I would have committed hours, days, or weeks of my time to building a system that had zero chance of ever being used. Here's what this experience taught me:
  1. Talk about your ideas. It's natural to feel protective of your concepts, and worry that they might be stolen or plagiarised (or laughed at - see 3.). In my experience, it's highly unlikely that they will, and if they do, talking to enough people makes it clear where the idea originated. The knowledge and insights you stand to gain from sharing knowledge and ideas far outweigh any perceived risks of collaboration.
  2. Act Early. If you have an idea, or discover a niche you could occupy, act on it early. If I'd had the confidence to explore my concept a year earlier, I'd have been first to market, so to speak, and would have been in a much better position to compete with the larger system that has come along now. Who knows, I might have gained enough traction to have become that larger system.
  3. Fight your inner perfectionism. The world is imperfect, and so your contributions to it can be too. Don't spend forever perfecting designs, ideas and implementations. However scary it might be, put yourself and your ideas out there, and you'll be pleasantly surprised at the what happens.
So there we have it, it took me less than a day to establish that, despite a market need, my good idea wasn't commercially viable in current circumstances. My time is now free to explore my next project. Next time you find yourself thinking "maybe there's a need for this... one day I'll have the time..." - take the time to explore, find feedback and fail fast or fly.


[Image credit] http://thumannresources.files.wordpress.com

Friday, 16 November 2012

Finding out what Matlab tooboxes are available for use

Finding out what toolboxes are available to a Matlab installation is actually relatively simple - it's just a case of typing:
 ver  

at the Matlab prompt.

On it's own, however, this function doesn't supply much useful information, as it doesn't tell you what licences are available to the installation, and hence which toolboxes you can actually use (which is probably what you care about when you're trying to find out what toolboxes are available).

It is possible to find out what licenses are available to the installation, however it takes a bit more work.

First, we need to define a feature string, which is just a list of all the toolboxes potentially installed:
 featureStr = {'Aerospace_Blockset'; ...  
        'Aerospace_Toolbox'; ...  
        'Bioinformatics_Toolbox'; ...  
        'Communication_Blocks'; ...  
        'Communication_Toolbox'; ...  
        'Compiler'; ...  
        'Control_Toolbox'; ...  
        'Curve_Fitting_Toolbox'; ...  
        'Data_Acq_Toolbox'; ...  
        'Database_Toolbox'; ...  
        'Datafeed_Toolbox'; ...  
        'Dial_and_Gauge_Blocks'; ...  
        'Distrib_Computing_Toolbox'; ...  
        'Econometrics_Toolbox'; ...  
        'EDA_Simulator_Link_DS'; ...  
        'Embedded_Target_c166'; ...  
        'Embedded_Target_c2000'; ...  
        'Embedded_Target_c6000'; ...  
        'Embedded_Target_MPC555'; ...  
        'Excel_Link'; ...  
        'Filter_Design_HDL_Coder'; ...  
        'Filter_Design_Toolbox'; ...  
        'Fin_Derivatives_Toolbox'; ...  
        'Financial_Toolbox'; ...  
        'Fixed_Income_Toolbox'; ...  
        'Fixed_Point_Toolbox'; ...  
        'Fixed-Point_Blocks'; ...  
        'Fuzzy_Toolbox'; ...  
        'GADS_Toolbox'; ...  
        'IDE_Link_MU'; ...  
        'Identification_Toolbox'; ...  
        'Image_Acquisition_Toolbox'; ...  
        'Image_Toolbox'; ...  
        'Instr_Control_Toolbox'; ...  
        'Link_for_Incisive'; ...  
        'Link_for_ModelSim'; ...  
        'Link_for_Tasking'; ...  
        'Link_for_VisualDSP'; ...  
        'MAP_Toolbox'; ...  
        'MATLAB'; ...  
        'MATLAB_Builder_for_dot_Net'; ...  
        'MATLAB_Builder_for_Java'; ...  
        'MATLAB_Distrib_Comp_Engine'; ...  
        'MATLAB_Excel_Builder'; ...  
        'MATLAB_Link_for_CCS'; ...  
        'MATLAB_Report_Gen'; ...  
        'MBC_Toolbox'; ...  
        'MPC_Toolbox'; ...  
        'NCD_Toolbox'; ...  
        'Neural_Network_Toolbox'; ...  
        'OPC_Toolbox'; ...  
        'Optimization_Toolbox'; ...  
        'PDE_Toolbox'; ...  
        'Power_System_Blocks'; ...  
        'Real-Time_Win_Target'; ...  
        'Real-Time_Workshop'; ...  
        'RF_Blockset'; ...  
        'RF_Toolbox'; ...  
        'Robust_Toolbox'; ...  
        'RTW_Embedded_Coder'; ...  
        'Signal_Blocks'; ...  
        'Signal_Toolbox'; ...  
        'SimBiology'; ...  
        'SimDriveline'; ...  
        'SimElectronics'; ...  
        'SimEvents'; ...  
        'SimHydraulics'; ...  
        'SimMechanics'; ...  
        'Simscape'; ...  
        'SIMULINK'; ...  
        'Simulink_Control_Design'; ...  
        'Simulink_Design_Verifier'; ...  
        'Simulink_HDL_Coder'; ...  
        'Simulink_Param_Estimation'; ...  
        'SIMULINK_Report_Gen'; ...  
        'SL_Verification_Validation'; ...  
        'Spline_Toolbox'; ...  
        'Stateflow'; ...  
        'Stateflow_Coder'; ...  
        'Statistics_Toolbox'; ...  
        'Symbolic_Toolbox'; ...  
        'SystemTest'; ...  
        'Video_and_Image_Blockset'; ...  
        'Virtual_Reality_Toolbox'; ...  
        'Wavelet_Toolbox'; ...  
        'XPC_Embedded_Option'; ...  
        'XPC_Target'};  

That's a lot of typing, so I'd just copy and paste from above, into your Matlab prompt.

Next, we want to check that licenses exist for each of the items in the above list:
 index = cellfun(@(f) license('test',f),featureStr);  
 availableFeatures = featureStr(logical(index));  

Licences that exist are now stored in availableFeatures, so typing that at the prompt gives us a list of licences that are available to the installation:
 availableFeatures  

To see if a single license exists, we can also simply supply license('test',<feature_name>) with the single feature string we care about. For example:
 license('test', 'Neural_Network_Toolbox')  

This is normally as much information as we need, but if we need to know if a licence is available to checkout at the present time, we can continue as below.

NB: Using license('checkout',<license>), as described below, is not normally a good idea when using network licences, as the license will not be released until Matlab is shut down. Usually, licenses are checked out automatically, as and when they are required by a toolbox.

So far, we've found out whether or not a license exists, but not if one is presently available for us to check out. To actually checkout a license, such as the Signal Toolbox, we can do:
 license('checkout','Signal_Toolbox')  
Where Signal_Toolbox comes for the feature string list at the top of this post.

If Matlab returns:
 ans = 1  

Then the license checkout was successful. We can also look at the licenses we currently have checked out out using:
 license('inuse')  

As usual, I hope this has been helpful, and feel free to add any notes or observations in the comments below.

References:

  1. Matlab documentation: license()
  2. Matlab newsgroup: License checking
  3. StackOverflow: How would one check for installed MATLAB toolboxes in a script/function?


Tuesday, 6 November 2012

Fixing Print Screen in Linux Mint XFCE

To enable Print Screen key functionality in Linux Mint XFCE:

Ensure xfce4-screenshooter is installed:
 sudo apt-get install xfce4-screenshooter  

Then, in the Menu, go to Settings -> Keyboard then Application Shortcuts -> Add. In the Command field, enter xfce4-screenshooter and then click OK, before pressing the Print Screen key on the keyboard, to set that shortcut.