0% found this document useful (0 votes)
174 views23 pages

Bash Shell Projects for Linux Certification

Uploaded by

jamespromiii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views23 pages

Bash Shell Projects for Linux Certification

Uploaded by

jamespromiii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Book Title: eTextbook: COMPTIA Linux Guide to Linux Certification

Chapter 7. Working with the Bash Shell


Hands-On Projects

Hands-On Projects
These projects should be completed in the order given. The hands-on projects

presented in this chapter should take a total of three hours to complete. The

requirements for this lab include:

A computer with Fedora Linux installed according to Hands-On Project

2-1.

Project 7-1
In this hands-on project, you use the shell to redirect the stdout and stderr to a
file and take stdin from a file.

Boot your Fedora Linux virtual machine. After your Linux system
1
has been loaded, switch to a command-line terminal (tty5) by

pressing Ctrl+Alt+F5 and log in to the terminal using the user

name of root and the password of LINUXrocks!.

At the command prompt, type touch sample1 sample2 and press


2
Enter to create two new files named sample1 and sample2 in your

home directory. Verify their creation by typing ls –F at the


command prompt, and press Enter.

At the command prompt, type ls -l sample1 sample2 sample3 and


3
press Enter. Is there any stdout displayed on the terminal screen?

Is there any stderr displayed on the terminal screen? Why?


At the command prompt, type ls -l sample1 sample2 sample3 >
4
file and press Enter. Is there any stdout displayed on the terminal

screen? Is there any stderr displayed on the terminal screen?


Why?

At the command prompt, type cat file and press Enter. What are
5
the contents of the file and why?

At the command prompt, type ls -l sample1 sample2 sample3 2>


6
file and press Enter. Is there any stdout displayed on the terminal

screen? Is there any stderr displayed on the terminal screen?


Why?

At the command prompt, type cat file and press Enter. What are
7
the contents of the file and why? Were the previous contents
retained? Why?

402
At the command prompt, type ls -l sample1 sample2 sample3 >
8
file 2>file2 and press Enter. Is there any stdout displayed on the

terminal screen? Is there any stderr displayed on the terminal


screen? Why?

At the command prompt, type cat file and press Enter. What are
9
the contents of the file and why?

At the command prompt, type cat file2 and press Enter. What are
10
the contents of the file2 and why?

At the command prompt, type ls -l sample1 sample2 sample3 >


11
file 2>&1 and press Enter. Is there any stdout displayed on the

terminal screen? Is there any stderr displayed on the terminal


screen? Why?
At the command prompt, type cat file and press Enter. What are
12
the contents of the file and why?

At the command prompt, type ls -l sample1 sample2 sample3 >&2


13
2>file2 and press Enter. Is there any stdout displayed on the

terminal screen? Is there any stderr displayed on the terminal


screen? Why?

At the command prompt, type cat file2 and press Enter. What are
14
the contents of file2 and why?

At the command prompt, type date > file and press Enter.
15

At the command prompt, type cat file and press Enter. What are
16
the contents of the file and why?

At the command prompt, type date >> file and press Enter.
17

At the command prompt, type cat file and press Enter. What are
18
the contents of the file and why? Can you tell when each date

command was run?

At the command prompt, type tr o O /etc/hosts and press Enter.


19
What error message do you receive and why?

At the command prompt, type tr o O </etc/hosts and press Enter.


20
What happened and why?

At the command prompt, type tr o O <<EOF and press Enter. At


21
the secondary prompt, type oranges and press Enter. Next, type
Toronto and press Enter, type Donkey Kong and press Enter, and

then type EOF and press Enter. Note the output. What is this type
of input redirection called?
Type exit and press Enter to log out of your shell.
22

Project 7-2
In this hands-on project, you redirect stdout and stdin using pipe
metacharacters.

Switch to a command-line terminal (tty5) by pressing Ctrl+Alt+F5


1
and log in to the terminal using the user name of root and the

password of LINUXrocks!.

At the command prompt, type cat /etc/services and press Enter to


