Question

In: Computer Science

CIT 371 Lab 12: shell scripting Start VMware, your VM and log in as Student. cd...

CIT 371 Lab 12: shell scripting

Start VMware, your VM and log in as Student. cd to your home directory and create a subdirectory called scripts. cd to that directory. You can use gedit or some other editor but you need to get used to vi, so I strongly recommend that you use vi. Remember before running any new script that you will need to modify its permissions to be executable. Use either 745 or 755.

  1. Create a script called script1 with the following. Match the syntax precisely. Pay close attention to the use of the back tick marks on line 4. This is the character right above the tab key on your keyboard.

#!/bin/bash

echo You are $USER

echo Your home directory is $HOME

echo Your home directory consists of `du –sH ~`

Run the script by typing ./script1 <enter>. Alter your script so that the output of each echo statement is redirected to the file info.txt. How did you do this?

  1. Create the following script, named script2 and run it.

#!/bin/bash

echo What is your name?

read NAME

echo What is your username?

read USERNAME

echo Hello $NAME, your home directory contents and size:

Complete this script by adding instructions to output this user’s home directory contents (using ls) assuming that the user’s home directory will be /home/$USERNAME as well as using the du statement from script1 to output the disk usage of the user’s home directory. When done, add this script to your answer file.

  1. Rewrite the script from #2 so that the NAME and USERNAME are supplied to the script as parameters, removing the first four instructions (the first two echo and the two read statements). Save and test your script. When done, add this script to your answer file.

  1. Write the following script, calling the file script

#!/bin/bash

if [ $# -ne 2 ]; then echo Illegal input

elif [ $1 –gt $2 ]; then echo $1 is greater

else echo $2 is greater

fi

Save and run the script providing it no parameters, two parameters of 5 and 10, two parameters of 10 and 5, and two parameters of 5 and 5. What does [ $# -ne 2 ] mean? Do we need this in the script?

  1. Modify the script from #4 so that it will also output a message if the two parameters are the same. For instance, it might output “5 equals 5”. When done, include the modified script as the answer to this step.
  1. Write the following script calling it script

#!/bin/bash

read –p “Enter the number you seek   ” NUM

for VALUE in $@; do

     if [ $VALUE –eq $NUM ]; then COUNT=$((COUNT+1)); fi

done

echo $NUM appeared $COUNT times

Run script5 passing it the list of numbers 5 10 6 12 5 18 10 4 19 21 5 12 18 22 and when prompted, input 5. Rerun the script inputting 18 instead. Rerun the script inputting 23 instead. What outputs did you get for each input?

  1. Rewrite the script from #6 to input two inputs from the user, instead of just NUM, and count the number of parameters that are equal to or in between the two. For instance, if the user inputs 12 and 20, we would have 5 matches (12, 18, 19, 12, 18). Assume the first input is less than the second (that is, you do not need to worry about the user inputting 20 and then 12). Add this revised script to your answers file.
  1. Write a script which receives a list of parameters and iterates through them using a for loop similar to what you wrote in #6. In this case, the for loop requires two if statements (or an if-then-else) to locate the smallest and the largest values of the inputs. Output both values. Assume the list of parameters contains at least one number. As an example, if the list of parameters was 5 10 6 12 5 18 10 4 19 21 5 12 18 22, it will output that 4 is the smallest and 22 is the largest. When done, add this script to your answer file.

Shut down your VM, exit VMware and submit your lab report.

Solutions

Expert Solution

Script 1
***********

bash script1.sh > info.txt

Script 2
**********

#!/bin/bash
echo "What is your name?"
read NAME

echo "What is your username?"
read USERNAME

echo "Hello $NAME, your home directory contents and size: `ls`"
echo
echo "size: `du -sH /home/$USERNAME`"

Modified

#!/bin/bash

echo "Hello $1, your home directory contents and size: `ls`"
echo
echo "size: `du -sH /home/$2`"

Script 3
**********

#!/bin/bash

if [ $# -ne 2 ]
then
   echo "Illegal input"
elif [ $1 -gt $2 ]
then
   echo "$1 is greater"
else
   echo "$2 is greater"
fi

What does [ $# -ne 2 ] mean

This means if there must be 2 arguments if not then condition will be false

Script 4
*********

#!/bin/bash

if [ $# -ne 2 ]
then
   echo "Illegal input"
elif [ $1 -gt $2 ]
then
   echo "$1 is greater"
elif [ $1 -eq $2 ]
then
   echo "$1 equals $2"
else
   echo "$2 is greater"
fi

Script 5
************

#!/bin/bash

read -p "Enter the 1st number " NUM1
read -p "Enter the 2nd number " NUM2

for VALUE in $@;
do
if [ $VALUE -ge $NUM1 ] && [ $VALUE -le $NUM2 ]
then
    COUNT=$((COUNT+1))
fi
done
echo
echo "$NUM appeared $COUNT times"

#!/bin/bash
echo "enter size of an array"
read n
#taking input from user
for((i=0;i<n;i++))
do
echo " enter $((i+1)) number"
read nos[$i]
done
#printing the entered number
echo "number entered are"
for((i=0;i<n;i++))
do
echo ${nos[$i]}
done
#main loop
small=${nos[0]}
greatest=${nos[0]}
for((i=0;i<n;i++))
do
#logic for smallest number
if [ ${nos[$i]} -lt $small ]; then
small=${nos[$i]}
#logic for greatest number
elif [ ${nos[$i]} -gt $greatest ]; then
greatest=${nos[$i]}
fi
done
#printing smallest and greatest number
echo "smallest number in an array is $small"
echo "greatest number in an array is $greatest"

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and i can edit and change the answers if you argue, thanks :)


Related Solutions

In this lab, you will learn how to create shell scripts using the Bourne Shell Scripting...
In this lab, you will learn how to create shell scripts using the Bourne Shell Scripting language. The student will have to do research on the Bourne Shell Scripting language and then writ a script that asks the First Name , Last Name, Age, and Country of origin of the student and then print out these items in a statement Then the students will write a 3 to 4-page paper (not including the title and references pages) describing how the...
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
Start an FC-PC Lab Assignment document and save it to your drive. Note: As you complete...
Start an FC-PC Lab Assignment document and save it to your drive. Note: As you complete the below exercises add them to your Assignment document and save it Create the algorithms in both flowchart and pseudocode form for the following two program requirements:   Given is the array prices with 100 elements(prices[100]). The problem consists of two parts:  1. Find the highest price in the array; and  2. Reduce that price by 10% For any three numbers input by the user, output the...
Log onto website where you can observe your service bill for the last 12 months (electric...
Log onto website where you can observe your service bill for the last 12 months (electric bill, cell phone bill, water bill, etc.). If you do NOT feel comfortable sharing this data, you can make up values. In excel, list the values of your bill for the last 12 months on one column. Find the sample mean and sample standard deviation of your data. Pick three bills from the last 12 months and change the values into z-scores. What does...
Log onto website where you can observe your service bill for the last 12 months (electric...
Log onto website where you can observe your service bill for the last 12 months (electric bill, cell phone bill, water bill, etc.). If you do NOT feel comfortable sharing this data, you can make up values. In excel, list the values of your bill for the last 12 months on one column. Find the sample mean and sample standard deviation of your data. Pick three bills from the last 12 months and change the values into z-scores. What does...
Problem : You are a very savvy college student and start your own food stand from...
Problem : You are a very savvy college student and start your own food stand from a catering truck. You understand the thought process of your peers and decide to do business when it would be most profitable, from 10:00pm to 3:00am. Considering vacations and finals, you only operate the catering truck 320 days per year. The truck is leased by you for $1000/month with a continuous 12 month lease. Additional expenses for insurance, maintenance, etc come out to $2000...
After graduation, you plan to work for Dynamo Corporation for 12 years and then start your...
After graduation, you plan to work for Dynamo Corporation for 12 years and then start your own business. You expect to save and deposit $7,500 a year for the first 6 years (t = 1 through t = 6) and $15,000 annually for the following 6 years (t = 7 through t = 12). The first deposit will be made a year from today. In addition, your grandfather just gave you a $37,500 graduation gift which you will deposit immediately...
After graduation, you plan to work for Dynamo Corporation for 12 years and then start your...
After graduation, you plan to work for Dynamo Corporation for 12 years and then start your own business. You expect to save and deposit $7,500 a year for the first 6 years (t = 1 through t = 6) and $15,000 annually for the following 6 years (t = 7 through t = 12). The first deposit will be made a year from today. In addition, your grandfather just gave you a $25,000 graduation gift which you will deposit immediately...
Assume that you recently applied for a student loan to go to graduate school. As part of the application process, your bank requested a list of your assets. Aside from an extensive CD collection, your only other asset is a pickup truck.
DepreciationAssume that you recently applied for a student loan to go to graduate school. As part of the application process, your bank requested a list of your assets. Aside from an extensive CD collection, your only other asset is a pickup truck. You purchased the truck six years ago for $15,000. Its current fair value is approximately $5,000.a. What factors caused your pickup truck to decline $10,000 in value?b. Assume that the bank is willing to lend you money for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT