Novell Home

HowTo: Using a Window interface with the Linux Shell -- Part 2

Novell Cool Solutions: Feature
By Stomfi

Digg This - Slashdot This

Posted: 13 Oct 2004
 

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

Aims and Purposes

In the first HowTo we wrote two simple shell scripts, one to facilitate the saving of humourous poems into a text file, and one to show a number selected poem on a terminal window.

The functional aim of this two-part HowTo is to build on the previous article and create a window interface to let us write and save the poem in the first part, and to let us select and show the poem in the second part..

In Part 1 we created a window for entering and saving a humourous poem into a text file using the Runtime Revolution IDE.

Start RunRev and open your poems.rev file which is saved in your home/bin or home/bin/develop directory.

The window you previously created will open.



Using the toolbox as previously shown, create a new button called SeePoem with the Label See A Poem.

Set the Text Size to 18.

Click the script Tab and enter the following script.

on mouseUp
  go card READ
end mouseUp

Apply

Now we have to create a card called READ so this button event can go there.

Use the New icon on the main RunRev bar to create a new blank card. This will replace the current card in position and size.



Right-click and select card properties. Set the name to READ. Click the script Tab and enter the following script.



 on  openCard 
  put empty in field “ ReadPoem ”
 end openCard

Apply and x click the properties.

Now create the field ReadPoem on this card.

Set the name to ReadPoem.
 Set the Text size.
 Set the horizontal grid.
 Set the Fixed Line Height.

Now we shall create the button to select a poem.



We are going to use a dialog box to get the number from the user.

A look in the RunRev Docs can show us where to look for the right command.



Left-click on help on the RunRev Main bar and select development guide.

In my documentation the path is Windows and Dialog Boxes, where I select Display a Dialog Box.





Left-click on help on the RunRev Main bar and select development guide.

Click the highlighted word “ask”



This help shows how to use the ask command.

We will use this in our script.

First we have to find out how many poems there are in our poems.txt file.

Remember our Linux script in the first scripting HowTo. It set the variable name NUMPOEMS to the output of the command:
grep -c “%%” $POEMS
where $POEMS was the file path.

RunRev has a function called “shell”. A look at the command in the RunRev documentation shows how to use this command.



We shall use the first synopsis and put our shell command into a RunRev variable.

Then we will put the shell of Variable into the result variable.

Then we can say:

ask “Enter a number between 1 and result variable”

and check that the entered number is within these two numbers.

Create a new button on the bottom left of the new window.
Name it “Select”.
Label it “Select a Poem”.



Using the “Text” Menu on the RunRev bar, change the size to 18.



Use the Save icon on the bar to save the file.

Before we type the script for our Select button, we have to create the shell script we shall use to return the poem we have selected which will be put into the field “ReadPoem”

Open a terminal from the Linux Menu. Use vi or your favourite text editor to open a file called getpoem.sh where we are going to store out shell command.

Type

#!/bin/bash
#getpoem.sh number
#This command uses the first shell argument $1 for the awk variable ANUM
#The Record Separator is set to %%
#If the Record Number equals ANUM it prints each line it comes to in the file
#$HOME/poems.txt
#what it doesn't do is be efficient on a big file by going directly to the
#required record, and exiting when NR is greater than ANUM.
awk -v ANUM=$1 'BEGIN { RS = "%%" }{if( NR == ANUM ) print $0 }' $HOME/poems.txt

Save the file in your bin directory and make it executable with the shell command.

$chmod +x $HOME/bin/getpoem.sh

which you must type in the terminal window.

Back to RunRev

Click the Script tab on the Select button Properties popup.

Type the script as shown below.

This script contains comment lines which start with a “#” They explain what we are doing in the script.

If you leave out the comments the script will still work, but you may want to refer to it at a later stage of these HowTos to jog your memory about something you have forgotten how to do.

Script of button Select of card READ

