Shell Scripting Extra practice 1
Goal
This extra credit is just to practice basic shell scripting. The main goal is to get comfortable doing the following:
- Creating and executing a basic script
- Using the
man
command to get help
We will use the following commands only:
echo
: to display textdate
: to manipulate datesdf
: disk space usagefree
: memory usageuname
: basic system informationclear
: clear screen
How to write and execute a basic script step by step
1. The tools:
- To write the script you will need a text editor. These text editors are easy to use:
- Gnome Text Editor: Ubuntu’s default text editor
- Geany: More advanced and feature rich but still easy enough for beginners
- There are a tone more. A simple google search will tell you so! However the text editor you choose is irrelevant so just pick one and move on.
- In this guide I will be using Gnome Text and Geany.
- A terminal emulator to run the script. Any terminal emulator will work but here I will be using Tilix.
2. Write the script
- Open your text editor
- The first line of code is the shell declaration or shebang:
#!/bin/bash
This line will tell the shell (bash) who will be running the script. This line is important because without it, all the commands in the script will be run by the current shell. - Save your file with the file extension
.sh
. The file extension is not mandatory but it is good practice. - In this guide, we will be placing all our scripts in a folder/directory called
scripts
in your home directory. This will make it easier to find our scripts.
3. Execute the script
- Open your terminal emulator.
- The most basic way of running a script is using this formula:
shell
+path/to/script/
- For example, we know that our shell is
bash
and we placed our script in~/scripts/
therefore the command to execute our script will be:bash ~/scripts/hello.sh
- This part of the path:
~
is a shorthand for/home/$USER
which is the current’s user home directory. If you want to learn more about this read read the presentation The Linux Filesystem or the article The Linux FS. For now, every script will be placed in~/scripts/
so you should not worry about it.
4. Add more code to the script
- Notice that when you ran the script, nothing happened. That is because there is nothing to do in the script.
- Let’s write some basic commands in the script:
- Use
echo
to print/display a line of text:echo "hello world"
- Use
- Save and run the script. Notice that the line “hello world” is displayed in the terminal.
Let’s Practice!
You should now be comfortable writing and executing a basic script. However, before we practice, let’s make our environment more comfortable.
Customize Gnome Text Editor:
- Open the Text Editor Preferences:
- Choose a theme you like
- Enable custom font and increase the size to a size you are more comfortable with
- Enable “Highlight Current Line”
- Enable “Display Overview Map”
- Disable “Restore Session”
- In the Options menu:
- Enable line numbers
- Change the “Spaces per tab” to 4
- Disable “Check Spelling”
Now, let’s place the terminal emulator and text editor side by side. Use the Windows/Super Key
+ Right Arrow
to snap one window to the right then select the terminal emulator to snap it to the left. This will allow us to work more efficiently
Practice 1: Learning more about echo
The echo
command is used to display/print text to the screen. The formula for the command is:
echo
+option
+"String you want to display"
Where option can be one of the followings:
-n
: Do not output a trailing new line.-e
: Enable interpretation of backslash escapes. See man echo for a list of backslash escapes-E
: disable interpretation of backslash escapes (this is the default behavior)
In a terminal emulator practice the echo command. You may need to open the man page of echo for reference. To practice echo, complete the following tasks:
- Display a line of text.
- Display a line of text suppressing the trailing new line
- Display a line of text with a tab at the beginning of the line
- Display multiple lines of text in a single echo command
- Display multiple lines of text in a single command including horizontal and vertical tabs
Practice 2: Working with more than just echo
NOTE: At the top of this guide, you have a short description of some basic commands. When needed, use the man command to read the man page of any of those commands |
- Create a shell script named
systemInfo.sh
and save it in thescripts
directory/folder. - This script should output the following information about your system:
- Current date in UTC
- Disk space usage in human readable format
- Memory/RAM usage in human readable format
- Network node hostname (machines name)
- Type of processor and type of operating system
- Make sure that each part is described before it is printed to the screen. This is an example output of your program:
If you are doing this activity for the extra credit. Take a screenshot of the output of your script (terminal)
Practice 3: More on Echo!
In this practice, use what you have learned about echo to better the script’s output.
- Make a copy of
systemInfo.sh
calledsystemInfo2.sh
- Your goal is to use echo to make the information more presentable. You may need to move some parts around.
- Here are some tips:
- Echo can insert new empty lines
- Echo can insert tabs within text
- Echo can suppress the new line putting the output of echo in the same line as the next command
- You should review the man page of echo and practice some of those commands.
- This is how the output of your script should look like:
If you are doing this activity for the extra credit. Take a screenshot of the output of your script (terminal)
What will you submit for extra credit
- In your cis106 directory/local repository, create a new directory called “ExtraCredit” (One word no spaces in file names pleases!)
- Inside “ExtraCredit”, create another directory called “extra2”
- Open your cis106 directory/local repository in VS Code
- Inside “extra2” create a markdown file called “extra2.md”
- Add all the screenshots to “extra2.md” using proper markdown syntax and labeling each screenshot using heading 2 formatting. Save the markdown file.
- Your markdown document should start with the following heading1:
# Extra Credit 2: Shell Scripting Extra practice 1
- Convert your file to PDF
- Run the github commands, in VS Code Terminal, to push all your changes to github:
gid add .
git commit -m "Completed Extra 1"
git push
- In blackboard submit the following:
- The URL of
extra2.md
- The PDF file
extra2.pdf
- The URL of