Question 2
A) The AIT canteen wants you to write a script for them which would do the following
I) receive records from the user and keep these records into a file, the records must include
ID
Name of meal
Price of meal
Quantity bought today
Sales made for the day
3
Today the sales person wants to make seven entries into the file via the command line interface.
Your script should be able to find to present the sales person with the ability to
Search for a particular meal by ID
Search for a particular meal by name
find out how much money has been made from the sales of a particular meal
list all records
list all meals and their prices
list a particular meal and its price
[10marks]
B) With a shell script explain how to use case
[9marks]
C) With code explain how the following work
if statements
if else statements
if elif else fi statements
[4 marks]
D) Explain the difference between= grep -i "some string" some file and
grep -iw "some string" some file
A)
results = {
weak_sent: [
"row 1 data",
"row 2 data"
],
weak_sent_num: [1,2]
}
function generate_table() {
// get the reference for the body
var body = document.getElementsByTagName("body")[0];
// creates a <table> element and a <tbody> element
var tbl = document.createElement("table");
//var header = document.createElement("header");
// var header = '<tr><th>Country</th><th>City</th></tr>';
var header= document.createElement('thead')
var headingRow = document.createElement('tr')
var headingCell1 = document.createElement('td')
var headingText1 = document.createTextNode('country')
headingCell1.appendChild(headingText1)
headingRow.appendChild(headingCell1)
var headingCell2 = document.createElement('td')
var headingText2 = document.createTextNode('City')
headingCell2.appendChild(headingText2)
headingRow.appendChild(headingCell2)
header.appendChild(headingRow)
tbl.appendChild(header)
//var header = "<th>Header</th>";
var tblBody = document.createElement("tbody");
// creating all cells
for (var i = 0; i < results.weak_sent.length; i++) {
// creates a table row
var row = document.createElement("tr");
for (var j = 0; j < 2; j++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
var cell = document.createElement("td");
if (j == 0) {
var cellText = document.createTextNode(results.weak_sent_num[i]);
} else {
var cellText = document.createTextNode(results.weak_sent[i]);
}
cell.appendChild(cellText);
row.appendChild(cell);
}
// add the row to the end of the table body
tblBody.appendChild(row);
}
// This is for the quick solution
// tbl.innerHTML = header
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
body.appendChild(tbl);
// sets the border attribute of tbl to 2;
tbl.setAttribute("border", "2");
}
generate_table()
B) CASE
A case construct is used to simplify nested if statement. Here, it reads a variable value, and matches the value with all case and finds out the matching pattern. Syntax starts with keyword 'case' and ends with 'esac'
ANIMAL="cat"
case "$ANIMAL" in
"cow") echo "cow gives milk"
;;
"dog") echo "dog is lovely"
;;
"cat") echo " cat is cute "
;;
esac
C) SHELL SCRIPT TO FIND GREATEST OF TWO NUMBERS USING
IF STATEMENT
ELSE IF STATEMENT &
IF ELIF ELSE FI STATEMENT
p=5
q=12
if [ $p == $q ]
then
echo "p is equal to q"
elif [ $p -gt $q ]
then
echo "p is greater than q"
elif [ $p -lt $q ]
then
echo "p is less than q"
else
echo "None of the condition met"
fi
D)
grep -i "some string" some file
MEANING:
grep :- print lines matching a pattern
grep -i :- print lines matching a pattern which is case insensitive
RESULT: Matches all lines of the file named 'some-file' which contains the string 'some-string'.
MEANING:
grep :- print lines matching a pattern
grep -iw :- WORD grep that print lines matching a pattern with given WORD
RESULT: Matches all lines of the file named 'some-file' which contains the string 'some-string' as a single WORD
Comments
Leave a comment