Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Comand Line / Bash Magic 8 ball?

  1. #1
    Join Date
    Aug 2009
    Location
    Ireland / The Czech Repub
    Beans
    283
    Distro
    Ubuntu 20.04 Focal Fossa

    Comand Line / Bash Magic 8 ball?

    I feel sure one of these existed...

    I guess it's a program that ignores whatever your input is and gives you back one of maybe 4/8/16 (?) answers seemingly at random every time it gets any command at all.
    Did I dream it?
    __________________

  2. #2
    Join Date
    Aug 2009
    Beans
    34

    Re: Comand Line / Bash Magic 8 ball?

    Ran a quick search myself, most of the top results are for GUI versions, and in particular the magic 8 ball screenlet. Either way if it's just a simple program that displays one of so many random messages with no regard to the actual input (and assuming it has no extra options to pass to it when executed) then it should be fairly easy to write in bash or python.
    If I remember tomorrow I might quickly throw something together along those lines, probably using python.

    Edit: Nevermind, quickly wrote up something in two minutes, hope this is sort of what you want? (Requires python 2.6)
    Attached Files Attached Files
    Last edited by HoKaze; November 14th, 2010 at 11:04 PM.

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Comand Line / Bash Magic 8 ball?

    Hmmm, I bored. So here is a bash/fortune variant. You need to install fortune first, then create the fortune file, /usr/share/games/fortunes/magic8ball:
    Code:
    As I see it, yes
    %
    It is certain
    %
    It is decidedly so
    %
    Most likely
    %
    Outlook good
    %
    Signs point to yes
    %
    Without a doubt
    %
    Yes
    %
    Yes - definitely
    %
    You may rely on it
    %
    Reply hazy, try again
    %
    Ask again later
    %
    Better not tell you now
    %
    Cannot predict now
    %
    Concentrate and ask again
    %
    Don't count on it
    %
    My reply is no
    %
    My sources say no
    %
    Outlook not so good
    %
    Very doubtful
    %
    create the random access file:
    Code:
    sudo strfile /usr/share/games/fortunes/magic8ball
    and run:
    Code:
    read -p "What's your question?"$'\n' && fortune magic8ball

  4. #4
    Join Date
    Aug 2010
    Beans
    62

    Re: Comand Line / Bash Magic 8 ball?

    Nice simple script to have some command line fun. One problem: The terminal crashes if I launch the script from a custom launcher after I type the question and hit enter. Though the script works fine if I do ./magic8ball from terminal.


    Latest Linux and Ubuntu News - Ubuntu Vibes

  5. #5
    Join Date
    Aug 2009
    Beans
    34

    Re: Comand Line / Bash Magic 8 ball?

    Quote Originally Posted by Vrroom View Post
    Nice simple script to have some command line fun. One problem: The terminal crashes if I launch the script from a custom launcher after I type the question and hit enter. Though the script works fine if I do ./magic8ball from terminal.
    I'm pretty sure most launchers will run a program in the terminal and then close the terminal as soon as the program finishes. In other words, it'll print the response but close the terminal before you can actually even see the response. The way to avoid this is to either run it manually from the command line, add a pause onto the end of the script or request additional user input to close the script after the message has been displayed.
    (This applies to both the python script and the fortune method)

    Fix for python version:
    Add this code to the end of the file if you want to have to press enter to continue
    Code:
    raw_input()
    or add this to pause for 2 seconds:
    Code:
    import time
    time.sleep(2)
    Fix for bash/fortune method:
    Instead of using this command,
    Code:
    read -p "What's your question?"$'\n' && fortune magic8ball
    Use this command instead:
    Code:
    read -p "What's your question?"$'\n' && fortune magic8ball && sleep 2

  6. #6
    Join Date
    Aug 2010
    Beans
    62

    Re: Comand Line / Bash Magic 8 ball?

    Quote Originally Posted by HoKaze View Post
    I'm pretty sure most launchers will run a program in the terminal and then close the terminal as soon as the program finishes. In other words, it'll print the response but close the terminal before you can actually even see the response. The way to avoid this is to either run it manually from the command line, add a pause onto the end of the script or request additional user input to close the script after the message has been displayed.
    (This applies to both the python script and the fortune method)

    Fix for python version:
    Add this code to the end of the file if you want to have to press enter to continue
    Code:
    raw_input()
    or add this to pause for 2 seconds:
    Code:
    import time
    time.sleep(2)
    Fix for bash/fortune method:
    Instead of using this command,
    Code:
    read -p "What's your question?"$'\n' && fortune magic8ball
    Use this command instead:
    Code:
    read -p "What's your question?"$'\n' && fortune magic8ball && sleep 2
    Great work. Thanks alot


    Latest Linux and Ubuntu News - Ubuntu Vibes

  7. #7
    Join Date
    Aug 2010
    Beans
    62

    Re: Comand Line / Bash Magic 8 ball?

    One more question. Is it possible to loop entire thing after raw_input()? I mean after I hit enter I am again brought to the line 'what is your question' The only way to close terminal would be from window controls. Sorry I am no programmer and i have no idea how to do this


    Latest Linux and Ubuntu News - Ubuntu Vibes

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Comand Line / Bash Magic 8 ball?

    Code:
    while (True):
      raw_input("What is your question?\n")
      print random.choice(answers) + '\n'
    Code:
    while true; do read -p "What's your question?"$'\n' && fortune magic8ball; echo; done

  9. #9
    Join Date
    Aug 2010
    Beans
    62

    Re: Comand Line / Bash Magic 8 ball?

    Nice. Thanks alot


    Latest Linux and Ubuntu News - Ubuntu Vibes

  10. #10
    Join Date
    Aug 2010
    Beans
    62

    Re: Comand Line / Bash Magic 8 ball?

    I have combined everything form your scripts and added standard answers from wikipedia

    Code:
    #!/usr/bin/env python
    # Magic 8 ball program
    
    import random
    answers = ["As I see it, yes","It is certain","It is decidedly so","Most likely","Outlook good","Signs point to yes","Without a doubt","Yes","Yes definitely","You may rely on it","Reply hazy, try again","Ask again later","Better not tell you now","Cannot predict now","Concentrate and ask again","Don't count on it","My reply is no","My sources say no","Outlook not so good","Very doubtful"]
    raw_input("What is your question?\n")
    response = random.choice(answers)
    print response + '\n'
    while (True):
      raw_input("What is your question?\n")
      print random.choice(answers) + '\n'


    Latest Linux and Ubuntu News - Ubuntu Vibes

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •