All About Ubuntu

Changing the world -- one desktop at a time

Depending of the feedback, I shall add more chapters to this blog subject.

So if you want more, you have to say so.
The Tools
As a Linux
Power User, I know how to use the shell and its tools to perform just about any task I can think of, and when I can't find a shell tool to do what I want, I can always google an application that can be run non interactively from the command line.

I like the shell rather than one of the modern scripting languages like Java, Perl or Python partly because I learned it when UNIX came without a GUI, but mostly because it is an informal end user tool.

When I want shell scripts to be windowed in a GUI, with buttons and data fields and other GUI events, I use the proprietary Runtime Revolution IDE (http://www.runrev.com) because its scripting language is as informal as the shell, and I can call shell commands or previously written scripts to return a value, contents for a field, or perform some activity the results of which are needed later on.

The only problem for the newbie is that they have to learn two scripting languages, but as you shall see, although they are different, they are also quite similar if only the basics are used.

Unlike formal programming languages, the Linux shell tool kit and runrev Transcript lets one write without bothering about declaring data types or the order in which variables are set or even what they are reused for, although a little bit of structure can help when reusing the scripts and fault finding.

These two languages are remarkably intuitive, a wonderful advantage when thinking out the solution to a problem, and are so easy to use once one has learned some basic rules, the solution can be written with little, if any, formal planning,

Using only the basics one can create simple or naïve scripts that can be understood by newcomers to the art, and even you later on. Of course all computer languages allow one to insert comments, which are an invaluable aid when remembering how you did something you want to do again.

All the scripts I write follow this naïve style, so hopefully you will understand them.

General Script Basics

Think of a script as a list of instructions that gets the slaves inside your computer to do your bidding.

Of course, with scripts, like any languages including those we speak, there is the syntax to learn.

Each script command can be thought of as a sentence ended with a new line character instead of full stop, and like any sentence it must contain all the necessary components before it is understood.

In a script command these components are the command, the optional modifiers, and the optional information the command needs.

The optional modifiers are single characters of which the first is preceded by a minus sign.

The information is commonly called the command arguments.

When an argument contains embedded spaces or characters have meaning to the script interpreter, they are enclosed in single or double quotes.

Components, like words in any written language sentence, are usually separated by a space.

Some script commands require special characters like braces, commas, colons, semi colons, quotes, greater and less than symbols, slashes, arithmetic operators, dollar symbols, ampersands, and other symbols, all which need to be placed correctly. Braces and quotes are opened and closed in a pair.

Because a script is a computer program, it also has commands that allow the script to perform activities that depend on whether certain conditions are true or false. These let you make tests or loop around the conditions until the answer finishes the test or completes the loop. These commands are called control structures as they control the flow of the script, like paragraphs and sections control the flow of this article.

For ease of reading, use indentation to delineate your control structures.


Linux shell basic rules

To use shell commands in a GUI open a terminal window.

Most shell commands use lower case.

To find out the syntax, what the options are, and how to use each Linux shell tool use the “man tool_name” command. The command “man man” shows all about the “man” command.

To find out what a particular command does use the “whatis tool_name” command.

If you have “info' on your system, try “info tool_name”. This will give a man page if the info file for the tool is missing. If it is missing, try the command “info coreutils”. Navigate the page until you find the tool name with a star preceding it, and press enter. Navigating info pages is harder than man pages.

A new line activates a command.

To run more than one command per line use the semi colon to separate each command.

To use the output of one command as the input of the next use the pipe operator “|” to join the commands. There can be many commands piped together in one line.

To output the results of one command in a pipe, use the “tee” command where you want the output to occur.

Command input is typed in and output is printed on the controlling terminal. That is the terminal window on which you typed the commands.

Command output can be redirected from the terminal window into a file by using the greater than symbol. One will over write existing contents or create a new file, two next to each other will append to existing contents or create a new file.

Command input can be redirected from a file with the less than operator.


Shell scripting basics

All my scripts use the bash shell.

I add the extension “.sh” to each one so that they are easy to identify as shell scripts.

Start each script with the line #!/bin/bash

Comments start with a #

Write a synopsis comment at the start of each script with the name of the script, any single character options preceded by a minus sign and a descriptive name for each argument enclosing optional ones in square brackets.

Use upper case letters for variable names. Variables are named containers for your own data or information. Using upper case helps to find them quickly.

The dollar symbol is used to access the value of a shell variable.

The “test condition” command can be written “[ condition ]”.

Use a backslash “\” to escape the meaning of character symbols that the shell interprets before performing a command.

Use the “expr” command to perform arithmetic.


Transcript basic rules

Transcript is written in the runrev Transcript editor.

Each script or section of a script starts and ends with an event control command.

Transcript has its own syntax, control structure names, and layout, that although similar to the shell, is not the same.

Transcript is more English like than the Linux shell. For example to put the number 10 into a variable container in Transcript, one uses “put 10 into VAR_NAME”, whereas in the Linux shell, one uses, “VAR_NAME=10”.

Examples.

Now that you've managed to read this far, I better give some examples to show you how easy scripting actually is.

A simple shell command for listing JPEG files in a named folder


ls $HOME/images/*jpg


The command “ls” means, list the contents of a named folder or if none is given list those in the current folder.

$HOME is a shell environment variable and expands in my case to /home/stomfi.

The expansion of these special characters and variable names is done before the controlling shell performs the command.

The star “*” character expands to all the file names in the folder, and “jpg” limits the list to names ending with “jpg”.

I could have included a dot before “jpg” for clarity, but dot is another special character that is expanded to mean any character, thus by the rules above, the dot needs to be escaped by a backslash, making the file selection look like this “*\.jpg”. You can please yourself, but I prefer the simpler looking method.

Another listing with files listed and sorted with block sizes


ls -sS $HOME/images/*jpg


Yet another listing sorted by modify time and size. To use the “ctime” option this has to be a long listing with other information.

ls -ltcsS $HOME/images/*jpg


Shell script example

A simple shell script for listing all the JPEG files showing their name and size that are in a named folder.

The script contains lots of comments. each comment line starting with a “#”. Sometimes a single # is used to leave a blank line to enhance the readability of a script, or comment out something that you may want to leave for future use, like a debugging command.

#!/bin/bash

#jpeglist.sh [folder_name]

#

#List all the jpeg files and sizes in the current or named folder.

#

#Test to see if there is an argument to the command. “$#” expands to the number of arguments.

#”-gt” mean greater than

if [ $# -gt 0 ]

then

#$1 is the first command argument. The quotes protect any embedded spaces

#Change to this folder so that ls does not have to list the path in front of file names

cd ”$1”

#The if, then control structure ends with “fi”

fi

#Back quotes put the result of the included command into the preceding variable name.

#The -1 option tells the ls command to print 1 file per line.

#This makes sure that each file detail is separated by a new line from the next.

#The output of the ls command is piped to the cut command.

#The delimiter -d is set to space enclosed in single quotes.

#The fields from 5 to the end are cut and saved into a variable name holder

#

JPGLIST1=`ls -l1 *jpg | cut -d' ' -f5- `

#

#Reorder the information in a tabbed list and sort it.

#The swiss army knife tool of data formatting, called awk is used.

#Awk has its own syntax quite different from the shell tools.

#Awk reads each line of input and does curly brace enclosed actions on the line.

#The BEGIN statement is only performed once before the actions are done.

#OFS=”\t” set the Output Field Separator to a tab.

#Each space or tab separated field in an input line is numbered $1 to $NF.

#NF being the number of fields.

#Thus we can use awk to print out a tab separated list of selected fields

#from the input in the order we require.

#

NEWLIST=`echo “$JPGLIST” | awk 'BEGIN{OFS=”\t”}{print $4, $1}'`

#

#end of script


You can see that most of this script is explanatory comments.


I could have piped the two commands together in one line thus:


ls -l1 $JFOLD/images/*jpg | cut -d' ' -f5- | awk 'BEGIN{OFS="\t"}{print $4,$1}'

but when learning it is easier to use variable name holders for each logical collection of processes and makes finding mistake a lot simpler.

Be careful copying and pasting shell scripts as word processors and the like embed special characters that shell scripts don't like.

Save the script in your $HOME/bin folder and make it executable with the command:

chmod +x $HOME/bin/jpeglist.sh


Transcript and GUI window example

A simple Transcript for running the above shell script from a button click and placing the output into a field complete with the folder name.

This is the script for a button named CHOOSE

on mouseUp

answer folder “Please choose a JPEG folder”

put it into JPFOLD

put it into field "JFOLDR"

put ($HOME & "/bin/jpeglist.sh" && quote & JPFOLD & quote) into DUSHELL

replace return with empty in DUSHELL

put the shell of DUSHELL into field "JPEGS"

end mouseUp


Each event in Transcript is opened with the “on” command as in on mouseUp


Transcript uses $HOME internally to mean your home folder. Names of objects like fields and button are enclosed in quotes. Variable names are not and are reference by just their name.

The answer folder command pops up a folder chooser dialog with the quoted prompt. The selected is put into the special Transcript variable called “it”, so that the command “put it into JPFOLD” puts the chosen folder name in the JPFOLD variable name. The next command puts it into a display field which has been dragged and dropped onto the window with the GUI bulder.

The long string enclosed in brackets makes up the shell command and the folder. The & symbol means no space and the && one space. The quote puts a literal quote around the folder name.


The command replace return with empty makes sure there are no embedded carriage returns that Transcript often puts into long strings and which the shell sees as a command execute symbol, not what we want.

The shell is executed and the result put into another display field.


Finally the script event is ended with end mouseUp.

Pictures

Here is a picture of the file chooser and the GUI display window.

Notice how Runtime revolution has used my GNOME GTK theme.

I could pretty things up with labels over the display fields and even add some colours and maybe another field for displaying images I click on from the list.

Want me to do that and show you a bit more about the capabilities of RunRev?

Share 

Comment

You need to be a member of All About Ubuntu to add comments!

Join this Ning Network

Badge

Loading…

© 2009   Created by The VAR Guy on Ning.   Create a Ning Network!

Badges  |  Report an Issue  |  Privacy  |  Terms of Service