2
view the /etc/services file. Next, type cat /etc/services | less at the
command prompt and press Enter to perform the same task page-
by-page. Explain what the | metacharacter does in the previous

command. How is this different from the less /etc/services


command?

403
At the command prompt, type cat /etc/services | grep NFS and
3
press Enter. How many lines are displayed? Why did you not need
to specify a filename with the grep command?

At the command prompt, type cat /etc/services | grep NFS | tr F f


4
and press Enter. Explain the output on the terminal screen.

At the command prompt, type cat /etc/services | grep NFS | tr F f


5
| sort –r and press Enter. Explain the output on the terminal
screen.
At the command prompt, type cat /etc/services | grep NFS | tr F f
6
| sort –r | tee file and press Enter. Explain the output on the
terminal screen. Next, type cat file at the command prompt and

press Enter. What are the contents? Why? What does the tee
command do in the pipe above?

At the command prompt, type cat /etc/services | grep NFS | tr F f


7
| sort –r | tee file | wc –l and press Enter. Explain the output on

the terminal screen. Next, type cat file at the command prompt
and press Enter. What are the contents and why?

At the command prompt, type cat /etc/services | grep NFS | tr F f


8
| sort –r | sed /udp/d | sed /tcp/s/mount/MOUNT/g and press
Enter. Explain the output on the terminal screen. Can this output

be obtained with the grep and tr commands instead of sed?

At the command prompt, type cat /etc/hosts . Next, type cat


9
/etc/hosts | awk ’/localhost/ {print $1, $3}’ and press Enter.
Explain the output on the terminal screen.

Type exit and press Enter to log out of your shell.


10

Project 7-3
In this hands-on project, you create and use an alias, as well as view and
change existing shell variables. In addition to this, you export user-defined
variables and load variables automatically upon shell startup.

Switch to a command-line terminal (tty5) by pressing Ctrl+Alt+F5


1
and log in to the terminal using the user name of root and the
password of LINUXrocks!.
At the command prompt, type set | less and press Enter to view
2
the BASH shell environment variables currently loaded into
memory. Scroll through this list using the cursor keys on the

keyboard. When finished, press q to quit the less utility.

At the command prompt, type env | less and press Enter to view
3
the exported BASH shell environment variables currently loaded
into memory. Scroll through this list using the cursor keys on the
keyboard. Is this list larger or smaller than the list generated in
Step 2? Why? When finished, press q to quit the less utility.

At the command prompt, type PS1="Hello There:" and press


4
Enter. What happened and why? Next, type echo $PS1 at the
command prompt and press Enter to verify the new value of the
PS1 variable.

At the command prompt, type exit and press Enter to log out of
5
the shell. Next, log in to the terminal again using the user name of
root and the password of LINUXrocks!. What prompt did you
receive and why? How could you ensure that the “Hello There: ”
prompt occurs at every login?

404
At the command prompt, type vi .bash_profile and press Enter.
6
At the bottom of the file, add the following lines. When finished,
save and quit the vi editor.

Explain what the preceding lines will perform after each login.
At the command prompt, type exit and press Enter to log out of
7
the shell. Next, log in to the terminal using the user name of root
and the password of LINUXrocks!. When prompted for a hello
prompt, type y and press Enter. What prompt did you receive and
why?

At the command prompt, type exit and press Enter to log out of
8
the shell. Next, log in to the terminal using the user name of root
and the password of LINUXrocks!. When prompted for a hello
prompt, type n and press Enter to receive the default prompt.

At the command prompt, type MYVAR="My sample variable" and


9
press Enter to create a variable called MYVAR. Verify its creation
by typing echo $MYVAR at the command prompt, and press
Enter.

At the command prompt, type set | grep MYVAR and press Enter.
10
Is the MYVAR variable listed? Why?

At the command prompt, type env | grep MYVAR and press


11
Enter. Is the MYVAR variable listed? Why?

At the command prompt, type export MYVAR and press Enter.


12
Next, type env | grep MYVAR at the command prompt and press

Enter. Is the MYVAR variable listed now? Why?

