0% found this document useful (0 votes)
59 views

Lab 2

Lab 2

Uploaded by

Talha Tahir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Lab 2

Lab 2

Uploaded by

Talha Tahir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 2: Exploring the Bash Shell (part II)

Again, log in as Student (password of LinuxIsCool!) and open a terminal window.

1. Shortcuts: ~, tab completion, wildcards


a. The ~ symbol represents “home directory”. Type cd ~. Where are you? Type cd
~zappaf. What does your prompt now say? Use pwd to obtain the full path to your
location. Type cd ~foxr/HUMOR. What directory does your prompt show now?
b. Type cat bumperstickers to make sure it exists. Type cat b<tab> meaning
type cat b and then press the tab key. What happened? Type cat s<tab>. This emits a
beep (if you have speakers turned on) indicating that the interpreter could not uniquely
identify a file. Type cat s<tab><tab> (tab key twice). What happened? With cat
s on the command line, add m<tab>. What appears? Press <enter>.
c. Type cd /usr/bin. Type cat c<tab><tab>. How did pressing <tab> twice
differ from in step 1b? You may answer y or n to this.
d. Type cd ~/DUMMY-DIRECTORY. Type ls and then ls *.txt. Compare the
results.
e. Type cd directory2. Type ls to view the two files. Type ls aa?.*. Why did
aa1.abC match but not aa10.bBC? Now, type cd ../directory3/c1. Type ls
foo?. Why does foo not appear but foo2 and foo3 do? Explain what the ? does.
f. Return to DUMMY-DIRECTORY (..) and type ls *.[bc]*. Which files are listed?
Why did aaa.abc not appear? What does the [ ] indicate?

2. Command Line Editing


a. In ~/DUMMY-DIRECTORY there are files named file1.txt – file4.txt. Let’s rename
them to end with .dat. The mv instruction (used to rename files) can only rename one file
at a time. Enter mv file1.txt file1.dat. The novice Linux user would then
retype this instruction 3 times changing the numbers each time (from 1 to 2, etc).
Command line editing will save you time and effort. How? Type control+p. What
does this do? Type control+b. What does this do? Repeat this until you reach the “.”
before the .dat. Hit the backspace key to erase the “1”. Type 2. Now type
escape+b (hit the escape key and then type b) two times. What does escape+b do?
Type control+b to move back to the period and then backspace to erase the “1”. Type
2. Press <enter> to enter the instruction. Repeat this operation to change the 2’s to 3’s.
Repeat this again to change the 3’s to 4’s. Did this get easier with each attempt?
b. To create an empty file, type touch filename. Use touch to create a_file.txt using
touch a_file.txt. Now, create a new file called b_file.dat using command
line editing. Explain the specific steps needed to perform the operation. Notes: control+a
moves you to the beginning of a line, control+e to the end, you do not need to be at the
end of the line to submit the changed instruction, just press <enter> as soon as you are
done.
c. What do each of these do: control+k, control+y, escape+f, control+d, escape+d. You
might look these up in the textbook (table 2.13).
3. A pipe ( | ) redirects the output of one program (command) to be the input to another.
a. Type cd /etc and type ls. The files scroll past. We used less and more earlier to
display output one screen at a time. Type ls | more. What happens? More
responds to the space bar, <enter> and s and q. What do each of the space bar, enter, s
and q do? Type ls | less. What happens? The less command is controlled by the
same keys plus the arrow keys, <page_up>, <home>, etc. Experiment with less and
summarize how it differs from more. At the bottom of the screen to continue scrolling,
what appears when using more and what appears when using less?
b. Type ls | sort –r | less. What does this command do?
c. Change directory to ~zappaf/ZAPPA. Type the following command cat c* | grep
“f$” | sort. Explain what each part of this instruction does. The $ in “f$” says
“match an ‘f’ that ends the line”.
d. cd back to /etc. The command wc filename will output the number of characters
(bytes), words and lines in filename. We can combine ls and wc to count the number of
files. For instance, ls | wc –l will count the number of lines output by ls, or the
number of files. Try it. Now, use ls and a pipe to wc to find out how many files in /etc
start with the letter ‘c’. How many did you find? What command did you enter?

