A Shell Programmers: Mohammad Hassan Ghaed(83105525) Farhaad Pashakhanloo (83112018) Teacher: Ali Kardani 1 Table of contents: The Explorer Variables Conditional Structures Loops Batch Files Redirecting the Output External Commands 2 The Explorer Using the Shell you can browse the computer for files and directories. You can set the Shell’s Path’s. The Shell enables you to execute .exe files by typing their names. Here are some of the commands involving the explorer: Cd The cd command changes the current directory. It can be used in different ways: Syntax1: cd [directory to change to] Syntax2: cd .. The first syntax changes the working directory to a specific one and the second syntax changes the current directory to the one which is one level upper than current directory. Path The path command can be used in two ways: Syntax1: path Syntax2: path [path to append] The first syntax lists current paths The second syntax appends the path in the first argument to path and then lists current paths Clearpath The clearpath command can be used to delete all current paths Syntax :clearpath Dir The dir command lists the directories and files in current directory Syntax: dir 3 Variables You can define your own variables as shell variables. Variables may be of type int/float/string. Variables can be set to their initial value at the time of creation or can be assigned to valid values using assignment syntaxes. Variables can be used in FOR and IF structures in batch files. These are the commands involving variables: Int The int command is used to define a new variable of type int Syntax: int [variable name] [optional variable value] This creates an integer variable named [variable name] with initial value of [optional initial value] If no value is specified 0 is assigned to the variable Float The float command is used to define a new variable of type float Syntax: float [variable name] [optional variable value] This creates a floating point variable named [variable name] with initial value of [optional initial value] If no value is specified 0 is assigned to the variable String The string command is used to define a new variable of type string Syntax: string [variable name] ["optional variable value"] This creates a string variable named [variable name] with initial value of [optional initial value] If no value is specified, “” is assigned to the variable Clearvars The clearvars command removes all the variables from the memory Syntax: clearvars 4 Assignments Syntax1: [variable] = [variable/value] Syntax2: [variable] = [variable/value] [operand] [variable value] In the first syntax the right and left side of the equation must be of the same type. Note that for string values double quotations should be used. In the second syntax, in case of integer and floating point values and variables any assignment is valid. This includes operands +-/*. But when using strings in the second syntax only the + operand is valid and it adds the second variable to the end of the first one (cats it.) 5 Conditional Structures The If commands is used to execute a block of commands if a condition is satisfied. Syntax: if [1st variable name/value] [</=/>] [2nd variable name/value] { [Block of commands which may contain internal FOR's and IF's] } else { [block of commands which may contain internal FOR's and IF's] } Note that if command can only be used in batch files. 6 Loop structures The for command is used to execute a block of commands several times in *.shl batch files Syntax: for [variable name] [initial value] [final value] { [block commands which may contain internal FOR's] } If the variable does not exist it is created automatically Note that for command can only be used in batch files. 7 Batch Files The Shell can run files with the extension .shl as batch files. Each line of the Batch file is interpreted as user input. User can pass arguments to batch file. These arguments must be separated by spaces. At any point in the batch file you can access these arguments by using %n syntax where n specifies the argument number starting from 1. Note that for and if commands can only be used in batch files. 8 Redirecting the Output At any time the output of the Shell can be redirected to a file using out command. Syntax: out [std/target file path] For example the following line redirects the output to the file c:\new.txt Out c:\new The redirection is continued until the Shell reaches this line: Out std After that the output will be printed to the monitor. There is another way of redirecting the output of one line: Syntax: [commandline to execute] > [target file path] 9 External Commands At present, the Shell has two external commands. These commands are precompiled exe files that get their arguments from the shell. Anyone can add his own external commands to the shell easily. Help Syntax1: help Syntax2: help [command] The first command guides the user how to use help The second syntax gives information on any command Copy The copy command is an external command like help and is executed using a compiled exe file and passing the arguments to it Syntax: copy [source file] [destination directory] This syntax copies the source file to the destination. If the source file is in current path there is no need to specify its path 10