At the command prompt, type exit and press Enter to log out of
13
the shell. Next, log in to the terminal using the user name of root
and the password of LINUXrocks!.

At the command prompt, type echo $MYVAR and press Enter to


14
view the contents of the MYVAR variable. What is listed and why?
At the command prompt, type vi .bash_profile and press Enter.
15
At the bottom of the file, add the following line. When finished,

save and quit the vi editor.

At the command prompt, type exit and press Enter to log out of
16
the shell. Next, log in to the terminal using the user name of root
and the password of LINUXrocks!.

At the command prompt, type echo $MYVAR and press Enter to


17
list the contents of the MYVAR variable. What is listed and why?

At the command prompt, type alias and press Enter. Note the
18
aliases that are present in your shell.
405
At the command prompt, type alias asample="cd /etc ; cat hosts ;
19
cd ~ ; ls –F" and press Enter. What does this command do?

At the command prompt, type asample and press Enter. What


20
happened and why? What environment file could you add this
alias to such that it is executed each time a new BASH shell is
created?

Type exit and press Enter to log out of your shell.


21

Project 7-4
In this hands-on project, you create a basic shell script and execute it on the
system.
Switch to a command-line terminal (tty5) by pressing Ctrl+Alt+F5
1
and log in to the terminal using the user name of root and the
password of LINUXrocks!.

At the command prompt, type vi myscript and press Enter to


2
open a new file for editing called myscript in your home directory.

Enter the following text into the myscript file. When finished, save
3
and quit the vi editor.

At the command prompt, type ls -l myscript and press Enter.


4
What permissions does the myscript file have? Next, type bash
myscript at the command prompt and press Enter. Did the shell
script execute? What do the \t and \a escape sequences do?

Next, type ./myscript at the command prompt and press Enter.


5
What error message did you receive and why?

At the command prompt, type chmod u+x myscript and press


6
Enter. Next, type ./myscript at the command prompt and press
Enter. Did the script execute? Why?

Type exit and press Enter to log out of your shell.


7

Project 7-5
In this hands-on project, you create a shell script that uses decision and loop
constructs to analyze user input.
Switch to a command-line terminal (tty5) by pressing Ctrl+Alt+F5
1
and log in to the terminal using the user name of root and the
password of LINUXrocks!.

At the command prompt, type vi [Link] and press Enter to


2
open a new file for editing called [Link] in your home
directory. Why is it good form to use a .sh extension for shell
scripts?

Enter the following text into the [Link] file. As you are
3
typing the contents, ensure that you understand the purpose of
each line (reviewing the chapter contents and appropriate man
pages as necessary). When finished, save and quit the vi editor.

406
At the command prompt, type chmod u+x [Link] and
4
press Enter. Next, type ./[Link] at the command prompt
and press Enter. Note the filename that your report was saved to.

At the command prompt, type less filename where filename is the


5
filename you noted in the previous step, and press Enter. View the
contents. Would this shell script work well to record storage
configuration from different systems? Why?
At the command prompt, type vi [Link] and press Enter to
6
open a new file for editing called [Link] in your home
directory.

Enter the following text into the [Link] file. As you are
7
typing the contents, ensure that you understand the purpose of
each line (reviewing the chapter contents and appropriate man
pages as necessary; the tar backup command will be discussed in
Chapter 11 and does not require detailed interpretation here).

When finished, save and quit the vi editor.

407
At the command prompt, type chmod u+x [Link] and
8
press Enter. Next, type ./[Link] at the command prompt
and press Enter. Note the error that you receive because you did
not specify a positional parameter. Type ./[Link]
/etc/samba at the command prompt and press Enter to create a
backup of the /etc/samba directory within the [Link] file
in your home directory.

At the command prompt, type ls -F and press Enter. Was the


9
[Link] file successfully created?
At the command prompt, type vi [Link] and press Enter.
10
As you are modifying the contents, ensure that you understand the
purpose of each change (reviewing the chapter contents and
appropriate man pages as necessary). Edit the text inside the
[Link] shell script such that it reads:

