One step closer to world domination
Posts tagged Bash
Bash Examples
Nov 23rd
A while ago a friend was taking an entry level Unix programming class and asked me to help him study. I came up with some sample problems for him. Here are my solutions:
The first is a program that will allow a user to input as many numbers as the user wants (999 as the choice that ends the user input). The program will then output the highest number, the lowest number, the sum of all the numbers, and the average number.
#!/bin/bash COUNT=0 echo -n "Enter a number (999 to quit): " while read NUM; do # Set initials if [ $COUNT -eq 0 ]; then HIGHEST=$NUM LOWEST=$NUM SUM=$NUM fi # If 999 is entered, break out if [ $NUM -eq 999 ]; then break else # See if inputted number is the highest number if [ $NUM -gt $HIGHEST ]; then HIGHEST=$NUM fi # See if inputted number is the lowest number if [ $NUM -lt $LOWEST ]; then LOWEST=$NUM; fi # Calculate sum SUM=$(($SUM+$NUM)) echo -n "Enter another number (999 to quit): " fi COUNT=$(($COUNT+1)) done AVERAGE=$(($SUM/$COUNT)) echo "Highest number: $HIGHEST" echo "Lowest number: $LOWEST" echo "Sum of the numbers: $SUM" echo "Average of the numbers: $AVERAGE"
The second program is a number guessing game. The computer picks a number from 0-60 then allows the user to guess at the number until the user gets it right. The program gives the user a hint on whether they should guess a higher or lower number.
#!/bin/bash RAN=$((RANDOM%60)) COUNTER=1 echo -n "I've guessed a number between 0-60, what is it? " while read GUESS; do if [ $GUESS -eq $RAN ]; then break; else echo if [ $GUESS -lt $RAN ]; then echo -n "Nope, try a higher number: " else echo -n "Nope, try a lower number: " fi fi COUNTER=$((COUNTER+1)) done echo echo "Yep, good job! It took you $COUNTER tries to guess correctly."
The third program allows a user to type in a number, then the program will either count up to the number provided by the user or count down from that number. In addition, a menu lets the user choose which counter they wish to use. In both cases that counter will either count down to 0, or start counting from 0.
#!/bin/bash echo -n "Enter a number: "; read NUM echo -n "Enter U to count up to $NUM and D to count down to 0: "; read CHOICE case $CHOICE in 'u'|'U') COUNTER=0 while [ $COUNTER -le $NUM ]; do echo $COUNTER COUNTER=$(($COUNTER+1)) done ;; 'd'|'D') while [ $NUM -ge 0 ]; do echo $NUM; NUM=$(($NUM-1)) done ;; *) echo "Error: You entered an invalid argument." esac
The last program asks the user if they want to add, subtract, multiply or divide two user inputted numbers. Then the program outputs the sum of number 1 and number 2, the difference of number 1 and number 2, the product of number 1 and number 2, and the quotient of number 1 and number 2
This program will run until the user wants to quit. Also, if the user puts in a 0 as the second number, we let the user know that if they choose divide that a division by zero error will occur.
#!/bin/bash while true; do echo "Please enter two numbers (type 'q' to quit): " echo -n "Number 1: " read NUM1 if [ $NUM1 == "q" ]; then exit; fi echo -n "Number 2: " read NUM2 echo -n "Enter (a) to add, (s) to subtract, (m) to multiply, or (d) to divide these numbers: " read OPERATION case $OPERATION in a) echo "The sum of $NUM1 and $NUM2 is: $(($NUM1+$NUM2))" ;; s) echo "The difference of $NUM1 and $NUM2 is: $(($NUM1-$NUM2))";; m) echo "The product of $NUM1 and $NUM2 is: $(($NUM1*$NUM2))" ;; d) if [ $NUM2 -eq 0 ]; then echo "You can't divide by 0, a division by zero error will occur." else echo "The quotient of $NUM1 and $NUM2 is: $(($NUM1/$NUM2))" fi ;; esac done










