Sie sind auf Seite 1von 6

Operating Systems

Laboratory Exercise 4

Basic Shell Scripting


Objectives: At the end of the session, the students are expected to: become familiar with basic shell scripts use proper commands for shell scripts construction Materials: 1 PC with Linux Procedures: ASSUMPTIONS: user is already familiar with basic Linux commands user is already familiar with vi utility STEPS: 1. It is mandatory that you use an editor program such as vi or pico. Most Linux variants make use of vi as the primary editor. 2. Upon the completion of the script file, your task is to modify the permission of the file such that it can be executed. 3. Lastly, execute your file. TRY THIS: 1. Log on to your Linux account. 2. Once you are logged under your directory, create a file called myFirst using vi editor and type the following inside vi. $vi myFirst #This is my first program clear echo THIS IS EASY #echo This command is not executed Note: The # symbol in shell scripting is used as a REMARK. This simply means that any command or statement placed after the # symbol will be disregarded. The # symbol is useful for inserting comments on the program.

Operating Systems

3. Once the file is saved, your next task is to modify the file access permission such that it can be executed. The command is: $ chmod 755 myFirst This simply means that the owner of the file will have full access (Read, Write, and Execute). Group and Other users will have an access right of Read and Execute. Confirm that the permission has been changed by simply typing the command $ ls -l. This command will give a long list of files in your current directory. The output should look something like this: -rwxr-xr-x 1 benz benz 81 Jul 12 00:49 myFirst 4. All that is left to do is to execute the script file. There are many ways on how the file can be executed. These are the following: $ bash myFirst $ sh myFirst $ ./myFirst Your task is to create a new script file called myInfo. This file will simply print personal details of yourself such as name, address, age, gender, phone number, mobile number, course, and year level. You should format your output in the following manner. Format: Name: Peter Parker Address: New York Phone Number: 1-800-258647 Mobile Number: 0917-1234567 Be sure that the output follows the desired format. What happens if the access permission was not changed and ./myInfo is executed? What happens if the chmod 444 myInfo command is used for changing the access right of the file when ./myInfo is executed? Age: 25 Sex: Male Course: Photography Year Level: 3
Basic Shell Scripting

Operating Systems

MORE ON SCRIPTING Under shell scripting, it is possible that the user may use ordinary commands such as ls, cd, cal, who, whoami, etc. that may be necessary for a certain script. For instance, $ vi myInfo # # # Script to print user information who currently login, current date # & time # clear echo "Hello $USER" echo "Today is `date` echo "Number of user login : `who | wc -l` echo "Calendar" cal Note: Use the ` (back quote) sign, not the (single quote) sign. Back quote is generally found with the tilde (~) key or above the TAB key on a PC keyboard. Exercise: Write down this script using vi and save the file as myInfo. Execute this script and write down the result. SHELL SYSTEM VARIABLES Linux has many kinds of shell variables. For instance, the $USER contains the name of the current user in a certain terminal. In other words, this command is also equivalent to whoami command. Other shell variables include the following: $BASH $BASH_VERSION $COLUMNS $HOME $LINES $LOGNAME $OSTYPE $PATH $PS1 $PWD $SHELL $USERNAME Write down the result of each of the shell variables by typing $ echo <shell variable> For example, $ echo $USER.
Basic Shell Scripting

Operating Systems

USER DEFINED VARIABLES (UDV) In Linux, it is possible that the user may define its own variables. This may be necessary to simplify certain commands or simply combine multiple commands into one command execution/call. Warning: Do not modify system variables because this can sometimes create problems. Treat the system variables as reserved words in programming. To define UDV, use the following syntax: Syntax: variable name=value 'Value' is assigned to a given 'variable name' and value must be on right side of the = sign. Examples: This is ok: $ no=10 Error, not ok, and the value must be on right side of the = sign: $ 10=no To define a variable called 'vech' having value Bus: $ vech=Bus To define a variable called n having value 10: $ n=10
Basic Shell Scripting

Operating Systems

CONVENTIONS IN NAMING VARIABLE NAMES Variable names should begin with alphabetic characters or underscore ( _ ) followed by one or more alphanumeric characters. Examples: HOME SYSTEM_VERSION vech no Do not put spaces on either side of the equal sign when assigning a value to a variable. Examples: $ no=10 $ no =10 $ no= 10 $ no = 10 <--- This is correct <--- This is wrong <--- This is wrong <--- This is wrong Variables are case-sensitive, just like commands and filename in Linux. Example: Assuming we have created the following variables: $ no=10 $ No=11 $ NO=20 $ nO=2 The following commands will result to a different output: $ echo $no $ echo $No $ echo $nO <--- will print 10 but not 20 <--- will print 11 but not 20 <--- will print 2 but not 20
Basic Shell Scripting

Operating Systems

You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition): $ vech= $ vech="" When trying to print the value of a NULL variable like this: $echo $vech <--- nothing is shown since it is null PRINTING SHELL VARIABLE VALUES In order to print or view the value of a variable, follow this syntax: $ echo $variable name For example: $var=27 $echo $var Warning: <--- you have created a variable called var with a value 27 <--- this command will print the current value of variable Do not try $ echo var, as it will print var instead its value 27. You must use $ followed by variable name. Write down the command to: Define variable x with value 10 and print it on screen. Define variable xn with value BenZ and print it on screen. Point out the errors, if there is any, in the following script. $ vi myScript # # # Script on script debugging # myname=Linus Torvalds myos = TroubleOS myno=5 echo "My name is $myname" echo "My os is $myos" echo "My number is myno, can you see this number" Rewrite this script such that it does not result to any error.
Basic Shell Scripting

Das könnte Ihnen auch gefallen