script

How to access command-line arguments in Scala shell scripts

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.11, “How to access command-line arguments in a Scala shell script.”

Problem

You want to access the command-line arguments from your Scala shell script.

Solution

Use the same script syntax as shown in Recipe 14.8, “Generating Documentation with scaladoc”, and then access the command-line arguments using args, which is a List[String]` that is implicitly made available:

How to use Scala as a scripting language

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.10, “How to use Scala as a scripting language.”

Back to top

Problem

You want to use Scala as a scripting language on Unix systems, replacing other scripts you’ve written in a Unix shell (Bourne Shell, Bash), Perl, PHP, Ruby, etc.

Back to top

Solution

Save your Scala code to a text file, making sure the first three lines of the script contain the lines shown, which will execute the script using the scala interpreter:

Table of Contents

  1. Problem
  2. Solution
Back to top

Mac OS X: ‘sed’ commands I use to clean MacDown HTML output

FWIW, this is the source code for a sed script I use on my Mac OS X system to convert HTML output generated by MacDown into a format I need. MacDown generates some extra “cruft” that I don’t need, so I use these sed commands to clean up that HTML output:

Raspberry Pi camera module shell script

As a quick note today, this is the source code for a Raspberry Pi (RPI) camera module shell script that I created so a friend can turn her Raspberry Pi camera on and off from the RPI command line (Linux command line):

Converting PDF content to plain text with Scala (or Java)

I recently wrote a little application to convert pages from a PDF to plain text. The GUI portion of the application looks like this:

As you can see, the application just needs the name of a PDF file to convert, along with the page you want to start at and the page you want to end at. There are several ways I could make the application more convenient to use, but since I don't plan to use it that often, I can deal with its limitations.

How to get the IP address of a Linux system from the command line

One way to to get the IP address of a Linux system from the Linux command line is this:

$ hostname -I

That’s the hostname command, followed by a capital letter i as a command line parameter. On my Raspberry Pi system, this command returns its IP address — and only its IP address — like this:

10.0.1.9

It’s nice that this command returns only the IP address, because that means I don’t have to pipe together several commands to get what I need.

Complete backup scripts for my websites (Drupal, MySQL)

I’m spending a little time today trying to automate the process of backing up my websites, and in doing so I thought I would share the Linux shell scripts that I use to generate the backup files, including backups of my MySQL databases and Drupal website directories. If you are comfortable with shell programming in Linux, I think you’ll be able to follow the code in the following scripts.

MySQL database backup script

First, this is a backup script I use to backup a MySQL database:

A Bash for loop to iterate over a file that has blank spaces in its lines

I just had to write a Linux bash shell script that has a for loop that reads a file, and that file contains lines with blank spaces in it. This sounds simple, but blank spaces cause major problems in a Bash for loop.

Fortunately there’s a simple solution: Before the for loop, declare the input field separator to be a newline character, as shown in this for loop code:

How to read and write from a file in a Linux bash shell script

I’m currently writing a complicated Linux bash shell script where I need to keep a counter in an external file, and to do so, I need to be able to write to a file and then read from that file.

In short, this is how I write my counter to that file:

# create a variable to represent the filename
COUNTER_FILE="counter.tmp"

# write to the file
echo "0" > $COUNTER_FILE

Later in the code I increment the counter and write it to the file like this: