In this assignment, you will build on your previous knowledge of screen and Markdown (.md) file creation, focusing on advanced shell scripting techniques to automate tasks in the terminal. You will practice creating, editing, and executing complex scripts, while capturing and organizing your workflow using screenshots and structured documentation.
Open the terminal and create the Assignment3
folder using the following command:
mkdir Assignment3
Navigate into the Assignment3
folder:
cd Assignment3
create_files.sh
ScriptUse vim
or your preferred text editor to create the create_files.sh
script:
vim create_files.sh
In the file, add the following content:
#!/bin/zsh
# Create directories
mkdir scripts documents figures
# Create files
touch documents/exercise.txt
# Write content to files
echo "This is the first line written with the '>' operator, which overwrites the file." > documents/exercise.txt
echo "This is the second line written with the '>>' operator, which appends to the file." >> documents/exercise.txt
# Create shell scripts inside the scripts folder
touch scripts/loops.sh scripts/operations.sh scripts/conditions.sh
Take a screenshot of your create_files.sh
content, save the screenshot as create_files.png
inside the figures
folder as in the image below.
Save and exit the file by typing :wq
.
create_files.sh
Script ExecutableIn the terminal, use chmod
to make the script executable:
chmod +x create_files.sh
Run the script to create the necessary folders and files:
./create_files.sh
Now, if you run ls
command in Assignment3 folder you will see documents
, figures
, scripts
and create_files.sh
files.
Next, use the screen
command to split your terminal into two windows:
screen
Split the screen horizontally by pressing ctrl + z
and then |
(pipe key).
operations.sh
ScriptIn the left window of the terminal:
scripts
folder:
bash
cd scripts
operations.sh
file in vim
:
bash
vim operations.sh
For the operations.sh
script, write the following content to this file:
#!/bin/zsh
echo "Example 1: Addition"
x=12
y=28
result=$(($x + $y))
echo "Sum: $result"
echo "**********"
echo "Example 2: Subtraction"
x=50
y=15
result=$(python3 -c "print($x - $y)")
echo "Difference: $result"
echo "**********"
echo "Example 3: Multiplication"
x=8
y=9
result=`expr $x \* $y`
echo "Product: $result"
echo "**********"
echo "Example 4: Division"
x=100
y=25
result=$(($x / $y))
echo "Quotient: $result"
echo "**********"
echo "Example 5: Modulus"
x=45
y=7
result=$(python3 -c "print($x % $y)")
echo "Modulus: $result"
Save the file by typing :w
.
In the right window of the terminal:
- Make the operations.sh
script executable and run it:
chmod +x operations.sh
./operations.sh
Take a screenshot of your terminal, with the code on the left window and the output on the right window. Save the screenshot as operations.png
inside the figures
folder.
An example screenshot of this process is as follows:
loops.sh
and conditions.sh
Follow the same process for loops.sh
and conditions.sh
:
- In the left window, open and edit loops.sh
and conditions.sh
files in vim
.
- Add the necessary script content for each:
For the loops.sh
script, write the following content to this file:
#!/bin/zsh
echo "Example 1: Simple Loop (Numbers from 10 to 13)"
for i in 10 11 12 13
do
echo ${i}
done
echo "**********"
echo "Example 2: Range Loop (From 15 to 18)"
for i in {15..18}
do
echo ${i}
done
echo "**********"
echo "Example 3: Loop with Seq (Numbers between 30 and 34)"
for i in $(seq 30 34)
do
echo ${i}
done
echo "**********"
echo "Example 4: Range Loop with Step (From 30 to 40, step by 3)"
for i in {30..40..3}
do
echo ${i}
done
echo "**********"
echo "Example 5: Incremental For Loop (From 5 to 20, step by 3)"
for ((i=5; i<=20; i+=3))
do
echo ${i}
done
echo "**********"
For the conditions.sh
script, write the following content to this file:
#!/bin/zsh
echo "Example 1: String Comparison"
x="hello"
y="world"
if [ $x = $y ]
then
echo "x equals y"
else
echo "x does not equal y"
fi
echo "********************************************"
echo "Example 2: Numeric Equality"
x=25
y=25
if [ $x -eq $y ]
then
echo "x is equal to y"
else
echo "x is not equal to y"
fi
echo "********************************************"
echo "Example 3: Greater Than or Equal to"
x=30
y=15
if [ $x -ge $y ]
then
echo "x is greater than or equal to y"
else
echo "x is less than y"
fi
echo "********************************************"
echo "Example 5: Modulus Comparison"
z=7
t=3
if [ $(($z % $t)) -eq 1 ]
then
echo "z mod t equals 1"
else
echo "z mod t does not equal 1"
fi
echo "********************************************"
For each script and output (loops.sh
and conditions.sh
), take a screenshot and save it as loops.png
and conditions.png
inside the figures
folder.
In the image below you can find an example of the screenshots you should take for loops.sh
and conditions.sh
:
create_files.sh
to the Scripts FolderNow, move the create_files.sh
script into the scripts
folder.
Write the following command in the Assignment3
folder:
mv create_files.sh scripts/
README.md
FileFinally, create a README.md
file in the Assignment3
folder:
vim README.md
In the README.md file, briefly describe what each file and folder is as follows:
# Assignment 3: Advanced Shell Scripting and Workflow Automation
## Objective
This assignment focuses on improving your shell scripting skills and workflow automation using the terminal. The project is divided into four components, each containing scripts and documentation for the tasks.
There are four components (3 `folders` and 1 `.md` file) here:
1. **`scripts`**
This folder contains 4 shell scripts:
- `create_files.sh`: A script to create directories and files and write content into those files.
- `loops.sh`: A script demonstrating loop operations in shell scripting.
- `conditions.sh`: A script demonstrating conditional statements in shell scripting.
- `operations.sh`: A script showcasing basic arithmetic operations.
2. **`documents`**
This folder contains:
- `exercise.txt`: A text file where content is written using shell commands as part of the assignment.
3. **`figures`**
This folder contains 4 screenshots documenting your work with the shell scripts:
- `create_files.png`: A screenshot of the terminal running the `create_files.sh` script.
- `loops.png`: A screenshot of the terminal running the `loops.sh` script.
- `conditions.png`: A screenshot of the terminal running the `conditions.sh` script.
- `operations.png`: A screenshot of the terminal running the `operations.sh` script.
4. **`README.md`**
This `README.md` file contains the instructions and overview of the assignment and its components.
Save and exit the README.md
file. (:wq
)
Before running the submitAssignment.sh
file, make sure you are in the "Assignment 3" folder.
Running this script in any other folder or subfolder may result in missing files being uploaded.
When executed in the correct folder, all files in the "Assignment 3" directory will be included in the submission.
submitAssignment.sh