Thursday, April 7, 2011

Assignment 4.2 (continued)

Part 2

There were a couple tutorials that I really liked. I'll choose this one as the best because it shows you a basic program, which is a little guessing game.



This video goes over if statements and how to use the less-than, equal, greater-than, etc. Here's the code for the guessing game:


clear
ANSWER = $RANDOM

while [1]; do
{
echo "Enter your guess (number): "
read USER_GUESS
if [ $USER_GUESS -eq $ANSWER ]; then
{
echo "Correct guess."
exit 0
}
elif [ $USER_GUESS -gt $ANSWER ]; then
{
echo "Too high. Try again."
}
else
{
echo "Too low. Try again."
}
fi
}
done


I wanted to mention this video as well:

http://www.youtube.com/watch?v=QGvvJO5UIs4

This one was nice because it showed you how to easily create and modify text files. This was something that other tutorials didn't cover, or they had you open up a text editor, which you don't really need to do. You just type:

nano file_name.sh

and then it takes you into the file to edit it. To quit and save the file you press control X and it takes you back to the normal terminal. To run the script, you type:

./file_name.sh

Pretty simple. It also teaches you how to write a simple script that asks for user input then stores what you typed into a separate text file. Here's the code:


#!/bin/bash

clear
echo "Welcome"
sleep 2
echo "how is your day going?"
read ans

clear
echo "you typed $ans"
echo "$ans" >> ans.txt

cat ans.txt

No comments:

Post a Comment