Question

UNIX QUESTION Exercise #1. [8 Marks] Create a script that does the following: a. Takes 4...

UNIX QUESTION

Exercise #1. [8 Marks] Create a script that does the following:

a. Takes 4 input parameters b. Using the shell special variables print the following:

i. The command you executed

ii. First input

iii. Second input

iv. Third input

v. Fourth Input

vi. Process Id

vii. Total number of input parameters [ by querying the system for it ]

Exercise #2. [8 Marks] Creates a script that does the following:

a. Takes two input parameters

b. Using the numeric comparison syntax, it tests and prints whether the first input in relation to the second input is

: i. Greater than

ii. Greater than or equal

iii. Less than

iv. Less than or equal

v. Equal

vi. Not equal

c. Using the string comparison syntax, it tests and prints whether the first input in relation to the second input is

i. Greater than

ii. Greater than or equal

iii. Less than

iv. Less than or equa

l v. Equal vi. Not equal

b. Use the following pairs to test your code:

i. 123 14

ii. 55 55

iii. Bad Band

iv. bad Band

v. chaos banana

Exercise #3 [4 Marks] Creates a script that explores the content of a directory, and does the following:

a. Takes as an input the name of a folder at your current working directory

b. Prints the time and date to a file called dirLog which is located at your current working directory

c. Loops through the content of the directory and appends the name of each file found to a file called dirLog

0 0
Add a comment Improve this question Transcribed image text
Answer #1

script1.sh

#! /bin/bash

# this can be used to check the number of arguments passed
# but it is not required as part vii. asks for total number of input parameters
if [[ $# -ne 4 ]]; then
exit 1
fi

# you can also use just 1 echo command to display each parameter required as
# echo $0 $1 $2 $3 $4 $$ $#
echo $0
echo $1
echo $2
echo $3
echo $4
echo $$
echo $#

script2.sh

#! /bin/bash

if [[ $# -ne 2 ]]; then
exit 1
fi

# Uncomment the following code for a somewhat smarter comparison rather than checking all
# if [[ $1 -eq $2 ]]; then
# echo "$1 = $2"
## you can also add statements for greater than or equal to
## but that seems redundant
# else
# echo "$1 != $2"
# if [[ $1 -gt $2 ]]; then
# echo "$1 > $2"
# else
# echo "$1 < $2"
# fi
# fi

# Using numeric comparison
# use the following regular expression to check if the input parameters are numbers
re='^[0-9]+$'
if [[ $1 =~ $re && $2 =~ $re ]]; then
if [[ $1 -gt $2 ]]; then
echo "$1 > $2"
fi

if [[ $1 -ge $2 ]]; then
echo "$1 >= $2"
fi


if [[ $1 -lt $2 ]]; then
echo "$1 < $2"
fi

if [[ $1 -le $2 ]]; then
echo "$1 <= $2"
fi

if [[ $1 -eq $2 ]]; then
echo "$1 = $2"
fi


if [[ $1 -ne $2 ]]; then
echo "$1 != $2"
fi
fi

# Using string comparison
if [[ $1 > $2 ]]; then
echo "string \"$1\" > string \"$2\""
fi

if [[ $1 > $2 || $1 == $2 ]]; then
echo "string \"$1\" >= string \"$2\""
fi

if [[ $1 < $2 ]]; then
echo "string \"$1\" < string \"$2\""
fi

if [[ $1 < $2 || $1 == $2 ]]; then
echo "string \"$1\" <= string \"$2\""
fi

if [[ $1 == $2 ]]; then
echo "string \"$1\" = string \"$2\""
fi

if [[ $1 != $2 ]]; then
echo "string \"$1\" != string \"$2\""
fi

script3.sh

#! /bin/bash

if [[ $# -eq "1" && -d "$1" ]]; then
date > dirLog
# No need to loop, we can use the output of the ls command for this
# uncomment the following if you need to use a loop
# for file in "$1/*"; do
# echo $file
# done
ls $1 >> dirLog
else
exit 1
fi

OUTPUT:

Contents of dirLog after executing script3.sh. Note that date -u can be specified for utc time

Script Screenshots:

script1.sh

script2.sh

script3.sh

Add a comment
Know the answer?
Add Answer to:
UNIX QUESTION Exercise #1. [8 Marks] Create a script that does the following: a. Takes 4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them....

    Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them. The program should print the result of the comparison. Specifically, it should print “<x> is <comparison> <y>”, where <x> is the first number, <y> is the second number and <comparison> is one of “equal to”, “greater than” or “less than”. If the two numbers are equal, the program should have an exit status of zero. The exit...

  • Question 1 a. Define the following matrices in a script file (M-file), ? = ( 8...

    Question 1 a. Define the following matrices in a script file (M-file), ? = ( 8 9 10 11; 23 9 16 15 ;11 12 3 6; 1 2 8 9 ) ? = ( 2 21 7 15; 12 4 8 22; 23 9 5 13; 23 4 21 22) ℎ = (4 9 12 15) b. Add suitable lines of codes to the M-file to do the following. Each of the following points should be coded in only...

  • 8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does no...

    8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does not exist, is a directory, or there are more or less than one parameters provided, display an appropriate error/usage message and exit gracefully Display the number of a specified major who are taking a given class iii. The following is a sample...

  • 3. Write a program called VibratingSystem that does the following a. Takes the following as input...

    This two problems go together 3. Write a program called VibratingSystem that does the following a. Takes the following as input i. Spring constant, k; ii. Mass under vibration, m; iii. Coefficient of viscous damping, c, iv. Initial position of the mass. xi, v. And initial velocity of the mass. vi b. If the values k or m are less than or equal to zero, ask for those variables again. c. Plot the results for t from 0 to 20...

  • Question 1 (25 marks) A telecommunication engineer is given a task to assess the output power...

    Question 1 (25 marks) A telecommunication engineer is given a task to assess the output power levels of a transmitter with respect to different kind of modulation modes with the same intelligibility received. Given that the antenna transmits a 12.5kW of total power at 90% modulation of conventional amplitude modulation (AM) Compute the following: (i) (a) the amount of carrier power, Pe delivers through the antenna. [2 marks the amount of power using Double-Side Band Suppressed Carrier [2 marks] (ii)...

  • stion 4: 7 Marks) 1/3 A landscape contractor kept track of how long it takes to...

    stion 4: 7 Marks) 1/3 A landscape contractor kept track of how long it takes to cut the lawn in the centre part of the University during the summer. The time varies from week to week. The cutting times are as follows: x = cutting minutes = {56, 90, 88, 58, 72, 65, 58, 65} The contractor is interested in knowing the: (i) (ii) (iii (iv) (v) (vi) mean of cutting time mode of cutting time variance of cutting time...

  • Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a....

    Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a. Write the following as script6.sh and change its permissions to count for num in d do if num eq 100 then count (count+1) done echo The number 10 was found $count times What is the output? Run the script as ./script6.sh 10 20 30 10 40 20 50 10 60 <enter Summarize what the script does r using the read command. The following script...

  • Question 5 (8 marks) The cart in Figure 1 moves at constant velocity V, and takes...

    Question 5 (8 marks) The cart in Figure 1 moves at constant velocity V, and takes on water with a scoop of width B that dips h into a pond (the width B is normal to the paper). The fluid velocity in the pond is v=0. Neglect air drag and wheel friction. The aim is to estimate the force required to keep the cart moving. Assume density is constant. (i) Select a C.V. and clearly indicate w, v and n...

  • Which of the following is/are true for the average accounting return method of project analysis? 1....

    Which of the following is/are true for the average accounting return method of project analysis? 1. does not need a cutoff rate II. ignores time value of money III. is based on project's cash flows IV. easily obtainable information for computation Multiple Choice I and IV only II and IV only ooo Tonly I, II, III, and IV I, II, and IV only Which of the following statements related to annuities and perpetuities is/are correct? 1. Most loans are a...

  • wnloads/HW5 pdf Create a MATLAB code to calculate function f (z) = ei8r at any given...

    wnloads/HW5 pdf Create a MATLAB code to calculate function f (z) = ei8r at any given point using Taylor series. Write a script file called myTaylor.m and a function file called myFact.m to do the following: (a) The function myFact.m should return the factorial of a number when sent the number. In the script file: i. Set the starting point as r0-1 and fo = f(d)-6.049647 ii. Ask the user to enter his/her desired point and store it in x1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT