├── README.md ├── calculator.sh ├── guess.sh ├── myrm.sh └── utilities.sh /README.md: -------------------------------------------------------------------------------- 1 | # Shell-Scripting-Projects 2 | Bash shell scripting 3 | 4 | * calculator.sh 5 | 6 | Emulates a simple calculator that supports addition, subtraction, multiplication and division. 7 | 8 | * guess.sh 9 | 10 | prompts the user to guess a number between 1 and 64 with hints, if the number guessed is too high or to low 11 | if the user can guess the correct number in 6 attempts or less, the user wins. Otherwise the user loses. 12 | 13 | * myrm.sh 14 | 15 | emualtes the rm command on unix, remove file, except this script does a safe delete. 16 | Puts the file to be removed in a new temprary folder called recycle-bin 17 | 18 | * utilities.sh 19 | 20 | performs basic utility task using an interactive menu from the following queries 21 | 1) What is the ongoing processor activity 22 | 2) Information about the users currently logged in 23 | 3) The number of users currently logged in 24 | 4) The login names only of the users that use bash, sorted and with duplicates removed 25 | -------------------------------------------------------------------------------- /calculator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | value1=$1 # first parameter 3 | operator=$2 # second parameter 4 | value2=$3 # third parameter 5 | zero=0; # decalring variable 6 | 7 | div(){ 8 | if [[ $value2 != $zero ]] # if (divisor != 0) then simply divide integers 9 | then 10 | echo $((value1/value2)) 11 | else # else display this message 12 | echo "Division-by-zero Error!" 13 | fi 14 | } 15 | 16 | error(){ 17 | echo "Usage - ./calculator.sh value1 operator value2" 18 | echo "where," 19 | echo "value1: numeric value" 20 | echo "value2: numeric value" 21 | 22 | } 23 | 24 | if [ $# -gt 3 ] 25 | then 26 | error 27 | 28 | elif [ $# -eq 0 ] 29 | then 30 | error 31 | 32 | elif [ "$2" == "+" ] 33 | then 34 | echo $((value1+value2)) 35 | 36 | elif [ "$2" == "-" ] 37 | then 38 | echo $((value1-value2)) 39 | 40 | elif [[ $2 == / ]] 41 | then 42 | div # calls div() function 43 | 44 | elif [ "$2" == "x" ] 45 | then 46 | echo $((value1*value2)) 47 | else 48 | error 49 | 50 | fi 51 | -------------------------------------------------------------------------------- /guess.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Welcomme to the number game" 4 | echo "Guess a number between 1 and 64 (inclusive)" 5 | 6 | RANGE=64 # our upperbound on the range 7 | number=$RANDOM 8 | let "number %= $RANGE" # gives us a random # between 0 and our specified range = 64 9 | 10 | for i in 1 2 3 4 5 6 # for 6 iterations 11 | do 12 | read value 13 | if [ "$value" -lt "$number" ] # if (value < number) 14 | then 15 | echo "Too small" 16 | echo "Try again" 17 | elif [ "$value" -gt "$number" ] # if (value > number) 18 | then 19 | echo "Too big" 20 | echo "Try again" 21 | else 22 | echo "You Won!" 23 | exit 24 | fi 25 | done 26 | 27 | echo "You lost!" 28 | -------------------------------------------------------------------------------- /myrm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #if the path does not already exist, then make it. 3 | if [ ! -d "/tmp/garhajas/eecs2031m/a1/recycle-bin" ] 4 | then 5 | mkdir -p /tmp/garhajas/eecs2031m/a1/recycle-bin 6 | fi 7 | 8 | if [ $# -gt 0 ] #if there are more than 0 files to delete 9 | then 10 | for i in $@ 11 | do 12 | if [ -e "$i" ] 13 | then 14 | mv $i /tmp/garhajas/eecs2031m/a1/recycle-bin 15 | echo "deleteing $i" 16 | else 17 | mv $i /tmp/garhajas/eecs2031m/a1/recycle-bin 18 | fi 19 | done 20 | 21 | else 22 | #display error message 23 | echo "Error: no target specified" 24 | echo "usage:./myrm " 25 | fi -------------------------------------------------------------------------------- /utilities.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | clear 3 | 4 | menu(){ 5 | echo "----------------------------" 6 | echo 7 | echo " MAIN - MENU" 8 | echo 9 | echo "----------------------------" 10 | echo 11 | echo "1. Ongoing Processor Activity" 12 | echo "2. Users currently logged in" 13 | echo "3. Number of users currently logged in" 14 | echo "4. Users with bash shell" 15 | echo "5. Exit" 16 | echo 17 | echo "----------------------------" 18 | echo 19 | echo "Please enter option [1 - 5]:" 20 | } 21 | 22 | option=y 23 | while [ "$option" != "5" ] 24 | do 25 | menu # calls the menu function each time, while the whileloop remains true 26 | read option 27 | case $option in 28 | 1) top;; # the user has to press [q] to exit and get back to main menu 29 | 2) who;; 30 | 3) who | wc -l;; 31 | 4) ps aux | grep bash | grep -v grep | cut -d " " -f 1 | sort | uniq;; 32 | 5) break;; # exit 33 | *) echo "Wrong option";; # * means anything else 34 | esac 35 | 36 | read -p "Press [enter] key to continue..." # waits for the user to press [enter] to continue 37 | clear 38 | 39 | done 40 | 41 | clear 42 | --------------------------------------------------------------------------------