0% found this document useful (0 votes)
8 views31 pages

Linux Shell Scripting Lab Guide

XK0-005-main

Uploaded by

akk
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)
8 views31 pages

Linux Shell Scripting Lab Guide

XK0-005-main

Uploaded by

akk
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

Linux, day 4

This lab is licensed under Creative Commons BY-NC-SA 4.0.


[Link]

You are free to share and adapt, but NOT for commercial
purposes and you must attribute the source and share your own
adaptions under the same license.

1
LAB: The shell environment

57
Assignment
• Adjust your account's "~/.bashrc".
– Add "/opt/test" to $PATH.
• Make the "/opt/test" directory.
– Copy the "ls" binary to "/opt/test/testls".
• Do a new SSH login (or start a new Bash).
– Can you now run "testls"?

58
Solution
$ echo 'PATH=$PATH:/opt/test' >> ~/.bashrc

$ sudo mkdir -p /opt/test


$ sudo cp -p /usr/bin/ls /opt/test/testls

$ ssh localhost "which testls"

59
What will we do today?
• Recap
• Git lab, from day 3.
• The shell environment
• Shell scripting
• Closing: homework and Q&A

60
LAB: Shell scripting: a start

83
LAB: Create a simple script
• Create a script which:
– Runs ;)
– Reads a name from the first, passed parameter.
– Asks for a greeting interactively (with read).
– Outputs a greeting to the name.

84
Example
$ ./[Link] Tess
How would you like to be greeted?
Hello
Hello Tess

85
Solution
#!/bin/bash
NAME=$1

read -p "What greeting would you like? " GREET

echo "${GREET} ${NAME}"

86
Solution
$ chmod +x [Link]

$ ./[Link] Tess
What greeting would you like? Hello
Hello Tess

87
What will we do today?
• Recap
• Git lab, from day 3.
• The shell environment
• Shell scripting - MORE
• Closing: homework and Q&A

88
LAB: Shell scripting: I/O & Files

112
Assignment 1
• Find all files with "passwd" in their name.
• Find all files, literally and exactly called "passwd".
• Find all world-writable files.

113
Assignment 2
• SSH from one VM to the other,
• Use a HEREDOC to run:
– touch /tmp/foobar
– ls /tmp
– echo $(whoami) > /tmp/foobar
– cat /tmp/foobar

114
Solution
$ locate passwd
$ find / -type f -name "*passwd*"

$ locate -b -r /passwd$
$ find / -type f -name "passwd"

115
Solution
$ find / -perm -o+w -type f

116
Solution
$ ssh localhost << EOF
touch /tmp/foobar
ls /tmp
echo \$(whoami) > /tmp/foobar
cat /tmp/foobar
EOF

117
What will we do today?
• Recap
• Git lab, from day 3.
• The shell environment
• Shell scripting -> More!
• Closing: homework and Q&A

118
LAB: Shell scripting: Flow

150
Assignment
• Write a shell script which:
– Checks if it’s run as root; if not, "exit 1".
– Reads a command from passed parameters.
• This command is either "create" or "remove".
– Asks for a number (for now, keep it <20).
– Either creates or removes "user1", "user2", "user3" etc.
– Uses HEREDOC to make "[Link]" in their homedir.
151
Assignment
• If you run it, this looks like:

$ sudo /tmp/[Link] create


How many?
5

$ cat /home/user5/[Link] #works

152
Solution
• I have a sample solution on Github.
• It's here, in the Lesson 004 directory.

153
What will we do today?
• Recap
• Git lab, from day 3.
• The shell environment
• Shell scripting
• Closing: homework and Q&A

154
Closing

155
Homework
• Reading:
– Chapter 4: Processing and analyzing text
– Chapter 25: Shell scripting

157
Homework
• Create a shell script to "ping sweep" a network.
– It should ask for the first three octets of an IP.
– Then it should cycle through all 254 addresses.
– e.g. ./pingsweep [Link] 24

158
Other resources
• [Link] Bash manual

• Exercism: gamified learning programming.


– 80+ assignments for Bash.

• [Link] has some fun assignments.

159
Reference materials

160
Resources
• Creating shell scripts in Enterprise Linux (PluralSight)
• A roundup of 15 popular Linux shells.
• Linux shell metacharacters
• Bash globbing tutorial
• Bash read command
• Bash getopts example

161
Resources
• Really in-depth on pipes, forks and more
• Testing file characteristics in Bash
• Why you don’t read lines with "for"
• An excellent, in-depth study guide for shell scripting
• Practice more, take the challenge!
• Or try Tutorials Ground

162
Resources
• Exercism: gamified learning programming.
• [Link] has some fun assignments.
• Arithmetic in Bash
• [Link] Bash manual
• Shell Scripting tutorial for beginners (YouTube)

163

You might also like