PRODUCT






Home









Free Download








Installation Instructions





FAQ





FAQ








Ask A Question





LEARN SCRIPTING





Overview








Lesson 1








2


3


4


5








Exam





SAMPLE SCRIPTS





Computer








Internet








Administrators








Developers








Data








Miscellaneous





HELP / DOCUMENTATION





Commands








Automated Internet








Automated Editors








Sample Scripts








Precompiled Functions








System Features






Learning Scripting - Lesson 3
(Stream Redirection, Integers, Creating Scripts, While Loop, Special Characters)

In the last lesson, we saw all important aspects of a command. We will expand on that knowledge in this lesson, and see how to redirect streams asociated with a command. Next, we will introduce another data type - integer. I will show you how to create your own script. You will also be introduced to the while loop. Finally, a small discusssion on special characters and the lex command will help us get set for the next lesson.



PREPARATION
  1. Did you understand the important concepts in the last lesson ? If not, please take a look at that lesson again. Look at the words, phrases and sentences in red color. Make sure you understand them.
  2. Start biterScripting. Enter the following command exactly as you see it below. Then press the ENTER key.

    Command
    script "http://www.biterscripting.com/LearningScripting/Prep3.txt"


    Keep biterScripting open throughout this lesson. Make C:/LearningScripting/Lesson3 the current directory, by entering the following command. (This directory was created by the above script command.)

    Command
    ccd "C:/LearningScripting/Lesson3"




  3. Since we will be creating new scripts in this lesson, that means we will be creating new text files, open your system viewer. A system viewer shows on the left, the directory structure within your system, and on the right, it shows the list of files within one directory. It also allows you to navigate within directories by double clicking directories on the left. It opens files for you when you double click a file on the right. Further, it will allow you to create, edit, delete, rename a file by right-clicking on it. (Several system viewers are available - I like Windows Explorer. (Windows Explorer is a trademark, service mark or other forms of intellectual property of its respective owner.) You can use a system viewer that you like the most - just make sure it has the above capabilities.) Since I am not sure which system viewer you will be using, I will just refer to it as system viewer, or just, viewer.

    Once you have a system viewer open, go to directory C:/LearningScripting/Lesson3. We will use this directory throughout this lesson.




QUESTION 3.1 What streams are associated with a command ? Answer





3.1. STREAM REDIRECTION

We saw that output and error streams, by default, are written to screen. Also, input stream, by default, is assumed to be empty. A technique called stream redirection is provided in biterScripting so that you can manipulate all streams associated with a command. Stream redirection is a technique which allows one to redirect all streams associated with a command to/from files, variables, etc. . This is done using the >, >> and < symbols. I will demonstarate with examples. Enter the following commands.

Commands
ccd "C:/LearningScripting/Lesson3"
lf -rn "*" "C:/LearningScripting" > "filelist.txt"


The above lf command did not write any output to screen. That's because we redirected its output (using the symbol > ) to the file filelist.txt (in directory C:/LearningScripting/Lesson3). So, instead of writing its output to the screen, the command wrote the output into that file. Go ahead and open the file filelist.txt by double clicking it in the system viewer. This double clicking will open that file within a text editor. The contents of the file should display the list of files and directories within the directory C:/LearningScripting. Close the text editor (and the file).

The above was redirection of output stream. "filelist.txt" in the lf command above is called redirection target. Redirection target can be a file name or a string variable. (biterScripting allows other redirection targets as well, but, for now, we will just focus on files and string varaibles.) Enter the following commands.

Commands
var string fl
lf -rn "*" "C:/LearningScripting" > $fl


Above, we declared a string variable $fl and redirected the output from the lf command to that variable. The value of variable $fl is now set to the output from the lf command. We have thus saved the output of the lf command for future use. To verify, type the following command.

Command
echo $fl


The echo command above should show the exact same output that the lf command produced earlier.

Stream redirection comes in two flavors - overwrite mode and append mode. The redirection in overwrite mode is implemented with the > symbol. It overwrites the previous contents of the redirection target. The redirection in append mode is implemented with the >> symbol. It appends to the previous contents of the redirection target.

In the commands above, we used the overwrite mode. The contents of file filelist.txt and variable $fl still contain the output of the lf command. We will use those commands again, except we will use append mode. Enter the following commands.

Commands
lf -rn "*" "C:/LearningScripting" >> "filelist.txt"
lf -rn "*" "C:/LearningScripting" >> $fl


Since we used the append mode, we will expect that the file filelist.txt and variable $fl should contain output of the lf command twice. Let's verify that. Open file filelist.txt in system viewer. Does it now contain the output of the lf command twice ? It does. Close the file. Enter the following command.

Command
echo $fl


Does the variable $fl contain the output of the lf command twice ? It does.

QUESTION 3.2 The file C:/LearningScripting/Lesson3/filelist.txt contains some text. What command will wipe out its contents ? Answer



QUESTION 3.3 The variable $fl contains some text. What command will wipe out its contents ? Answer

The stream error can be similary redirected. The only difference is that we use 2 (number two) before the > and >> symbols. Enter the following commands.

Commands
ccd "C:/LearningScripting/XYZ" 2> "error.txt"
var string error
ccd "C:/LearningScripting/XYZ" 2>> $error


The first ccd command redirected the stream error to file error.txt in overwrite mode. The second ccd command redirected the stream error to variable $error in append mode. Let's verify it. Execute this command.

Command
echo $error




So far, none of the command examples we saw used stream input. Stream input only comes into play when it is redirected. It is done using the < symbol. Enter the following commands.

Commands
repro < "error.txt"
lf -n "*.txt" "C:/LearningScripting/Lesson3" > $fl
repro < $fl


The first repro command showed you the contents of file error.txt. The second repro command showed you the contents of variable $fl. The repro command reproduces the contents of stream input in addition to reproducing the contents of command arguments. Also, input stream redirection does not have the two modes - overwrite and append. The entire contents of the redirection target are used as stream input to the command.

One important thing to remember - only variables of type string can be used in stream redirection.

QUESTION 3.4 What is the difference between overwrite mode and append mode ? Answer

Details about redirection of stream output, stream error and stream input, are available in help topics 'output', 'error' and 'input' respectively.



3.2. INTEGERS

So far, we saw one type of variable - string (str). I will now introduce another important variable type - integer. An integer is a number without a fraction. It can be a positive number, negative number, or 0. The integer data type is abbreviated as int . An integer variable is declared using the var command. Enter the following command.

Command
var integer i


The above var command declared an integer variable, whose variable name is i, and whose variable type is integer (int). Like any other variable,
  • An integer variable is accessed by prepending a dollar sign to the variable name.
  • The set command assigns the value to an integer variable.
  • The echo command shows the value of an integer variable.


Enter the following commands.

Commands
set $i=2
echo $i




The first command above sets the value of $i to 2. The second command shows that value.

We saw earlier that all commands take command arguments of type string. The echo and set commands are the only exceptions to that rule. The echo and set commands accept arguments of any type.

QUESTION 3.5 Among the following, which have the type integer ?
  1. "Hello world"
  2. 5
  3. 5.20
  4. 5.0
  5. "5.20"
  6. "5"
Answer



3.3. WHILE LOOP

We will learn about while loops in this section. But first, I want to show you how to create a new script. Going forward we will mostly type commands into a script, instead of typing them in the input area. Let's create a script called 'squares.txt'. We are calling it squares because it calculates squares of numbers. I like to store scripts in .txt files because they can be easily edited using any text editor. Thus, we will create this 'squares.txt' script in file 'squares.txt' . To create this script, do the following.

  1. Go to system viewer. Go to directory C:/LearningScripting/Lesson3. Create a new file called squares.txt. (To create a new file, you can right-click on an empty area in the right pane, select New, then select Text Document. This will create a new file called 'New Text Document.txt' . Right-click on this newly created file, select Rename, type in the file name 'squares.txt' . Depending on your system viewer, you may have to follow a different approach to creating a new file. Also, depending on your settings, you may (or may not) have to type the extension '.txt' as part of file name. Some viewer settings will "hide" the file extensions. I will refer to file names throughout, with extensions included in the file names.)
  2. Edit file squares.txt you just created. (To edit a file, double click on that file. Again, based on your system viewer, you may have to follow a different approach.)
  3. Now that the file is open in a text editor, copy and paste the following commands into the file.

    Script squares.txt
    var integer number power
    while ($number <= 30)
    do
        set $power = $number * $number
        echo $power
        set $number = $number + 1
    done


  4. Save the file squares.txt after entering above commands into it. (To save a file, press CTRL-S when the file is open in a text editor. You can also use the File menu, then select Save.)


Congratulations, you just created your very own first script . The script is stored in file C:/LearningScripting/Lesson3/squares.txt . I will explain the contents of this script step-by-step in a bit. The script prints squares of all numbers between 0 and 30. To verify this, let's go ahead and execute the script. Enter the following command in the input area of biterScripting.

Command
script "squares.txt"


Let me now explain the script we created command-by-command. The var command declared two integer variables - number and power. Until both $number and $power are assigned a value, their value will remain 0 (zero) by default.

The while loop provides a mechanism to execute something in a loop as long as some condition is true. That condition is called the while condition. In our case that condition is ($number <= 30). The operator <= means less than or equal to. We will look at all such operators in more detail in the next lesson.

The while command is the command that implements the while loop. It is followed by a command block.

The do and done commands create a command block. A command block is a sequence of commands - one command per line. Either all commands within a command block are executed, or no commands within a command block are executed.

The overall syntax of the while loop includes the while command, followed by the do command, followed by a number of commands, ending in a done command.

QUESTION 3.6 Using the system viewer, create a new script file cubes.txt. Copy and paste into it the script in file squares.txt. Change the cubes.txt script so that the script will produce cubes (3rd power) of numbers, instead of squares (2nd power). Answer





