script

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:

How to run a Unix shell script from the Mac Finder

If you ever want to create a Unix shell script that you can give to someone else so they can double-click it and run it through the Mac OS X Finder, all you have to do is (a) name the file with the ".command" extension and (b) make it executable. So, just name your Mac/Unix script like this:

ShowProcesses.command

Then make it executable, like this:

chmod +x ShowProcesses.command

You can also leave out the usual #!/bin/sh part on the first line.

Setting the classpath in a Scala script (Scala shell script)

Scala script FAQ: How do I set the CLASSPATH in a Scala shell script?

If you need to set the CLASSPATH when executing a Scala script, use the exec command as shown in the following example:

#!/bin/sh
exec scala -classpath "lib/htmlcleaner-2.2.jar:lib/scalaemail_2.9.1-1.0.jar:lib/stockutils_2.9.1-1.0.jar" "$0" "$@"
!#

As you can see from that code, I'm adding three jar files to my classpath at the beginning of my Scala shell script.

A Perl script to print Nagios log records in a human readable date format

Perl date FAQ: Can you share a Perl date example where you print a date in a decent human-readable format?

Update: Be sure to look at the comments below for some great Perl "one liners", i.e., one-line solutions to this problem.

In this blog I'll share the source code for a Perl program that takes nagios.logrecords as input, then outputs the records with a human-readable date format. More specifically, the input records look like this:

Reading Scala command line arguments

Scala command line FAQ: How do I read command line arguments (args) in a Scala shell script?

If your Scala shell script is very short, and you're not using an object or class declaration -- i.e., you have no main method -- you can access the script's command line arguments through the default args array, which is made available to you by Scala.

For instance, you can create a one-line Scala script named hello.scala like this:

Syndicate content