Novell Home

HowTo: Create a GUI Interface to the Shell

Novell Cool Solutions: Feature
By Stomfi

Digg This - Slashdot This

Posted: 28 Apr 2005
 

StomfiLearning to use Linux at Home and Work
Welcome to my ongoing series of HowTo articles designed to help Linux newbies get comfortable with Linux. Before trying any of these HowTos, take a few minutes to study the prerequisites so you can hit the ground running.
--Stomfi

This is a simple RunRev/shell application for newbies that gives them a familiar window where they can use a point and click interface to construct a shell command line. It lets the newbie lookup shell commands by entering a key word, view the synopsis, and optionally view the info manual. Buttons and input windows are used to enter the required elements of a command line. It includes the ability to use pipes, sequential commands and redirection. Errors are reported and the constructed command line can be edited to get rid of bugs. The output of each command line is displayed in the GUI window.

This HowTo describes a Graphical interface for searching for shell commands to perform actions and a command builder using point and click functions. Runtime Revolution is used to create the Graphical User Interface screens and the shell tools are used to process the required information.

The RunRev GUI window looks like this:

Clicking the top button, ask for a word about what you want to do. A simple shell script is called that uses grep to looks in the list of available tools, for matching whole words. You can see from the label you can also put a dot (.) in the button input window, which lists all the commands in the lookup file.

This is the script:

#!/bin/bash

#whatword.sh word

#

WLIST="$HOME/shellgui/lists/whatlist"

grep -i -w "$1" $WLIST 

The list was made by me from the contents of "/bin", "/usr/bin", "/usr/local/bin", "/sbin", and "/usr/sbin", by running a loop that ran the whatis command for each file in each directory. I manipulated this list, deleting anything I thought was not needed by the average user, and word smithed it into a suitable format for this application. You can add others you think are needed.

Here is my list:

alias - make short names for commands.

apropos - keyword command lookup.

awk - pattern scanning and processing language.

banner - display string in large letters.

basename - display base part of file names.

cal - calendar display.

cancel - cancel requests to printer.

cat - concatenate files.

cd - change working directory.

chdir - change working directory.

chgrp - change group ownership of file or directory.

chmod - change permissions of file or directory.

chown - change ownership of file or directory.

clear - clear screen.

comm - display common, and not common lines, between two sorted lists.

cp - copy file.

date - display or set date.

df - display free disk space on file systems.

diff - display line-by-line differences between two text files.

du - display number of disk blocks used per directory or file.

echo - echo arguments to standard output.

exit - exit current shell or script.

expr - evaluate arguments as logical, arithmetic, or string expression.

false - provide truth values.

file - determine type of file by examining its contents.

find - find files by characteristics and perform optional commands.

finger - display information about users.

fold - fold long lines for display on an output device of given width.

ftp - start file transfer program.

grep - search file or files for regular expression or string.

groups - display user group memberships.

gzip - compress file and add extension .gz

gunzip - restore compressed file to its unencoded state.

head - display first part of text file.

history - show name of current host system.

join - join data in relational files.

kill - terminate process.

less - show one page at time.

ln - make hard or symbolic links to files.

login - log in to system.

lpl - send requests to printer.

lpq - display queue of printer jobs.

lpr - send job to printer.

lprm - remove jobs from printer queue.

ls - list contents of directory.

man - display reference manual pages.

mesg - permit or deny messages on terminal.

mkdir - make directory.

more - display text file one page at time.

mv - move or rename files.

nawk - pattern scanning and processing language.

nice - run command at low priority.

page - display text file one page at time.

passwd - change local or Network Information.

paste - join corresponding lines of several files or subsequent lines of one file.

pr - prepare file(s) for printing.

ps - display status of current processes.

ptx - generate permutated index.

pwd - display path name of current working directory.

rev - reverse order of characters in each line.

rm - remove or unlink file or directory.

rmdir - remove or unlink file or directory.

rusers - show who is logged in on local machines.

rwall - write to all users over network.

rwho - show who is logged in on local machines.

script - make typescript of terminal session.