on mouseUp
  #Lines starting with # are comments
  #set up a  NOGO  message for later use
  put "GO" into NOGO
  #First we have to find out how many poems are in our file
  #In the shell we did this with the shell command "grep"
  #Looking at the code in $HOME/bin/ poems.sh  we can see the line was
  #NUMPOEMS=`grep -c "%%" $POEMS`
  #RunRev allows us to use shell commands with its internal 
  #function called "shell"
  #We are going to use "put the shell of  MYCOMMAND  into NUMPOEMS
  #First get the path of the poems.txt file
  put ($HOME & "/poems.txt") into PoemFile
  replace return with empty in PoemFile

  #We have to put quotes around %% to get grep to work properly
  #Use the RunRev "quote" constant to do this
  #See the RunRev Docs
  put (quote & "%%" & quote) into  MyFlag 
  replace return with empty in MyFlag

  #Notice that & concatenates and && puts a space in between
  #See the RunRev Docs
  put ("grep -c " & MyFlag && PoemFile) into MYCOMMAND
  replace return with empty in MYCOMMAND
  put the shell of MYCOMMAND into NUMPOEMS

  put ("Select a number between 1 and" && NUMPOEMS) into  MyMsg 
  #We use the RunRev ask function for our selection number
  ask MyMsg
  #Save the response
  #The RunRev "it" constant means the last returned string
  #See the RunRev Docs
  put it into  MYRESPONSE 

  #Now we check the number is a valid number
  if the  isNumber  of MYRESPONSE is true
  then
    #Check that it is range
    if MYRESPONSE > 1 and MYRESPONSE <= NUMPOEMS
    then
      #It is okay so we can select a poem
      #we are going to use our shell getpoem.sh command to do this. 
      #we are going to call this file as a RunRev read process
      #with MYRESPONSE as the argument
      #and put the result into the field ReadPoem
   
      put ($HOME & "/bin/getpoem.sh" && MYRESPONSE) into  PoemCommand 
      replace return with empty in PoemCommand
      open process PoemCommand for read
      #read from the process until End Of File
      read from process PoemCommand until  EOF 
      put it into field "ReadPoem"
      close process PoemCommand
    else
      put "NOGO" into NOGO   
    end if
    #end the inner if control
else
  put  "NOGO" into NOGO
end if  
#end the outer if control
#nothing happens if the  number was not valid or in range
#If you like we could send an error message to the field
if NOGO = "NOGO"
then
  put (MYRESPONSE && "is not a valid selection") into field "ReadPoem"
end if
end mouseUp

Press the Apply button on the properties to save the script.

Now we shall create a button and script to quit the application when we have finished reading poems. Do you think you can do this on your own? Here is the script.

Script for button QUIT of card READ

on mouseUp
  #I hate these questions, but it shows how to use the answer command
  answer "Really Quit" with "Yes" or "No"
  put it into SURE
  if SURE = "Yes"
  then
    quit
  end if
end mouseUp

and here is what your window should look like when you press the Select button.



Save the file.

Now we are going to build the application so we can run it as an ordinary program and not from the RunRev IDE.

From the RunRev menu bar select File – Build Distribution The next window appears.



Enter the distribution name poems and browse to your bin/develop directory for the path to build in. Click Linux for the platform.

Now click the Stacks tab to go to where we select our poems stack.



Press the Add Stack File button. Select your poems.rev file from your bin/develop directory.

Press the Build Distribution button and the following Save button.





Click OK on the success window and use File – exit to close the RunRev IDE.

From your GUI menu bar click the icon that shows your home directory and click bin and develop. In this directory you will see a directory with the words Stand Alone. Click this directory and you will see a file called poems. This is your new application.

Copy it to the bin directory.

Type its name in a terminal and it will run your new program.

You can make a desktop shortcut if you like and show your friends how clever you are. All your own work.

The next article in the Poem series will show you HowTo do some more clever things with RunRev, the GIMP and animated pictures to go with your poems.

For more information about Runtime Revolution visit http://www.novell.com/coolsolutions/feature/1863.html


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

© 2014 Novell