command line

To access the command line from your NX session, open a terminal window: right-click on the background, and select ‘Open Terminal Here’:

../../_images/terminal-menu.png

Alternatively, there is a shortcut icon in the lower left of the screen:

../../_images/terminal-shortcut.png

shell script

A shell script is a series of commands saved to a file for later execution.

You can create a script with a text editor. Text editors available on the cluster include:

  • gedit: simple editor with a graphical interface
  • nano: simple editor with a curses-based interface
  • emacs: advanced editor with a graphical interface
  • vi: advanced editor with a text interface

Below is an example of a script that prints the date, and then sleeps 20 seconds.

  1. Start a text editor:

    $ nano
  2. Enter the following text, and save as ‘simple.sh’:

    #!/bin/sh
    date
    sleep 20
  3. Make the script executable:

    $ chmod a+x simple.sh
  4. Run the command at the shell prompt:

    $ ./simple.sh

iteration

A common use of shell programming is to perform a task multiple times, iterating over a list.

In this example, we use a ‘for’ loop to run the ‘echo’ command 5 times. As a result, the value of ‘i’ is printed (echo’d) to the screen, where i=1, i=2, i=3, i=4 and i=5. The for loop is terminated with the ‘done’ statement. You may copy (and paste) the lines below into a terminal window, or copy them into a file, and run it as a shell script:

for i in {1..5}; do
    echo "$i"
done

The ‘seq’ command prints a sequence of numbers, and has options to define the increment, pad with zeros, etc. In this example, we evaluate the numbers 0 to 20 by increments of 4. (A line starting with a pound symbol, #, is ignored):

# define j
j=0
# define i
for i in `seq 0 4 20`; do
    # print value of i
    echo "i:$i"
    # print equation, including current values of both i and j
    echo "j=${i}+${j}"
    # add i+j
    ((j = $j + $i))
    # print value of j
    echo "j:$j"
done

Again, you may copy the above lines, and paste them directly into a terminal window, or copy them into a file, and run it as a shell script. To learn more about seq, run ‘man seq’ (to quit the ‘man’ command, type ‘q’).

useful commands

See Unix Cheat sheet