Docsity

ULSA-Lab wk10 Scripting_.docx, Summaries of Network Programming

Toggle sidebar
bg1
ULSA Lab Chapter 7. (25 points)
In this lab you are to examine and replicate the 2 following scripts. Enter, run, and
take note of the output. Each one has 3 questions. Answer each thoroughly, in your
own words. You may use the editor of your choice to complete the assignment.
Remember, page 199 explains how to execute these scripts. Lastly write a
shell script(example 3) and paste the code into your lab within the dropbox.
Example 1:
#!/bin/bash
echo -n "Enter directory name ->"
read dir
if [ -d "$dir" ]
then
echo "Directory exists"
else
`mkdir $dir`
echo "Directory created"
fi
Question 1
What does this script do? If you run it twice giving the same directory name both times,
what is the output to the screen on the second time through?
This script is used to create a directory if it does not exist. If you run it twice giving the same
directory name both times, the output to the screen on the second time is Directory exists
Question 2
What is “fi”?
'fi' indicates the end of the conditionally executed code.
Question 3
What is the purpose of the very first line in the above script?
the program loader is instructed to use the /bin/sh program
Example 2
#!/bin/bash
echo ‘Please enter your name’
read name
echo “My Name is $name”
Question 4
What is the difference between single and double quotes in bash scripting?
When the $name is enclosed within single quotes, then the text inside is retained and the
value does not get displayed. The $name does not get interpolated. But when it is closed
within double quotes, then the $name is evaluated, and the value is showed up.
pf2

Partial preview of the text

Download ULSA-Lab wk10 Scripting_.docx and more Summaries Network Programming in PDF only on Docsity!

ULSA Lab Chapter 7. (25 points)

In this lab you are to examine and replicate the 2 following scripts. Enter, run, and

take note of the output. Each one has 3 questions. Answer each thoroughly, in your

own words. You may use the editor of your choice to complete the assignment.

Remember, page 199 explains how to execute these scripts. Lastly write a

shell script(example 3) and paste the code into your lab within the dropbox.

Example 1:

#!/bin/bash echo -n "Enter directory name ->" read dir if [ -d "$dir" ] then echo "Directory exists" else mkdir $dir echo "Directory created" fi Question 1 What does this script do? If you run it twice giving the same directory name both times, what is the output to the screen on the second time through? This script is used to create a directory if it does not exist. If you run it twice giving the same directory name both times, the output to the screen on the second time is Directory exists Question 2 What is “fi”?

'fi' indicates the end of the conditionally executed code.

Question 3 What is the purpose of the very first line in the above script?

the program loader is instructed to use the /bin/sh program

Example 2

#!/bin/bash echo ‘Please enter your name’ read name echo “My Name is $name” Question 4 What is the difference between single and double quotes in bash scripting? When the $name is enclosed within single quotes, then the text inside is retained and the value does not get displayed. The $name does not get interpolated. But when it is closed within double quotes, then the $name is evaluated, and the value is showed up.

Question 5 What does the $ signify in scripting? It is a special variable in shell that reads the exit status of the last command executed. Question 6

In the last line, would $name be the same as $Name? Why?

names are case-sensitive. For example, $date and $DATE are two different variables.

Example 3

This time, it’s your turn:

Write a simple script(using any language) where the user enters a number

and the script outputs whether that number is greater, lesser, or equal to

using System; namespace CshapeExample { class Program { static void Main(string[] args) { Console.WriteLine("Enter a number:"); int num = Console.ReadLine(); if(num>10){ Console.WriteLine(“Your number is greater than 10”); } if(num=10){ Console.WriteLine(“Your number is equal to 10”); } else{ Console.WriteLine(“Your number is lesser than 10”);} } } }