Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.

Similar presentations


Presentation on theme: "Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write."— Presentation transcript:

1 Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write Basic Shell Scripts Create and Use Variables

2 Start-up Files /etc/profile –belongs to root.bash_profile –belongs to user.bashrc –belongs to user.bash_logout (executed when you logout)

3 /etc/profile This file can only be modified by “root” Affects the environment of all Users Determines the History Size of all Users Users can change their environment by modifying the.bash_profile or the.bashrc files

4 .bashrc File.bash_profile File They are located in the user’s home directory These files are executed every time a user logs in. By modifying either one of these files, each user can change his individual working environment Can change your : prompt, screen display, create local variables, create new linux commands (aliases), map new keys on the key board, etc

5 The Shell Interpreter The Shell interprets every command line If it encounters any meta-characters such as *, ?, $ (there are many more), it will interpret the meta-character and in its place substitute whatever the meaning of that character is.

6 The Shell Interpreter - Continued e.g. Files in your current directory are: file1 file2 myfile yourfile You enter on the command line: ls file? The Shell interprets the command and substitutes each argument that contains the meta-character: ls file1 file2 This is the actual command that will be executed.

7 Shell Variables Primarily used for data storage Are always located in memory (fast access) 2 types of variables: –global/environment variables (permanent) –local/user defined variables (temporary) To view the contents of all variables, execute the command: set|more

8 Global Variables Global variables are assigned to each user as they login. Some Global Variables: HOME=user’s home directory HOSTNAME=matrix PS1=Prompt USER=user_nameHISTSIZE=1000 PATH=/dirX:/dirY:. TERM=vt220 SHELL=/bin/bashPWD=current_dir

9 Local Variables Created by the user, they vanish upon user logout. Created by a script, they vanish when the script ends. They can be used anywhere on the command line

10 Assigning Values to Variables Creating/Assigning a value to a variable: variable=value e.g. var=“Seneca College” Displaying the contents of a variable: echo $var The shell recognizes the $ as the variable name meta character, it will interpret the command and substitute the variable name with its contents, thus the interpreted command that will be executed is: echo Seneca College

11 Examples uname=jsmith file=datafile num=10 echo $uname $num $file grep “$uname” /etc/passwd grep “$uname” $file sort -k3 $file sort -k$num $file Write down the interpreted version of each one of these commands.

12 Exporting Variables uname=jsmith file=datafile num=10 The above created variables are only available to the current running shell. To make them available to any sub-shells spawned by the current running shell, they must be exported: export uname file num

13 Examples From the command line, execute the following commands in sequence: one=blue two=red echo “$two roses for a $one lady.” sh echo “$two roses for a $one lady.” exit export one two sh echo “$two roses for a $one lady.”

14 Input Data into Shell Scripts For the purpose of this course, you will only learn one way to input - from the terminal using the read command. The purpose of the read command is to pause the execution of the script until a user enters data from the terminal and presses the key.

15 Input Data into Shell Scripts  Examples of the read command:  read something  echo $something  read var1 var2  echo $var1  echo $var2 Shell script pauses until user types text (like hello there) and presses key. Text hello there is stored as the variable called something to be used in script Similar to above example, but multiple variables store text separately delimited (separated by) space(s) or tabs. For example, if user types hello there in this example var1 would store hello, var2 would store there

16 Input Data into Shell Scripts  WARNING: –It is highly recommended to avoid using variable names that are already reserved for other Unix/Linux commands or utilities. –Local variables (user-defined) names can be made up of any letter, the _, or any number, but must never start with a number! –A good suggestion is using the man or which utilities to determine if that name already exists!

17 alias - Creating New Commands As a user, you can create your own unique commands: alias la=“ls -a” alias dir=ls alias swho=“who |sort” Refrain from using existing command names as aliases, you will subvert the original command.

18 Exercises Make a backup copy of your.bash_profile file Using vi, open the file.bash_profile At the bottom, insert the following lines: PS1=‘My New Prompt$ ‘ LS_OPTIONS='-N --color=tty -T 0‘ alias la=‘ls -a’ echo -n “The number of users logged on is: “ who|wc -l log off and log back on again Notice any difference?

19 Exercises - Continued How many users are logged in the same cluster as you? Has your prompt changed? Execute the new command la Execute the command ls -is the output different now? Delete the PS1=‘My New Prompt$ ‘ line from the.bash_profile file. log off and log back on again, notice the old prompt is back If you wish, restore your original.bash_profile file by copying it from the backup you created.


Download ppt "Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write."

Similar presentations


Ads by Google