At the command prompt, type ./[Link] and press Enter.


11
Type /etc/httpd and press Enter when prompted to back up the
/etc/httpd directory to [Link] in your home directory.
Note that the backup file does not represent the directory or time
the backup was performed.
At the command prompt, type vi [Link] and press Enter.
12
As you are modifying the contents, ensure that you understand the
purpose of each change (reviewing the chapter contents and
appropriate man pages as necessary). Edit the text inside the
[Link] shell script such that it reads:

408
At the command prompt, type ./[Link] and press Enter.
13
Type /etc/httpd and press Enter when prompted to back up the
/etc/httpd directory. Note that the filename of the backup now
reflects the directory that was backed up as well as the date the
backup was performed.

At the command prompt, type vi [Link] and press


14
Enter to open a new file for editing called [Link] in
your home directory.
Enter the following text into the [Link] file. As you are
15
typing the contents, ensure that you understand the purpose of
each line (reviewing the chapter contents and appropriate man
pages as necessary). When finished, save and quit the vi editor.

409
At the command prompt, type chmod u+x [Link] and
16
press Enter. Next, type ./[Link] at the command
prompt and press Enter. Type a and press Enter when prompted
and supply the appropriate values for a family member of your
choice. Does the menu continue to appear when you have finished
entering your record? Type a and press Enter and supply the

appropriate values for a different family member. Type s and


press Enter and supply a piece of information you previously
entered for a family member to see the results. Type x and press
Enter to view the usage error. Finally, type q and press Enter to
quit your shell script.
Type exit and press Enter to log out of your shell.
17

Project 7-6
In this hands-on project, you create a local Git repository for the shell scripts
that you created in Project 7-5, and explore Git version control.

Switch to a command-line terminal (tty5) by pressing Ctrl+Alt+F5


1
and log in to the terminal using the user name of root and the
password of LINUXrocks!.

At the command prompt, type mkdir /shellscripts and press


2
Enter. Next, type mv [Link] [Link]
[Link] /shellscripts and press Enter to move your
shell scripts from Project 7-5 to the /shellscripts directory.

At the command prompt, type cd /shellscripts and press Enter.


3
Next, type git init and press Enter to create a Git repo in your
current directory. Finally, type ls -a and press Enter to verify the
creation of the .git subdirectory.

At the command prompt, type git status and press Enter. Note
4
that your shell scripts are detected but not managed by Git yet.
Next, type git add * and press Enter to add your shell scripts to the
index. Finally, type git status and press Enter to verify that your
shell scripts are ready for commit.
At the command prompt, type git commit -m "First commit" and
5
press Enter. What error did you receive and why? Next, type git

config --global [Link] "root@[Link]" and press Enter


to set your email address. Next, type git config --global [Link]
"root user" and press Enter to set your user name. Finally, type
git commit -m "First commit" and press Enter to create your first
commit.

At the command prompt, type vi [Link] and press Enter.


6
Add the following lines to the bottom of the [Link] file.
When finished, save and quit the vi editor.

410
At the command prompt, type git status and press Enter. Did Git
7
detect the modification to [Link]? Next, type git add * and
press Enter to add your shell scripts to the index. Finally, type git
commit -m "Added RAID to [Link]" and press Enter to
create a second commit that represents your change.

At the command prompt, type git log and press Enter. Note the
8
commit identifier next to your original commit (before the RAID
section was added). Next, type git reset --hard commitID and
press Enter, where commitID is the commit identifier for your
original commit. Finally, type cat [Link] and press Enter.
Was your RAID addition to the [Link] shell script removed?

At the command prompt, type cd and press Enter to return to


9
your home directory. Next, type git clone /shellscripts and press
Enter to clone your local /shellscripts repo to your home directory.
Next, type cd shellscripts and press Enter to switch to your
cloned repo. Finally, type ls -a and press Enter to verify that your
cloned repo contains the same contents as the original repo.
At the command prompt, type git branch and press Enter. What is
10
the default branch called? Next, type git checkout -b AddRAID
and press Enter to create a branch called AddRAID to your cloned
repo. Finally, type git branch and press Enter to verify that your
AddRAID branch was added. How can you tell that your AddRAID
branch is the active branch in the output?