sed - stream editor.

sh - open shell command interpreter.

size - display size of an object file.

sleep - suspend execution for specified interval.

sort - sort and collate lines.

spell - report spelling errors.

split - split file into pieces.

strings - find printable strings in an object file or in binary file.

stty - set or alter options for terminal.

su - super-user or temporarily switch to different user ID.

sux - super-user or temporarily switch to different user ID with GUI permissions.

tail - display last part of file.

talk - talk to another user.

tar - create tape archives, and add or extract files.

tee - replicate standard output.

test - return true or false according to conditional expression.

time - time command.

touch - update access and modification times of file.

true - provide truth values.

tty - display name of terminal.

uniq - remove or report adjacent duplicate lines.

units - start conversion program.

uptime - show how long system has been up.

users - display compact list of users logged in.

vacation - reply to mail automatically.

vi - edit files with visual display editor based on ex.

view - view files with visual display editor based on ex.

w - show who is logged in and what they are doing.

wait - wait for process to finish.

wall - write to all users logged in.

wc - display count of lines, words, and characters.

whatis - display one line summary of keyword.

whereis - locate binary, source, and manual page files for command.

which - locate command and display path name or alias.

who - show who is logged in on system.

whoami - display effective current user name.

write - write message to another user.

| - pipe output of command to input of next command.

; - add another shell command to be run concurrently.

yes - be repetitively affirmative.

zcat - display compressed files.

You can copy this list into the file "$HOME/shellgui/lists/whatlist"

Make sure there are no hidden characters in this file.

In the example my input word has created a list of possible commands. Clicking on one of the lines in this list put the command name into one field and generates a command synopsis from the following shell script.

#!/bin/bash

#

#sysnopsis.sh command

#Grab and print synopsis lines from man pages

#These show the usage of each command

#

#Create a pipe using man, head, and awk

#man prints the formatted page, head grabs the first 20 lines which should

#be enough for the NAME, SYNOPSIS, and DESCRIPTION headings in most

#man pages

#

#Man prints a Reformatting message which the grep gets rid of

#The Headings are in bold which means the contain a pattern of letters

#made up from each letter followed by a back space and a repeat of the letter.

#To get the ^H into this file type Ctrl v then Ctrl h when in vi insert mode

#

#The output from awk is still formatted like this and will have to be

#converted before it can be displayed in RunRev

#This is a matter of finding each ^H deleting it and the preceding character.

#

man "$1" 2>&1 | grep -v "Reformatting" | head -20 | awk 'BEGIN {COUNT=0};{{\

if($1 ~ "S^HSY^HYN^HNO^HOP^HPS^HSI^HIS^HS") COUNT=NR}\

{if(COUNT > 1 && NR > COUNT && $1 !~ "D^HDE^HES^HSC^HCR^HRI^HIP^HPT^HTI^HIO^HON^HN") print $0}\

{if($1 ~ "D^HDE^HES^HSC^HCR^HRI^HIP^HPT^HTI^HIO^HON^HN")\

exit}}'| sed -e "s/.^H//g"

This looks a bit complex, but is really quite simple. The man command prints the man page for $1 putting the standard out put and error into the pipe. The grep command prints lines that don't contain the pattern "Reformatting" sending the output into the pipe. The head command prints the first 20 lines into the pipe.

The awk command does three things.

This looks a bit complex, but is really quite simple. The man command prints the man page for $1 putting the standard out put and error into the pipe. The grep command prints lines that don't contain the pattern "Reformatting" sending the output into the pipe. The head command prints the first 20 lines into the pipe.

The awk command does three things.

It look for the "bolded" pattern SYNOPSIS and sets COUNT to the Number of the line.

It checks that if COUNT is greater than 1, and the line number is greater than COUNT, and it doesn't contain the pattern DESCRIPTION it prints the line. There can be several of these.

If the line does contain DESCRIPTION it exits.

If you look at a man page you will see that this script is printing out all the lines between SYNOPSIS and DESCRIPTION.

