NILUG

Monday, 29 September, 2008

October 2008 NILUG Meeting

Filed under: Uncategorized — nilug @ 8:59 am

The October meeting of the North Idaho Linux Users Group will be held on Saturday, 11 October at the River of Life Friends Church in Post Falls. The meeting will start at 1 pm.

No formal program is scheduled for this month, so we will be able to answer questions and provide help for new users or for people who are interested in learning more about Linux and Open Source software.

Wednesday, 17 September, 2008

Wednesday, 17 September, 2008

Filed under: Uncategorized — nilug @ 10:09 am

Go to the OpenStickers web site to pick up a couple of PDF files which contain some very nice stickers for open source.

Saturday, 13 September, 2008

Writing a Shell Script

Filed under: Command Line — nilug @ 9:17 am

1. Tools to use

To write a shell script, use a standard text editor. That can be a command line editor, such as vi, emacs, pico, ted or joe. It can also be a GUI editor, such as Gedit, Kate, Kwrite, Kedit or Jedit.

Shell Scripts always start with a line telling the shell what program to use to execute the script. The line always starts with a shebang and the path to the program that will execute the script:

#!/bin/bash
#!/bin/sh

The first line would invoke the Bourne Again Shell. The second line would invoke the generic Bourne Shell.

2. Executing a shell script

Once a shell script file is written and saved, you must tell the the shell that it is actually executable, not just another text file. You do this by changing the file permissions:

chmod +x myscript.sh
chmod 755 myscript.sh

Both of the above commands set the myscript.sh file to be executable. If you have saved the file someplace where root access is required to change permissions, use

sudo chmod +x myscript.sh
sudo chmod 755 myscript.sh

To execute the script, you must tell the shell to run it.

myscript.sh # script is in the shell PATH
/tmp/myscript.sh # script is not anywhere known
./myscript.sh # script is in the current directory

If the file has been saved somewhere on the path (echo $PATH to see where the shell looks), then just typing the name of the shell script will execute it. If the file has been saved somewhere else, type the full path to the file. If the file is in the current directory, type a ‘./’ before the name.

3. Writing the script

A shell script just contains a list of commands you could just as well type directly on the command line. The commands can be external programs or internal shell commands. If the list is long or the commands are long, it is easier to put them in a script and execute the script with a single command. Here is a simple ‘hello.sh’ script:

1: #!/bin/bash
2: #
3: # hello.sh
4: #
5: clear
6: echo "Hello, $USER"
7: echo "Today is"
8: date
9: echo "This is an $HOSTTYPE machine"
10: echo "Number of users logged in:"
11: who | wc -l
12: echo "This month's calendar:"
13: cal
14: exit 0

Line 1 tells the command line shell to use bash to execute this script.

Since anything after a single # is considered a comment and is ignored, lines 2 through 4 are ignored.

Line 5 is a Linux command that is used to clear the terminal display.

All shells have certain ‘built-in’ commands. The echo command is one of those ‘built-ins’. It prints the line to the display. Line 6 uses the echo command to display the line. Note the $USER on this line. All shells also have items called ‘environment variables’. The USER variable is one that is always present. It contains the name of the person who is logged in. The $ is used to tell the shell that this is an environment variable and that we want to use its contents. If you want to see all the environment variables, just type ‘set’ (the ‘set’ command is used to display, set and clear the variables.)

Like line 6, line 7 echoes its contents.

Line 8 executes the Linux ‘date’ command to display the current date and time.

Lines 9 and 10 echo their contents.

Line 11 demonstrates how to pipe the output of one Linux command into the input of another Linux command. The ‘who’ command prints a line for each use logged in to the machine. Those lines are piped into the ‘wc -l’, where the wordcount program counts the number of lines (-l) and displays the result.

Line 12 echoes its contents.

Line 13 is a Linux command that displays a calendar. With no arguments, it will display a calendar for the current month.

Line 14 is optional, but a good idea to include. All shell scripts return a value to the shell they are running under. If that return value is zero, then everything has gone all right in the script. If it is any other value, it’s considered an error. If you don’t include the ‘exit’, the script will return the results of the last command executed (the ‘cal’ command in our example).

4. Shell Variables

When processing data in a script, it is often necessary to refer back to values created earlier in the script, or to values created by Linux itself. These values are stored in places called shell variables. Shell variables are untyped: they can be treated as either alphanumeric strings of characters or as numbers, depending on whether they actually ARE numbers.

Each variable has a label. That variable is invoked by the shell script by using the label with a dollar sign ($) pre-pended to the label. For instance, the PATH variable is used as $PATH. If you neglected to put that dollar sign in front, the shell script would assume you wanted to use the word PATH. Important note: shell variables are case-sensitive. PATH, Path and path are all different variables.

There are a lot of built-in shell variables other than the ones we have already mentioned. Here are a few more that may be of interest.

  • $COLUMNS – the number of columns your terminal currently has.
  • $HOME – the home directory of the current user.
  • $IFS – list of characters that separate command line options.
  • $LINES – the number of lines you terminal currently has.
  • $LOGNAME – the login name of the current user.
  • $OSTYPE = the type of the operating system we are currently running.
  • $PATH – the search path the shell uses when looking for files.
  • $PS1 – the settings for the current command line prompt.
  • $PWD – the current working directory.
  • $SHELL – the name of the current command shell.
  • $USERNAME – the name of the current user.
  • $? – the exit value of the last program run by the command shell ($status in the C shell).
  • $# – the number of arguments on the command line ($#argv in the C shell).

5. Built-in Commands

While the command shell can execute any program you tell it (and it can find), its real power comes from using commands that are built into the shell program. These internal commands allow you to do things like changing environment variables, sending information to the display, make other changes to the shell environment and, most importantly, control the flow of your shell program.

Some of the environment-type commands that are built into the shell are

  • alias – assign a shorthand name as a synonym for a command.
  • cd – change the current working directory.
  • echo – display something on the terminal.
  • exit – leave a shell program.
  • history – display a numbered list of the command line history.
  • jobs – lost all running or stopped jobs.
  • kill – terminate a specified process ID or job ID.
  • pwd – display the current working directory name.

The program flow commands are

  • case…esac – given a value, find a match among a set of choices, then execute the lines associated with that choice. For the C shell, the case statement is switch…endsw.
  • break – exit from whatever loop construct you are in (for, while or until), or exit from a select list.
  • continue – skip the remaining statements inside a for, while or until loop and resume with the next iteration of the loop.
  • for…done – go through a list, assigning each member of the list to a variable and executing commands in the loop. For the C shell the for statement is foreach…end.
  • if…[else]…fi – perform a test and, if the test is true, execute the statements following. If an else is included and the test is false, execute the statements after the else. For the C shell, the if statement is if…[else]…endif.
  • select…do…done – a visual case statement. Allows you to chose a value from a list, then execute commands inside the do…done until a break is done.
  • until…do…done – perform a test and if the test is true, then execute commands inside the do…done until a break is done.
  • while…do…done – perform a test and if the test is true, then execute commands inside the do…done until a break is done.

Here is a simple iftest.sh script

1: #!/bin/bash
2: #
3: # iftest.sh
4: #
5: echo "What is your name?"
6: read yourname
7: if [ $yourname == "mike" ]; then
8: echo "Hey, $yourname, I know you!"
9: else
10: echo "Hello, $yourname, I'm happy to meet you."
11: fi
12: exit 0

We have already talked about lines 1 through 4 in our first example.

Line 5 is the prompt for user input.

Line 6 reads a line from the keyboard and puts it in the shell variable ‘yourname’.

Line 7 tests the contents of yourname against the string ‘mike’. if they are equal, line 8 is executed, then line 11. If the test is false, line 10 is executed, then line 11.

Line 12 is our normal exit from the program.

6. Towers of Hanoi Example (hanoi.sh)

The Towers of Hanoi program demonstrates the use of the case statement, subroutines and program recursion.

#! /bin/bash
#
# The Towers Of Hanoi
# Bash script
# Copyright (C) 2000 Amit Singh. All Rights Reserved.
# Modified by M. Burton

# http://hanoi.kernelthread.com
#
# Last tested under bash version 2.05b.0(13)-release
#

dohanoi() {
case $1 in
0)
;;
*)
dohanoi "$(($1-1))" $2 $4 $3
let "STEP=STEP + 1"
echo ${STEP}: move $2 "-->" $3
dohanoi "$(($1-1))" $4 $3 $2
;;
esac
}

STEP=0
case $# in
1)
case $(($1>0)) in

1)
dohanoi $1 1 3 2
exit 0;
;;
*)
echo "$0: illegal value for number of disks";
exit 2;
;;
esac
;;
*)
echo "usage: $0 N"

exit 1;
;;
esac

Sunday, 7 September, 2008

September 2008 NILUG Meeting

Filed under: Uncategorized — nilug @ 12:39 pm

The September meeting of the North Idaho Linux Users Group will be held on Saturday, 13 August at the River of Life Friends Church in Post Falls. The meeting will start at 1 pm.

Mike Burton will give a talk on ‘Command Line Basics – Writing a Shell Script’. The talk will also be posted here after the meeting. Those of you interested in getting more work done with your Linux box should give a listen.

The NILUG meeting is free to all. If you would like to know more about Linux, or would like help installing Linux on your computer, we encourage you to attend the meeting.