4. File redirection is used to alter the input/output of a command.


a. While in /etc, type ls > ~/etc-files.txt. This command performs an ls and
sends the output to the file etc-files.txt rather than to the window. Why is the ~/
necessary? HINT: try it without the ~/.
b. Change directory to /usr/bin. Type ls >> ~/etc-files.txt. cd to your home
directory and type less etc-files.txt and examine ~/etc-files.txt. Explain the
difference between > and >>.
c. Most Linux commands obtain input from files rather than keyboard so there is little need
for the < redirection. Let’s see. Type less ~/etc-files.txt followed by type
less < ~/etc-files.txt. Was there any difference between using < and not
using it?
d. Now let’s try <<. This works as command << string where command then obtains input
from keyboard until you type string. Type wc << quit. Now type the following:
Hello there<enter>, how are you today?<enter>, quit<enter>.
What was the output of the command? Now try wc << <enter> and type in some
sentences. How do we end the input since <enter> does not? Type control+d.
e. Let’s combine << and > to create a text file using cat. From your home directory, type
cat << quit > list.txt. Now enter any list of items (e.g., grocery items, band
names) but unsorted, and type quit to end. What output are you given? Type less
list.txt. You should see your list. Type the command cat << quit | sort >
list2.txt. Enter the same list. Type less list2.txt. How does this version of
the file differ? Explain each part of the command you entered.
f. We cannot use << and > without a word in between. Try cat << > list2.txt.
What response do you get?

Page | 1
5. If you enter a variable or alias, it only lasts during this session. You can instead define these
in one of several files so that they are available in any and every shell.
a. First, let’s do an experiment. Type X=0 <enter>. Type echo $X. Now type bash.
This starts a subshell (or inner session). Type echo $X. What response do you get?
Why? Type exit to resume your outer session and type echo $X. What did you get
this time?
b. We can make a variable accessible in other shells by exporting it. Type export X.
Type bash and from the inner session, type echo $X. What response did you get now?
Type X=1 to change X’s value. Type exit and echo $X. What value is X in this outer
shell? Why? Type export to list all exported variables in this shell. All but one of them
are environment variables defined in various scripts; one is X. It should say X=“1”. Type
exit to return to your outer session and type export. You will find in this list X=“0”
instead.
c. Type cd ~ to return to your home directory if you are not already there. Type
cat .bashrc. This script runs every time you start a new Bash session. It performs
only three operations: if /etc/bashrc exists, it runs that script; it adds two directories to
your PATH variable and it exports PATH. If you explore /etc/bashrc, you will find that
that script creates other environment variables like PROMPT_COMMAND, PS1 and
PATH. If you want to alter the values of your PS1 or PATH variables, would you do so
from the command line, from /etc/bashrc or ~/.bashrc? Why?
d. In your home directory, look at the contents of the file ~/.bash_profile. What does this
script do that .bashrc did not?
e. In comparing the two files (.bashrc, .bash_profile), there is a comment at the bottom of
each that explain what you might use the file for. What should you use .bashrc for and
what should you use .bash_profile for?
f. While in your home directory, type echo “alias me=‘ps aux | grep
Student’” >> .bashrc. This instruction is an echo statement to output the text
“alias me=‘ps aux | grep Student’” and redirect the output to the file .bashrc. View
your .bashrc file and you should find this entry at the bottom of the file. Thus, you have
added an alias to the file. You would more conveniently edit .bashrc (or .bash_profile)
using vi, but you haven’t learned vi yet (it’s the topic of the next lab). Explain what the
alias does. Type me. Why did it not work? Even though we’ve changed the .bashrc file,
we need our Bash session to re-read this file. To do so, type source .bashrc. Type
me What happens this time? Type alias to see that me has been defined. What does
source do?

Close any terminal windows and shut down your VM.

Page | 2

You might also like