The sed command substitutes a single character followed by a Control H, by nothing, for as many times it occurs in a line.

The output from this four stage pipe gets put into the RunRev field.

Clicking the Info Page button brings up a shell window with the info page which can be left on screen for as long as you need.

Clinking the Add Command to Line button, adds this command and the other buttons ask for any options and other arguments like file names, and give you the option of adding an output construction. This will let you add things like output or input to or from a file, or a pipe or colon so you can add more commands. The final button ends the command by setting a RunRev variable so you can add another command. Some of the other buttons also do this as the characters, (like "|") always terminate the preceding command.

The run command button send the command output to the output field so you see the results, which could be what you want or errors for you to debug with, like in my example.

In case you want to edit and re run the command, the Command Construction Field lets you do this.

This application has been designed so you can use a familiar point and click interface to learn the shell. After a while you can progress to the real command line, which lets you recall a command with history, edit any line, and many other more powerful functions. The command lookup features are extremely useful to the newbie as they let you know what a command does, and how to use it.

This is the Application browser window which shows you all the objects on the window.

The ones labelled "Field 1" and so on to "Field 5" are the label fields, which I hardly ever bother giving a meaningful name to. I should be consistent and use UPPERCASE for all my button and field names, but you can see I sometimes use a bit of a mix in the development phase. I usually fix this before final completion.

These are the RunRev scripts in order from the top of the window to the bottom.

Card Script

on openCard

   global NEWCOM

   put "OLD" into NEWCOM

   put empty into field "CHOOSE"

   put empty into field "MYCHOICE"

   put empty into field "THISCOMMAND"

   put empty into field "COMOUT"

   put empty into field "SYNOPSIS"

end openCard

WHAT button

on mouseUp

   ask "Enter a single word about what you want to do"

   put it into WHATWORD

   put($HOME & "/shellgui/bin/whatword.sh" && WHATWORD) into DOWHAT

   replace return with empty in DOWHAT

   put the shell of DOWHAT into field "CHOOSE"

end mouseUp

You can keep pressing this button and putting in new words until you find a command you think will do what you want.

CHOOSE field.

on mouseDown

   put the selectedText of me into MyChoice

   put ("echo" && MyChoice & " | awk -F- '{print $1}'") into MYSHELL

   replace return with empty in MYSHELL

   put the shell of MYSHELL into MYCOM

   put MYCOM into field "MYCHOICE"

   put ($HOME & "/shellgui/bin/synopsis.sh" && MYCOM) into DUSYNOP

   replace return with empty in DUSYNOP

   put the shell of DUSYNOP into field "SYNOPSIS"

end mouseDown

The first shell command of this script, gets the name of the command using "-" as the field separator, which is the first field, the other being what the command does.

ViewInfoPage button

on mouseUp

   if field "MYCHOICE" <> empty

   then

      put field "MYCHOICE" into MYCOM

      put ("xterm -e info" && MYCOM) into MYSHELL

      launch MYSHELL

    end if

end mouseUp

This script launches a shell window with the info command, which stays on the screen until you X it off.

AddCom button

on mouseUp

   global NEWCOM

   if NEWCOM <> "NEW"

   then

      #there isn't one in process

      if field "MYCHOICE" <> empty

      then

         #This is the start of the command constructor

         #First is the command name

         put field "MYCHOICE" after field "THISCOMMAND"

         replace return with empty in field "THISCOMMAND"

         put "NEW" into NEWCOM

      end if

   end if

end mouseUp

Each time anything is added after the THISCOMMAND field, RunRev inserts a newline (return). This is replaced with "empty" which puts each new element of the command one after the other.

NewOpt button

on mouseUp

   global NEWCOM

   if NEWCOM = "NEW"

   then

      ask "Enter character(s) for option"

      put it into OPTANS

      if OPTANS <> empty

      then

         if length(OPTANS) > 1

         then

            put (" --" & OPTANS) after field "THISCOMMAND"

         else

            put (" -" & OPTANS) after field "THISCOMMAND"

         end if

         global NEWOPT

         put "NEW" into NEWOPT

         replace return with empty in field "THISCOMMAND"

      end if

   end if

end mouseUp

This script checks the number of characters, and following shell command conventions puts a single dash if it is one letter, or two if it is several. In the real world of shell commands, you only need one dash for multiple single letters, but this application can't check for that, so you will have to edit your own construction line if you do that.

OPTARG button

on mouseUp

   global NEWOPT

   global NEWCOM

   if NEWOPT = "NEW" and NEWCOM = "NEW"

   then

      ask "Enter Argument for Option"

      put it into OPTARG

      if OPTARG <> empty

      then

         put space & OPTARG after field "THISCOMMAND"

         put "OLD" into NEWOPT

         replace return with empty in field "THISCOMMAND"

      end if

   end if

end mouseUp

This script only lets you add an argument if it finds a New Option.

COMARG button

on mouseUp

   global NEWCOM

   if NEWCOM = "NEW"

   then

      answer "What type of Argument is this" with "Argument" or "File"

      put it into NEWARG

      if NEWARG = "Argument"

      then

         ask "Enter Argument"

         put it into THISARG

         if THISARG <> empty

         then

            put space & THISARG after field "THISCOMMAND"

            replace return with empty in field "THISCOMMAND"

         end if

      else

         answer file "Choose a file"

         put it into THISFILE

         if THISFILE &lt;&gt; empty

         then

            put space & THISFILE after field "THISCOMMAND"

            replace return with empty in field "THISCOMMAND"

         end if

      end if

   end if

end mouseUp

This script lets you select either an ordinary argument or choose a file with the "answer file" RunRev file selector.

REDIRECT button

on mouseUp

   global NEWCOM

   if NEWCOM = "NEW"

   then

      answer "Choose Redirect" with "<" or ">" or ">>"

      put it into RED

      if RED <> empty

      then

         if RED = ">" or RED = ">>"

         then

            put "OLD" into NEWCOM

         end if

         put space & RED " space into field "THISCOMMAND"

         replace return with empty in field "THISCOMMAND"

         answer file "Choose file for Redirect Command"

         put it into REDFILE

         if REDFILE <> empty

         then

            put REDFILE after field "THISCOMMAND"

            replace return with empty in field "THISCOMMAND"

         end if

      end if

   end if

end mouseUp

This script find out what sort of redirection you need and sets the end command flag if it is an output redirection. It allows you to select the file name with the RunRev file chooser.

JOINER button

on mouseUp

   global NEWCOM

   if NEWCOM = "NEW"

   then

      answer "Choose Joiner" with "|" or ";"

      put it into JOINER

      if JOINER <> empty

      then

         put "OLD" into NEWCOM

         put space & JOINER & space after field "THISCOMMAND"

         replace return with empty in field "THISCOMMAND"

      end if

   end if

end mouseUp

This script also sets the end command flag, as anything after one of these joiners will be a new command.

ENDCOM button

on mouseUp

   global NEWCOM

   put "OLD" into NEWCOM

end mouseUp

This button is used to set the end command flag, which may be needed in some circumstances.

RunCom button

on mouseUp

   if field "THISCOMMAND" <> empty

   then

      #Run command whatever its status so error messages can be used

      #to learn from

      put field "THISCOMMAND" into MYCOM

      put the shell of MYCOM into field "COMOUT"

   end if

end mouseUp

The first picture showed an error message for my grep command, and told me to run the command "grep ?help"

That's two dashes before the "help"

This is the top part of what it printed:

The scroll bar (not shown) on the right of the OUTPUT field can be used to see the rest of this command help.

It's a pity that not all commands have this kind of help, as I would have like to have used the first lines of this for my synopsis.

Apart from a few in RunRev script shell commands, there are only two shell scripts used in this application. The RunRev scripts are all pretty straight forward and simple. The application does all the work creating you command line scripts. These are the important part.

I hope this simple application is successful in teaching you newbies the powers of command line processing while giving you a familiar windowed point and click interface. Anyone who can develop this application for their local newbies will be doing them a great favour.


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell