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 2
(Introduction to variables, Detailed look at a command)

In this lesson, we will introduce the concept of a variable. We will look in detail at the command structure and its various important aspects. As part of the discussion, we will also learn a few new commands, which we will use to emphasize some of the concepts.



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/Prep2.txt"


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

    Command
    ccd "C:/LearningScripting/Lesson2"








2.1. VARIABLES

No programming language will be complete without variables. A variable is a place-holder for a value. Each variable has a type, it is called the variable type. The variable can hold values only of that type. biterScripting provides various types. In this lesson, we will use variables of type - string. The string type is abbreviated as str. A string is a string of characters. We will explain the concepts of variable and string with examples. Enter the following command.

Command
var string s


The var command declares a variable. It takes two or more arguments. The first argument is the variable type, the second argument is the variable name. In this example, we named the variable   's'  . The var command can be used to declare more than one variable. Enter the following command.

Command
var string s2 s3 s4


Above, we declared three variables. Their variable names are s2, s3, and s4. Their variable type is string. A variable name can be any combination of characters, digits, underscores, etc. The first letter of the variable name must not be a digit. Further, a variable by the same name can only be declared once. Type the following command.

Command
var string s


What did the above command do ? You got the error 'Variable s is already declared.' .

Once a variable is declared, it can be accessed by prepending a dollar sign ($) to its name. Enter the following command.

Command
echo $s


The echo command displays the value of a variable. The output of the above command is - nothing. This is because variable s has not been assigned a value yet. The variable value is the value a variable holds. Let's go ahead and assign a value to variable s using the following command.

Command
set $s = "This is my first string value."


The set command above assigned the value "This is my first string value." to variable s. The syntax of the set command has the word 'set', followed by a variable, followed by the equal sign (=), followed by a value. Enter the echo command above again to verify that the value of variable $s is correctly set to "This is my first string value." .

Command
echo $s




QUESTION 2.1 We declared variables s2, s3 and s4 above - all of variable type string. Assign them values "second value", "third value" and "fourth value" respectively. Answer



2.2. COMMANDS - A DETAILED LOOK

We saw so far, that commands have arguments. In case of help command, we also saw that some commands have options. (The help command has the option -s (search) . ) Let's look at all aspects of a command.

Figure 2.1 - biterScripting Command
Figure 2.1 - biterScripting Command


As you can see, there are five aspects to a command.

  • Options
  • Arguments
  • Stream Input
  • Stream Outout
  • Stream Error

I will explain them all one by one.



2.3. COMMAND ARGUMENTS

Command arguments are objects on which the command will operate. We looked at some command arguments in the last lesson. We used a directory name or path as a command argument for the ccd command. We used a help topic or search word as a command argument for the help command. Command arguments can be anything depending on the command. They can be file names, directory names, file paths, directory paths, constants (double quoted strings), variables, words, etc. What type of an object, depends on what that particular command is expecting.

Most commands take arguments of type string. Thus, for these commands, whichever way you specify a command argument, it must always result in data of type string. Following are the various ways in which you can specify command arguments. I will show an example for each.

A Constant"This is my string argument."
A variable$s
A function
call
gettime()
An inline
command
{ echo "Pass this string to the command." }
An expression
consisting of
any of above
("This is my string argument."+" But, you can use it.")


All of the above must return data of type string. (You don't know what function calls, inline commands and expressions are yet - and, that's ok. I listed them here just to make this discussion complete.) The point is, there are several ways to pass arguments to a command. That's the strength of biterScripting. You do not have to know up-front what the arguments are. You do not need to hardcode them. I will soon show you some examples of how to harness this power.

QUESTION 2.2 Among the following, which have the type string ?
  1. "Hello world"
  2. 5
  3. 3.20
  4. "3.20"
Answer



2.4. COMMAND OPTIONS

Command options provide directions to the command as to how it is to operate on command arguments. Command options are specified with a dash (-) followed by a letter. We saw that help command accepts an option -s. This option directs the help command to treat command arguments as search words. Without this option, the help command treats command arguments as help topics.

