Basic Bash Talking AI Example

View previous topic View next topic Go down

Basic Bash Talking AI Example

Post by jamied_uk on 29th June 2017, 23:00



To get this working install espeak

Code:
sudo apt install -y espeak



Code:



Code:
#!/bin/bash
#
# jnet.forumn.biz/t1523-basic-bash-talking-ai-example#2267
#
# (c) J~Net 2017
#
# Usage: ./talk.sh
#
#
#
clear
echo "What is your name?"
echo "What is your name?" | espeak
read name
echo "Hi $name, how are you?"
echo "Hi $name, how are you?" | espeak
read how

if [ "$how" = "good" ] || [ "$how" = "ok" ]
then
  echo "Ok $name, I am happy you are $how"
  echo "Ok $name, I am happy you are $how" | espeak
else
  echo "Ok $name, Why are you $how"
  echo "Ok $name, Why are you $how" | espeak
  read why
fi


Make executable:

Code:
sudo chmod +x *.sh

Run:

Code:
./talk.sh
avatar
jamied_uk
Admin

Posts : 2278
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Basic Bash Talking AI Example

Post by jamied_uk on 29th June 2017, 23:47

Part 2



Code:

Code:
#!/bin/bash
#
# jnet.forumn.biz/t1523-basic-bash-talking-ai-example#2267
#
# (c) J~Net 2017
#
# Usage: ./talk.sh
#
#
#
clear
echo "What is your name?"
echo "What is your name?" | espeak
read name
echo "Hi $name, how are you?"
echo "Hi $name, how are you?" | espeak
read how

if [ "$how" = "good" ] || [ "$how" = "ok" ]
then
  echo "Ok $name, I am happy you are $how"
  echo "Ok $name, I am happy you are $how" | espeak
else
  echo "Ok $name, Why are you $how"
  echo "Ok $name, Why are you $how" | espeak
  read why
  echo "Ok $name, I hope $why gets sorted soon."
  echo "Ok $name, I hope $why gets sorted soon." | espeak
fi


echo "Pick something to do..."
echo "Pick something to do...(Number)" | espeak
echo "1 Play a game"
read opts
echo "You have selected $opts"
echo "You have selected $opts" | espeak

if [ "$opts" = "1" ]
then
# Play a game...
bash Apps/Games/Guess_Number.sh
fi

For this part to work you will need a folder called Apps and inside that folder you will also need a folder called Games

inside that Games folder you will need this file...


Code:
#!/usr/bin/bash
#
# Guess_Number.sh - guessing game in BASH (Bourne Again Shell)
#
# This is written to demonstrate this language versus the same program
# written in other languages.
#
# 17-Oct-2004    Brendan Gregg    Created this.
# Updated By J~Net 2017
# jnet.forumn.biz/t1523-basic-bash-talking-ai-example#2267
#
scorefile="highscores_bash"
guess=-1
typeset -i num=0

echo -e "guess.bash - Guess a number between 1 and 100\n"

### Generate random number
(( answer = RANDOM % 100 + 1 ))

### Play game
while (( guess != answer )); do
    num=num+1
        echo "Choose a number" | espeak
    read -p "Enter guess $num: " guess
    if (( guess < answer )); then
        echo "Higher..."
        echo "Higher..." | espeak
    elif (( guess > answer )); then
        echo "Lower..."
        echo "Lower..." | espeak
    fi
done
echo -e "Correct! That took $num guesses.\n"
echo -e "Correct! That took $num guesses." | espeak

### Save high score
echo "Please enter your name for the scoreboard" | espeak
read -p "Please enter your name: " name
echo $name $num >> $scorefile

### Print high scores
echo "Previous high scores" | espeak
echo -e "\nPrevious high scores,"
cat $scorefile

you will need to make it executable like the first file

Code:
sudo chmod +x *.sh
avatar
jamied_uk
Admin

Posts : 2278
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Basic Bash Talking AI Example

Post by jamied_uk on 30th June 2017, 00:21

Part 3



talk.sh now has a menu function for returning back from a game for example.



Code:
#!/bin/bash
#
# jnet.forumn.biz/t1523-basic-bash-talking-ai-example#2267
#
# (c) J~Net 2017
#
# Usage: ./talk.sh
#
#
#
clear
echo "What is your name?"
echo "What is your name?" | espeak
read name
echo "Hi $name, how are you?"
echo "Hi $name, how are you?" | espeak
read how

if [ "$how" = "good" ] || [ "$how" = "ok" ]
then
  echo "Ok $name, I am happy you are $how"
  echo "Ok $name, I am happy you are $how" | espeak
else
  echo "Ok $name, Why are you $how"
  echo "Ok $name, Why are you $how" | espeak
  read why
  echo "Ok $name, I hope $why gets sorted soon."
  echo "Ok $name, I hope $why gets sorted soon." | espeak
fi

# Menu Function
Menu () {
echo "Pick something to do..."
echo "Pick something to do...(Number)" | espeak
echo "1 Play a game"
read opts
echo "You have selected $opts"
echo "You have selected $opts" | espeak

if [ "$opts" = "1" ]
then
# Play a game...
 bash Apps/Games/Guess_Number.sh $name
fi
}

Menu

echo "Welcome back"
echo "Welcome back" | espeak

Menu


Updated Game

Guess_Number.sh


Code:
#!/usr/bin/bash
#
# Guess_Number.sh - guessing game in BASH (Bourne Again Shell)
#
# This is written to demonstrate this language versus the same program
# written in other languages.
#
# 17-Oct-2004    Brendan Gregg    Created this.
# Updated By J~Net 2017
# jnet.forumn.biz/t1523-basic-bash-talking-ai-example#2267
#
name="$1"
scorefile="highscores_bash"
guess=-1
typeset -i num=0

echo -e "guess.bash - Guess a number between 1 and 100\n"

### Generate random number
(( answer = RANDOM % 100 + 1 ))

### Play game
while (( guess != answer )); do
    num=num+1
        echo "Choose a number" | espeak
    read -p "Enter guess $num: " guess
    if (( guess < answer )); then
        echo "Higher..."
        echo "Higher..." | espeak
    elif (( guess > answer )); then
        echo "Lower..."
        echo "Lower..." | espeak
    fi
done
echo -e "Correct! That took $num guesses.\n"
echo -e "Correct! That took $num guesses." | espeak

### Save high score
# echo "Please enter your name for the scoreboard" | espeak
# read -p "Please enter your name: " name
echo $name $num >> $scorefile

### Print high scores
echo "Previous high scores" | espeak
echo -e "\nPrevious high scores,"
cat $scorefile
avatar
jamied_uk
Admin

Posts : 2278
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Basic Bash Talking AI Example

Post by jamied_uk on 30th June 2017, 00:45



The Updated File Link
app.box.com/s/414qsmn2oei3lr014iw4bd50j7e9nvw8


Please Note: The aes file to be decrypted will need to be placed in the correct folder along with the script you choose to decrypt it!
avatar
jamied_uk
Admin

Posts : 2278
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Basic Bash Talking AI Example

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum