The For Loop Lab
The lab is due by next class period. Use your VI editor to type the script, unless otherwise noted.
1.Enter vi and create a file called fruits with the code below:
for fruits in apples pears oranges mangoes
echo $fruits are fruits
2.Save and exit the editor
3.Change the permissions on the file. Give the yourself read, write, and execute and the group and others read only. How did you do this?
4.Execute the file fruits. Record the output below:
5.At your prompt, type vi namelist (This opens your VI Editor and creates a file named: namelist)
for arg in Tom John Eric
6.Save and exit the editor
7.Make the file executable.
8. Execute namelist
9.Type the following program:
for count in 1 2 3 4 5
echo “In the loop for $count times”
10.Run the program and record the output:
11. Is the script above a counter? Why or why not?
12.On your own, create a for loop that will give the following output:
The Mets are a Major League Baseball Team
The Yankees are a Major League Baseball Team
The Braves are a Major League Baseball Team
The Astros are a Major League Baseball Team
Write the code below, and circle the keywords:
The While and Until Lab
Use the VI editor to try the scripts below. Run the program and explain what the program is doing, unless you are asked to explain each line of code instead. Due next class.
while [ $carryon = Y ]
echo “This loop will continue as long as you type Y”
echo “Since you type $carryon, and that is not Y, the loop has ended”
while [ $counter != 10 ]
counter=`expr $counter + 1`
Explain what each line of code in the above program is doing
until [ $counter = 25 ]
counter=`expr $counter + 1`
4. Explain the difference between 2 & 3:
day=`date +%a` # Look up the Date command to see what this format does…
echo “Guess what day of the week it is today Use: Mon, Tue, Wed, Thu, Fri, Sat or Sun”
while [ $guess != $day ]
echo “wrong day, try again”
echo “Good Job, $guess is the right day!!!”
Type in and run the program. Then explain what each line of code is doing:
6. Try the following. Explain what happens:
while [ $x != 10 ]
x=`expr $x + 1`
7. Write a script that will count from 0 to 20 by 2’s.
8. Write a script that will count down from 100 to 0.
9. Write a script that will count to 243 multiplying by 3.
1.Create a file named breeds and type in the following information:
echo “Enter the name of a breed of animal”
case $breed in
arabiam|palomino|clydesdale)echo “$breed is a horse”;;
jersey|guernsey|holstein)echo “$breed is a cow”;;
husky|shepherd|setter|labrador)echo “$breed is a dog”;;
siamese|Persian|angora)echo “$breed is a cat”;;
*) echo “$breed is not in our catalog”;;
2.Save and exit the editor
3.Make the file breeds executable
4.Run the program 5 times, and give the output for each:
First try type:husky
Second try type:holstein
Third try type:clydesdale
Fourth try type:angora
Fifth try type:terrier
5.Type the following program into your editor, and then run it.
echo “Use one of the following options:”
echo “dTo display the time and date”
echo “pTo see what directory you are currently in”
echo “wTo see a list of who is online”
echo “Enter your option and hit Enter: \c”
case $option in
*)echo“That was not a valid selection”;;
echo “Good bye”
6. Explain what the program above accomplished:
echo “Type a letter from the Greek alphabet”
case $letter in
Alpha|alpha|ALPHA)echo “$letter is the first letter in the Greek alphabet”;;
Beta|beta|BETA)echo “$letter is the second letter in the Greek alphabet”;;
Gamma|GAMMA|gamma)echo “$letter is the third letter in the Greek alphabet”;;
OMEGA|omega|Omega) echo “$letter is the last letter in the Greek alphabet”;;
*)echo “$letter is not a letter in the Greek alphabet”;;
Explain what the pipe symbol is doing in the above program.
8. Write your own case statement where the user can type the name of a Car Maker, and your program will give a response determined by you. For example:
Question:What Car Maker are you interested in?
Program Response:Ford is a good car
Do this for 4 different car makers. Also remember to include a catchall in case the user types in a car maker that does not exist.
9. Using your knowledge of the while loop, take the program from #5, and nest that case statement inside a while loop, so the case statement will keep on running until q is typed. I left spaces below where the parts of the while loop, and the interactivity between the user and the program would go (ex. asking the user to enter a new choice, and allowing for it).
line 3: “Use one of the following options:”
line 4:echo “dTo display the time and date”
line 5:echo “pTo see what directory you are currently in”
line 6:echo “wTo see a list of who is online”
line 6:echo “ Enter your option and hit Enter: \c”
line 7:read option
line 10:case $option in
line 14:*)echo“That was not a valid selection”;;
line 20:echo “Good Bye”
The If Lab Lab 12 Try the programs below. Please be sure to use your editor, save the program and change the permissions before you run them. Then explain what the program is doing in the space provided. Write the number five next to your name above for two extra credit points on the next exam. 1. echo “Enter a Number” read num1 echo “enter another number” read num2 if [ $num1 = $num2 ] then echo “$num1 equals $num2” elif [ $num1 –ne $num2 ] then echo “$num1 is not equal to $num2” fi 2. echo “Please enter a number” read num echo “Please enter another number” read num2 if [ $num \< $num2 ] then echo “$num is less then $num2” elif [ $num \> $num2 ] then echo “$num is greater then $num2” else echo “$num is equal to $num2” fi What does –lt stand for? What does –gt stand for? What does –eq stand for? 3. echo “what day of the week is it, Monday, Tuesday, Wednesday, Thursday or Friday?” read day if [ $day = “Monday” ] then echo “I hate Monday’s” elif [ $day = “Tuesday” ] then echo “Tuesday Night is fun” elif [ $day = “Wednesday” ] then echo “Wednesday is the hump day” elif [ $day = “Thursday” ] then echo “Thursday, only 1 more day till Friday” elif [ $day = “Friday”] then echo “Party time lets have some fun” else echo “S-A-T-U-R-D-A-Y...NIGHT…The Bay City Rollers…” fi 4. Write an if-then-elif statement that will allow you to type the name of a ice cream flavor, and give back a response. Include a message that will tell the user if they entered an ice cream flavor that is not in your list.
Instructions: Using your knowledge of the Case, While, Until, and For Loop write programs to satisfy the questions below. Please include the code you used to create each program. 1. Using a for loop, write a program that will take the following list of words, and print out this response: The (word) is an animal The (word) is an animal The (word) is an animal The (word) is an animal 2. Write a loop that will count backwards from 10 to 0. 3. Write a short program that will allow a user to input two numbers, and then the program will give the product of the two numbers. 4. Write a program that will allow the user to type in a Season, and give back a response about that season. Please be sure to allow the user to type in the season using capital letters, and be sure to include an output if the user typed in a season that is not valid. 5. Write a program that create a menu, and let the user choose which option of the menu they wish to use. Have the program keep working until the user decides they wish to quit the menu.
Instructions: Complete the following Programs. You have the entire class period to work. The lab is due at the beginning of the next class. Print the program code to a printer. 1. Create counters for the following, using a while loop for two and an until loop for two. Add from 0 to 500, by 5’s. Subtract from 100 – 0, by 1’s Using 2, multiply from 0 to 1024 Divide by 4, from 1048576 to 1 2. Write a case statement that will allow the users to enter a sport. Then the computer will give an opinion on those sports. You choose the opinions and sports. Please choose at least 4 sports, and remember to include a catchall. 3. Write an If-then statement that will allow for a user to put in a color, then give back a desired response. You choose the colors and response. Please choose at least 3 colors. 4. Write a for statement that will allow for the following commands to run: ps who finger date 5. Write a simple script that will allow a user to type in two numbers, then will add the numbers, and tell if the numbers the user typed in, are greater then, or less then in the order in which they were typed.
|Due By (Pacific Time)||12/31/2013 12:00 am|
out of 1971 reviews
out of 766 reviews
out of 1164 reviews
out of 721 reviews
out of 1600 reviews
out of 770 reviews
out of 766 reviews
out of 680 reviews