190 likes | 305 Views
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. Start-up Files. /etc/profile belongs to root .bash_profile belongs to user .bashrc
E N D
Basic Shell Scripting - Part 1Objective - 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
Start-up Files • /etc/profile • belongs to root • .bash_profile • belongs to user • .bashrc • belongs to user • .bash_logout (executed when you logout)
/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
.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
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.
The Shell Interpreter - Continued • e.g.Files in your current directory are: file1 file2 myfile yourfileYou enter on the command line:ls file?The Shell interprets the command and substitutes each argument that contains the meta-character:ls file1 file2This is the actual command that will be executed.
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
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_name HISTSIZE=1000 PATH=/dirX:/dirY:. TERM=vt220 SHELL=/bin/bash PWD=current_dir
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
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
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.
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
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.”
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 <ENTER> key.
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 usertypes text (like hello there) andpresses <ENTER> key. Texthello there is stored as the variablecalled something to be used in script Similar to above example, butmultiple variables store textseparately delimited (separated by)space(s) or tabs. For example, if usertypes hello there in this examplevar1 would store hello, var2 wouldstore there
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!
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.
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?
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.