Skip to content

Instantly share code, notes, and snippets.

@keinydev
Last active January 20, 2023 21:20
Show Gist options
  • Save keinydev/4b5bd5271e31ba8457e87b3fc68ff06b to your computer and use it in GitHub Desktop.
Save keinydev/4b5bd5271e31ba8457e87b3fc68ff06b to your computer and use it in GitHub Desktop.

User interaction with Bash


Bash Series

Basic Bash commands

Programming with Bash

Bash Control Structure

🔵 User interaction with Bash


Options

script_name.sh

# Simple condition
while getopts :u:p:bi option; do
  case $option in
    u) user=$OPTARG;;
    p) pass=$OPTARG;;
    b) echo "you send the 'b' option";;
    i) echo "you send the 'i' option";;
    ?) echo "I don't know what is $OPTARG"
  esac
done

echo "user: $user / pass: $pass"

Getting input during execution

script_name.sh

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

echo "What is your pass?"
read -s pass

read -p "What is your favorite animal? " animal

echo name: $name, pass: $pass, animal: $animal
#!/usr/bin/env bash
select animal in "cat" "dog" "bird"
do
 echo "you selected $animal"
 break
done
#!/usr/bin/env bash
select animal in "bird" "dog" "fish" "quit"
do
  case $animal in
    bird) echo "Birds like to fly";;
    dog) echo "Dog like to play catch";;
    fish) echo "Fish like to swim";;
    quit) break;;
    *) echo "I don't know that option"
  esac
done

Ensuring a response

script_name.sh

#!/usr/bin/env bash
if (($#<3)); then
  echo "This command requires 3 arguments"
  echo "user, name and favorite color"
else
  echo "username: $1"
  echo "name: $2"
  echo "favorite color: $3"
fi
#!/usr/bin/env bash
read -p "Favorite color? " fav
while [[ -z $fav ]]
do
  read -p "I need an answer " fav
done 
echo "your input was $fav"
#!/usr/bin/env bash
read -p "What year? [nnnn] " year
until [[ $year =~ [0-9]{4} ]]; do
  read -p "A four-digit year, please! [nnnn] " year
done
echo "your input was $year"

A game from linkedin course

#!/usr/bin/env bash
# The game definitions
guess() {
    local -i mynumber=$(( 1 + RANDOM % 10 ))
    read -rp "I'm thinking of a number between 1 and 10. What do you think it is? " theguess
    if (( theguess == mynumber )); then
        echo "You got it! Great Job!"; echo
    else
        echo "Nope. I was thinking of $mynumber. Try again!"; echo
    fi
    sleep 1
    choosegame
}

flip() {
    local -i mynumber=$(( 1 + RANDOM % 2 ))
    if (( mynumber == 1 )); then
        local face="HEADS"
    else
        local face="TAILS"
    fi
    printf "I flipped a coin and it was: %s\n\n" $face
    sleep 1
    choosegame
}

dice() {
    local -i mynumber=$(( 1 + RANDOM % 6 ))
    local -i mynumber2=$(( 1 + RANDOM % 6 ))
    printf "I rolled two dice and the results are: %s and %s.\n\n" $mynumber $mynumber2
    sleep 1
    choosegame
}

# The game chooser
choosegame() {
    select game in "Guessing Game" "Flip a Coin" "Dice Roll" "Exit"
    do
        case $game in
            "Guessing Game") guess;;
            "Flip a Coin") flip;;
            "Dice Roll") dice;;
            "Exit") exit;;
            *) echo "Please choose from the menu.";;
        esac
    done
}

# If there's an argument provided, jump right to that game. Otherwise, show the game chooser.
case $1 in
    "guess") guess;;
    "flip") flip;;
    "dice") dice;;
    *) choosegame;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment