Format of Assignments
Submit a single Microsoft word file containing the text of your code. Take a snapshot of the output of your script and paste it in the word file. Submit the word document. The assignments must be done on ccsf linux servers. After running the script/program on the linux server run the command "whoami" on the server and the screen shot should show both the output of the script and the output of the "whoami" command.
Assignment 1
Write a script that will print the username and the host name and ip address.The folders should be on separate lines. An example output would be:
[amittal@hills hw1]$ ./getinfo.sh
Thx for spending time with me: amittal
You are logged into: hills.ccsf.edu
Ip Address: 192.32.34.100
[amittal@hills hw1]$
Assignment 2
var1="234 543 678 these are numbers."
Assume we have a variable that contains the above sentence.
Extract the first number "234" and place it in a variable "var2" and extract the
second number and place it in a variable "var3" . Print these variables out on the
console and their sum. The output should be:
234
543
777
Use "sed" and the "()" pattern matching to get the numbers out and then you can use "expr" to get their sum.
Place the contents of the shell file in a word document along with a snapshot of the output and the command "whoami" .
Assignment 3
Assume we have a file "marks.txt" whose contents are:
1) John 80
2) Peter 90
3) David 47
4) James 25
5) Lisa 89
6) Kenny 56
7) Sam 95
8) Julia 74
9) Cassie 66
10) Marelena 45
Write a script file using "awk" that works on the above file and produces the below:
Id Name Marks Grade
1) John 80 C
2) Peter 90 B
3) David 47 F
4) James 25 F
5) Lisa 89 B
6) Kenny 56 E
7) Sam 95 A
8) Julia 74 C
9) Cassie 66 D
10) Marelena 45 F
Number Of A:1
Number Of B:2
Number Of C:2
Number Of D:1
Number Of E:1
Number Of F:3
There will be a header at the top and another column Grade. The grade is given based on the following range:
A >90
B 80-90
C 70-80
D 60-70
E 50-60
F <50
The number of A's and B's are counted and printed in a summary at the bottom.
Assignment 4
(due Dec 17th )
You have to fill in the code for 2 functions and run the shell file from command line.
The following is a shell script file:
File hw4..sh
#A factorial is the product of the number and all numbers below it.
#Factorial of 3 is 1 * 2 * 3 = 6
#Factorial of 4 is 1 * 2 * 3 * 4 = 24
#The function sets the value of the variable "return" to the resultant value
#Remember factorial of 0 is 1 and factorial of 1 is also 1
factorial()
{
//To do
}
# This function will set the variable return to 0 if the number ( first argument )
# is prime otherwise it will set the value of "return" to 1
#
isPrime()
{
//To do
}
for item in 3 4 5 6 7
do
#echo $item
isPrime $item
if [ $return -eq 0 ];
then
echo $item is not a prime number.
else
echo $item is a prime number.
fi
factorial $item
echo "Factorial of $item is $return"
done
After filling in the code for the functions you can run your shell file as:
[amittal@hills hw4]$ ./hw4.sh
3 is a prime number.
Factorial of 3 is 6
4 is not a prime number.
Factorial of 4 is 24
5 is a prime number.
Factorial of 5 is 120
6 is not a prime number.
Factorial of 6 is 720
7 is a prime number.
Factorial of 7 is 5040
Your output should match the output that is listed above.