Exercise 3.1 - Working with Variables, Parameters, and Aliases v3.pdf
salmamontaser1
9 views
10 slides
Mar 03, 2025
Slide 1 of 10
1
2
3
4
5
6
7
8
9
10
About This Presentation
xc
Size: 27.04 KB
Language: en
Added: Mar 03, 2025
Slides: 10 pages
Slide Content
Linux
Exercise 3.1: Working with Variables, Parameters, and
Aliases
Dr. Mahran Al-Zyoud
Summer 2023/2024
In this exercise, practice creating and removing variables,
aliases, and shell options as well as editing shell configuration
files.
But first, when executing a command, be sure to test the
results. For example, to open a child shell, use the following
procedure:
1.Verify the process ID (PID) of the current shell using the
psorps -fcommand.
2.Create the child process (bash).
3.Verify you are in a child shell by executing theps -f
command and then look for aPIDwhose parent process
(PPID) is the same as you verified in step 1.
1
With that in mind, log on to the virtual machine and then
follow the following steps:
1.Right-click the Desktop and select Open Terminal. This
opens a text terminal (pseudo-terminal).
2.Execute thepscommand to display a list of processes
executing on the current terminal. Notice the process
bash. Also notice the process ID associated with the
commandbashin thePIDcolumn. This is your current
shell.
3.Execute thebashcommand.
4.Execute the commandps -f. Look at the second instance
of bash and itsPID. Now look at the parent process ID
(PPID). ThePPIDis the process number of the shell that
spawned the current shell (child). The second instance of
bash is the child shell.
2
5.Type theexitcommand. This terminates the current shell
and returns you to the parent shell.
6.Execute thepscommand. Notice thePID.
7.Create the shell variable flower and assign it the value
rose by executing theflower=rosecommand.
8.Execute the commandecho $flowerto see if the variable
has been created. Execute thesetcommand to see all
shell variables and theset | grep flowercommand to
view the shell variable flower.
9.Create the variablenutand assign it the valuealmondby
executing thenut=almondcommand. Test to see if the
variable has been created.
3
10.Open a child shell by executing the commandbashand
test to ensure you have created a child shell.
11.Determine if the variablesflowerandnutare present in
the child shell.
12.Return to the parent shell.
13.Add anexportattribute to the variableflowerby executing
the commandexport flower.
14.Open a child process by executing the commandbash.
Use the commandsecho $flowerandecho $nutto
determine if either the variableflowerornutexists in the
child process. Explain the results.
4
15.Return to the parent shell by executing theexit
command.
16.Create an environment variable calledfruitand assign it
the valueappleby executing the command
export fruit=apple.
Test to determine the following:
•The variable has been created and is a shell variable in the
current shell. To do this, type the commandecho $fruitor
set | grep fruit.
•The variable has been assigned anexportattribute. To do
this, use the commandenv | grep fruit.
17.To view the status of all shell parameters, execute the
commandset -o.
18.Type the commandset -o | grep allexportto view the
status of the shell parameterallexport.
5
19.Type the commandset -o allexport. This turns on the
bash parameter, which automatically applies anexport
attribute to all newly created variables.
20.Verify theallexportparameter has been turned on by
executing the commandset -o | grep allexport.
21.Create the variabletruckand assign it the valuechevyby
executing thetruck=chevycommand. Execute the
commandenv | grep truckto determine if the variable
has been assigned anexportattribute.
22.Turn offallexportby executing the command
set +o allexportand verify the shell parameter has been
turned off.
23.Create the aliasldetc, which will execute the commandls
-ld /etc, by executing the command
alias ldetc=’ls -ld /etc’.
6
24.Type the commandaliasto verify the aliasldetchas
been created. You could also type the command
alias | grep ldetc.
25.Type the commandldetc.
26.An alias only exists in the shell in which it is created. To
test this, open a child shell and perform the following
procedure:
a)Execute thepscommand to determine what thePIDof
the current shell is.
b)Execute thebashcommand to open a new shell.
c)Execute the commandps -fto verify a child shell has
opened by making certain thePPIDof the current shell is
thePIDdiscovered in step a.
d)Determine if the alias created in step 23 exists by trying to
executeldetc. Alternatively, execute the commandalias,
alias ldetc, oralias grep | ldetcto list the aliases in
memory.
7
27.Return to the parent shell by exiting the current process (exit).
28.Remove the aliasldetcby executing the command
unalias ldetc. Use the commandalias,alias ldetc, or
alias | grep ldetcto verify the alias has been removed from
memory.
29.Usevito addset -o noclobberto the last line of the file
/home/ubuntu/.bashrcand then save the file. The shell option
noclobberdoes not allow a user to redirect the standard output
(>) to an existing file.
30.Execute the commandset -o | grep noclobber. Isnoclobber
turned on or off? Why?
31.Start a child process by executing the commandbash.
32.Determine if the shell optionnoclobberis turned on or off.
Why?
33.Return to the parent process. Execute the command
source /home/ubuntu/.bashrc. Isnoclobberturned on or off?
Why?
8
Answers to Exercise 2
•Step 29)vi /home/ubuntu/.bashrc
•Step 30)noclobber will be turned off. After you edit a
configuration file, the changes must be read into memory.
•Step 32)noclobber is turned on. The file /home/ubuntu/.bashrc
is read each time a process is opened; therefore, changes were
read into memory.
•Step 33).bashrc is read when a child shell is created or a source
command is issued. When you made the first change to .bashrc
in the parent shell, you did not source .bashrc, so noclobber
was off in the parent shell. When you opened a child shell (step
31), .bashrc was read; therefore, noclobber was turned on in the
child shell. When you returned to the parent shell, noclobber is
turned off. The command source /home/ubuntu/.bashrc
executes each line in /.bashrc (including set -o noclobber). This
command turns noclobber on in the current shell.
9