QUESTION 2.3 Read the help page for the lf command. We will need it for further discussion in this lesson. Also, lf is one of those commands that you will use often. Answer

Many commands take more than one option. For example, the lf command (list files/directories), takes the following options.

-r (recursive)
-g (go around errors)
-t (list type)
-c (list creation time)
-a (list access time)
-m (list modification time)
-s (list size)
-n (list name)

You can specify command options in any order. Also, you can combine two or more command options after a single dash. All of the following are valid ways of specifying command options for the lf command.

-n (list name)
-c -n (list creation time and name)
-n -c (same as -c -n, list creation time and name)
-cn (same as -c -n, list creation time and name)
-s -cn (list size, creation time and name)
-scn (same as -s -cn, list size, creation time and name)



2.5. OUTPUT, ERROR AND INPUT STREAMS

A stream is a collection of bytes. Output from a command, errors from a command and input to a command are called streams. (There is some significance to using the word 'stream'. In programming world, you may have also heard the words standard output, standard error, standard input, stdout, stderr, stdin, etc. For our purposes, they all mean one and the same. They are all collections of bytes.)

Stream output is where a command writes its output. By default this output goes to the screen.

Stream error is where a command writes errors encountered during execution of the command. By default, these errors also go to the screen.

Stream input is where a command reads its input from. It is the input that the command will operate on, in addition to the command arguments. By default, stream input is empty.

Let us execute some commands and look at their stream output, error and input. Enter the following command.

Command
lf -rn "*" "C:/LearningScripting"


The lf command lists files and directories within a directory. The "C:/LearningScripting" is the directory. "*" is the pattern for file/directory names. -r means recursive. -n means list file/directory name. The lf command always lists file and directory names with full path, starting with "C:/"

The stream output of the above lf command contains file and directory names, one per line. This stream output appears on screen by default.

QUESTION 2.4 In the lf command we entered above, what command options and command arguments did we use ? Answer

Now, enter the following command.

Command
ccd "C:/LearningScripting/XYZ"


The stream error from the above ccd command contains a string such as 'Unable to change current directory to C:/LearningScripting/XYZ .' . (We are assuming that the directory C:/LearningScripting/XYZ does not exist.) This stream error appears on screen by default.



DID YOU KNOW ?
  1. We saw that we can use pattern-matching character star (*) in the lf command. The repro command also accepts star (*) as a pattern-matching character. Enter the following commands.

    Commands
    ccd "C:/LearningScripting/Lesson2"
    lf -n "*.txt"
    repro "*.txt"


    The lf command above will list all files with extension .txt . Similarly, the repro command will reproduce the contents of all files with extension .txt .
  2. biterScripting keeps a history of commands you type. Couple of keys on the keyboard are programmed to navigate through this command history. The F3 key is used to recall previous command. The F4 key is used to recall next command. When you use these keys, biterScripting will bring in the appropriate command into the input area. This convenience comes in handy when you are typing long commands again and again. Instead of having to retype the entire command again, just recall the previous command using the F3 and F4 keys, make necessary changes, if any; then hit ENTER to send the command for processing. Try the F3 and F4 keys now.

    If the cursor is in the input area, the UP and DOWN keys work the same way as F3 and F4 respectively. All such keyboard shortcuts are described in the help topic - keyboard . To see this help topic, type the following command.

    Command
    help keyboard






SUMMARY
In this lesson, we learned
  1. How to declare variables. We learned one variable type - string.
  2. Detailed aspects of a command - arguments, options, stream output, stream error, stream input.
  3. Some additional commands - var, echo, set, lf.




HOMEWORK 2
  1. Revisit the important concepts we learned in this lesson (words, phrases or sentences in red color). Make sure you understand them. Answer
  2. Using the lf command, list all files with extension .txt in directory C:/LearningScripting (and its subdirectories). 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.