Lecture 7: Example Problems

Shell Scripts

Q1

Make a directory as Q1, change your working directory in it. Write a shell script named shell1.sh. Create 3 folders and a file in it. Src, Docs, Data and README.md.

A1

#!/bin/sh
#   Author        : Ogrenci Adi
#   Date          : 10-24-07---10-11-2024
#   Last Modified : 10-24-32---10-11-2024

mkdir Src Docs Data
touch README.md

Q2

Make a directory as Q2, change your working directory in it. Write a shell script named shell2.sh. Create 4 folders and 2 files in this shell script. Data, Output, Scripts, Backup, README.md and evaluateMyExam.sh.

Q3

Make a directory as Q3, change your working directory in it. Write a shell script named shell3.sh creating the folders and files as shown below.

Q3
├── data
│   └── README.md
├── docs
│   └── README.md
├── README.md
└── src
    └── README.md

Q4

Make a directory as Q4, change your working directory in it. Write a shell script named shell4.sh creating the folders and files as shown below.

Q4
├── codes 
│   └── calculate.py
├── files
│   └── lists.txt
├── README.md
└── results 
    └── results.txt

Q5

Make a directory as Q5, change your working directory in it. Write a shell script named shell5.sh creating 3 folders named as 1, 2 and 3 and creating 100 files in each folder (totally 300 files) named as 001.txt, 002.txt, ..., 100.txt and writing the file name in it as This is file 1 in folder 1., This is file 2 in folder 3., ..., This is file 100 in folder 2. for example.

Q6

Make a directory as Q6, change your working directory in it. Write a shell script named shell6.sh creating 100 files named as 001.txt, 002.txt, ..., 100.txt and writing whether the file number is odd or even in the file. For example, This is an odd number. for file001.txt and This is an even number. for file100.txt.

Q7

Make a directory as Q7, change your working directory in it. Write a shell script named shell7.sh printing Hello Nicola Tesla. when it is run as ./shell7.sh Nicola Tesla. Use your own name to check if it works for any names.

Q8

Make a directory as Q8, change your working directory in it. Write a shell script named shell8.sh creating file.txt with a content of This file is made by shell8.sh file.. Then change its content using sed command to This file is changed by shell8.sh file..

Q9

Make a directory as Q9, change your working directory in it. Write a shell script named shell9.sh. In this script, check if the Data directory exists. If it does, print Data directory already exists.. If it doesn’t, create the Data directory and print Data directory created..

Q10

Make a directory as Q20, change your working directory in it. Write a shell script named shell20.sh that calculates and prints the sum of all even numbers between given 2 numbers. The script must work with ./shell10.sh 1 100 and prints The sum of even numbers from 1 to 100 is 2550..

Python scripts

Q11

Make a directory as Q11, change your working directory in it. Write a Python script named as ageCalculator.py and calculating users age from his/her birth year. Read the year information from the commmandline. Use input() function for it.

Q12

Make a directory as Q12, change your working directory in it. Write a Python script named as calculator.py and working as a calculator. Use argparse module to read numbers and mathematical operation from the commandline as

Q13

Make a directory as Q13, change your working directory in it. Write a Python script named as temperatureConverter.py converting temperature from Celcius to Fahrenheit (e.g. $(10^oC \times 9/5)+32=50^oF$). Use input() function to read the temperature value in Celcius.

Q14

Make a directory as Q14, change your working directory in it. Write a Python script named as calculateAverage.py taking 3 numbers from user with argparse module and prints the average of these three numbers. Usage must be python3 calculateAverage.py -f 5 -s 10 -t 15 and the result must be Average of 5, 10 and 15 is 10..

Q15

Make a directory as Q15, change your working directory in it. Write a Python script named as factorialCalculator.py taking a number from user with argparse module and prints the factorial of the number.

You can download an example midterm exam by clicking here