3.4. SPECIAL CHARACTERS

Text strings often include special characters or formatting characters. Newline, carriage-return, tab are some examples. From time to time, we need to use to them. What if we want to display tabs and newlines using the echo command ? All programming languages provide a way to handle. This is done by marking a special character with a backslash . The backslash is called the escape character. And this technique, is thus referred to as, escaping a character. I will show below some special characters and their representation using this technique.

\tTab
\nNew line
\rCarriage return
\\The backslash character itself
\"The double quote character


This representation can now be used in any biterscripting commands. Let's use the echo command with these special characters. Enter the following commands one by one. Each has one or more special characters. You will see their effect in the output.

Commands
echo "New Lines: \n Line 1 \n Line 2"
echo "Tab separated items: \n Item 1 \t Item 2"
echo "This string constant contains a double quote \" in it."
echo "This string constant contains a backslash \\ in it."


I added extra spaces around each escaped character in the above example. That was done for easy readability of the lesson. It is not necessary otherwise.

Any time you use a backslash on command line or within a script, it must be enclosed in double quotes. Otherwise you will get an error.



3.5. LEX COMMAND

The lex command (line extractor) extracts one line from an input string. The syntax of this command includes the lex command followed by a line number followed by the input string. Enter the following command.

Command
lex "1" "First Line\nSecond Line"


The above command extracts the first line of the input string and shows First Line. "1" is the line number. The input string is "First Line\nSecond Line". Note that the input string contains the special character - newline, represented as \n. This newline character tells biterscripting that the lines separate there. Now, enter the following commands.

Commands
var string s2
set $s2 = "First Line\nSecond Line"
lex "2" $s2


The lex command above will extract the second line from the string variable $s2. Thus, lex command can extract a line from either a string constant, or a string variable.

The lex command is part of a class of commands called the automated editors. We will look at automated editors in detail in a future lesson. Here, I just wanted to introduce you to the lex command because, in the next lesson, we will study a very commonly used, and very powerful, scripting technique called the file directory loop, which uses the lex command.

The lex command offers more than what we saw here. But, for now, it is just enough to know that the lex command extracts one line from an input string.

QUESTION 3.7 Using the lex command, extract the second line from the string "First Line\nSecond Line". Answer





DID YOU KNOW ?
  1. We know that the echo command shows the value of a variable. It can also show constants. Further, it can take any number of arguments and show each one of them. This can come in very handy. For example, in the script squares.txt we created, the echo command printed the squares. The echo command looked as follows.

    echo $power

    Let's say we want to make that script nicer, by printing a more verbose message such as,

    The square of number 3 is 9.

    To accomplish this we could use the echo command as follows.

    echo "The square of number " $number " is " $power "."

    The resulting script squares.txt will look like this.

    Script squares.txt
    var integer number power
    while ($number <= 30)
    do
        set $power = $number * $number
        echo "The square of number " $number " is " $power "."
        set $number = $number + 1
    done


    Try it out. You may like it.
  2. biterScripting allows a way to insert comments in scripts. This is done with the use of the sharp symbol (#) - this symbols is also known as the pound symbol.

    Any text starting with the sharp symbol (#) and ending with that line is considered a comment. biterScripting will not process a comment. It is meant merely for reader comprehension.

    I recommend using comments generously in your scripts. Think of comments in this way. The commands are instructions to the computer. The comments are instructions for a human reader. I will now present a commented version of the script squares.txt. This is just my style of commenting. You may develop your own style.

    Script squares.txt - With Comments
    # We will declare two integer variables. $number will hold the number. $power will hold the square of the number.
    var integer number power
    # Loop thru the numbers 0 through 30 one by one.
    while ($number <= 30)
    do
        # Calculate the square of the number.
        set $power = $number * $number
        # Show the square of the number.
        echo "The square of number " $number " is " $power "."
        # Increment number.
        set $number = $number + 1
    done






SUMMARY
In this lesson, we learned
  1. How to redirect streams.
  2. How to declare and use integer variables.
  3. How to create a new script.
  4. How to implement a while loop.
  5. How to represent special characters.
  6. How to use the lex command to extract a specific line from input string.




HOMEWORK 3
  1. Revisit the important concepts we learned in this lesson. (Look at this page from top to bottom. The important concepts are the words, phrases and sentences in red color.) Did you understand them ? Answer
  2. (This exercise is moderately hard.) What commands would you use the get the second line from file C:/LearningScripting/Lesson3/Sample.txt ? Answer
  3. (This exercise is significantly hard.) Read the help page for the lex command. What commands would you use the get the first two lines from file C:/LearningScripting/Lesson3/Sample.txt ? Answer





2008-2014, biterScripting.com. All rights reserved.
biterScripting, biterScript, biterBrowser, biterMobile, biterScripting.com, FVA (Forward Variable Assignment) are trademarks of biterScripting.com. Is it biterScripting-compatible ? is a service mark of biterScripting.com. Explorer, Unix, Windows are trademarks, service marks or other forms of intellectual property of their respective owners.