At the command prompt, type vi [Link] and press Enter.


11
Add the following lines to the bottom of the [Link] file.
When finished, save and quit the vi editor.

At the command prompt, type git add * and press Enter to add
12
your shell scripts to the index within your AddRAID branch.
Finally, type git commit -m "Added RAID to [Link]" and
press Enter to create a commit within your AddRAID branch that
represents your change.

At the command prompt, type cat [Link] and press Enter


13
and note that your RAID modification is shown. Next, type git
checkout master and press Enter to switch back to the master
branch of your cloned repo. Finally, type cat [Link] and
press Enter. Is your RAID modification visible in the master
branch?

At the command prompt, type git push origin AddRAID and press
14
Enter to push your AddRAID branch to the original repo. Next,
type cd /shellscripts and press Enter to switch your current
directory to the original repo. Finally type git branch and press
Enter to verify that the branch was pushed successfully to the
original repo from the cloned repo. Also note that your current
branch in the original repo is still set to master.
At the command prompt, type git merge AddRAID and press
15
Enter to merge the changes from the AddRAID branch to your
master branch. Next, type cat [Link] and press Enter. Is
your modification visible in the master branch?

411
At the command prompt, type cd ~/shellscripts and press Enter to
16
switch your cloned repo in your home directory. Next, type git
pull origin master and press Enter to pull a new copy of the
master branch from your original repo. Was the pull successful?

Type exit and press Enter to log out of your shell.


17
Book Title: eTextbook: COMPTIA Linux Guide to Linux Certification
Chapter 7. Working with the Bash Shell
Discovery Exercises

Discovery Exercises

1. Name the command that can be used to do each of the following:

a. Create an alias called mm that displays only those filesystems

that are mounted and contain an ext4 filesystem.

b. Create and export a variable called NEWHOME that is

equivalent to the value contained in the HOME variable.

c. Find all files that start with the word “host” starting from the

/etc directory and save the Standard Output to a file called file1
and the Standard Error to the same file.

d. Display only the lines from the output of the set command that

have the word “bash” in them. This output on the terminal


screen should be sorted alphabetically.

e. Display only the user name (first field) in the colon-delimited

/etc/passwd file and save the output to a file called users in the
current directory.

2. What would happen if the user executed the following commands?

Explain the output.


3. Recall that only Standard Output can be sent across a pipe to another

command. Using the information presented in this chapter, how

could you send Standard Error across the pipe in the following
command?

4. Name the test statement that can be used to test the following

scenarios:

a. The user has read permission to the /etc/hosts file.

b. The user has read and execute permission to the /etc directory.

c. The contents of the variable $TEST are equal to the string

“success.”

d. The contents of the variable $TEST are numerically equal to the


contents of the variable $RESULT.

e. The contents of the variable $TEST are equal to the string


“success” and the file /etc/hosts exists.

f. The contents of the variable $TEST are equal to the string


“success,” or the number 5, or the contents of the variable
$RESULT.

5. Examine the sample /root/.bash_profile file shown next. Using the


information presented in this chapter, describe what each line of this

file does.
6. Examine the following shell script and describe its function line-by-
412
line:

7. Examine the following shell script and describe its function line-by-

line:
8. [Link] is one of the most commonly used public Git

repositories. Navigate to [Link] using a Web browser and


create a free account by navigating the appropriate options provided.
Following this, create a public repository called shellscripts. Next, on
your Fedora Linux virtual machine, run the following commands as
root (where name is your GitHub account name) to push the contents
of the /shellscripts repo from Project 7-6 to GitHub and set GitHub as

the original repo. Supply your GitHub username and password when
prompted.

Finally, log into another terminal as user1 on your Fedora Linux


virtual machine and clone your public shellscripts repository from
GitHub using the following command (where name is your GitHub
account name). Verify the results when finished.

413

You might also like