473,836 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bash shell scripting

keyvanrahmadi
57 New Member
Sorry if this post is rather long but hopefully you wont get bored half way through. I have a project in hand which i have started and have a deadline of 1 week. Basically what i need to do is to create a shell script in Bash for the game Hangman. I am sure everyone is well aware of the game. The down side is that i can not use "sed" or "awk". FUN FUN

I am at my ends wit now and truley utterly lost, so any help would go a long way to stop my pulling my wife's hair out ( i ran out of my own hair a long time ago :)

The game has to have the option for 1 player or 2 player, which needs to be stated at the started, when the script is run. This is what i have done to remedy this option:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/bin/bash
  3.  
  4. echo "Choose number of Players:"
  5. read players
  6. if [ $players -eq 1 ]
  7. then
  8. echo "The script for thsi hasnt been written."
  9. elif [ $players -eq 2 ]
  10. then
  11. echo "choose a word for player two to guess:"
  12. read player2word
  13. else
  14. echo "incorrect option.. choose again"
  15. fi
  16.  
I am sure there is a better way of doing this, so if any suggestions would be great.

If a 1 player option is choosen, then the following which i havent even coded should happen:

1- A random word from a different file needs to be chosen.
2- The player should be told the word has been chosen and the display should be - - - - (number of dashes should be = to length of word.
3- player is prompted to guess a letter. If a correct letter is chosen then display the message: You have chosen <letter>, then echo the letter and place the chosen letter in correct field.



now 2 player is same as above except that player 1 has to input the word for player 2 to guess. So far i have done this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. life=10
  3. blank="......................"
  4. word=$player2word
  5. letters=$(echo $word | wc -c)
  6. letters=$(( $letters - 1 ))
  7. template="$(echo $blank | cut -c1-$letters)"
  8. remaining=$letters
  9. guessed=""
  10. guesses=0
  11. badguesses=0
  12. echo " ************** The word you are trying to guess has $letters letters **************"
  13. echo ""
  14. echo ""
  15.  
  16. while [ $remaining != $word ] ; do
  17.  
  18. echo -n "Word is: $template Please choose a Letter: " ;
  19. read guess
  20. guesses=$(( $guesses + 1 ))
  21. echo " you have chosen $guess"
  22.  
  23. if echo $guessed | grep -i $guess > .temp1 ; then
  24.  
  25. echo "You've already guessed that letter. Try again!"
  26.  
  27. elif ! echo $word | grep -i $guess > .temp1 ; then
  28.  
  29. echo "Sorry, the letter \"$guess\" is not in the word."
  30. guessed="$guessed$guess"
  31. badguess=$(( $badguess + 1))
  32. life=$(( $life -1 ))
  33. echo " you have $life left"
  34. else
  35.  
  36. echo "Good going! The letter $guess is in the word!"
  37. echo $letter
  38.  
  39. fi
  40. done
  41.  
  42. echo -n "Congratulations! You guessed $word in $guesses guesses"
  43.  
  44. echo " with $badguesses bad guesses"
  45.  
  46.  
problems with above script is as follow:

1- Not finished
2- when a letter is chosen the script excepts the fact its a correct or an incorrect letter but dosent insert it into ..... location.
3- i am sure there is more, best thing i can suggest if you can run it and see the result then you will have a better understading of it.

I know this is a tall order, but any help would be greatly appriciated and in my part main thing is to learn and understand from all you guys out there who have far more knowledge than me in shell scripting.

thank you

K
May 15 '07
14 4919
keyvanrahmadi
57 New Member
Hmm, it works fine for me ... except that I replaced $template1 by $template in my code.
mine creates a continues line as follow:

Word is: ______ Please choose a Letter:

Keyvan
May 17 '07 #11
arne
315 Recognized Expert Contributor
mine creates a continues line as follow:

Word is: ______ Please choose a Letter:

Keyvan
Hmm, and you would like to have "_ _ _ _"? My shell does that ...

You could introduce blanks, but this will make your code somewhat more complicated. so why not staying with the dots? ;-)
May 17 '07 #12
keyvanrahmadi
57 New Member
Hmm, and you would like to have "_ _ _ _"? My shell does that ...

You could introduce blanks, but this will make your code somewhat more complicated. so why not staying with the dots? ;-)
hehehe in that case i play around with it and possibly leave it with dots :)

btw what is "ndx"... i cant find any reference to it

Thx

K
May 17 '07 #13
arne
315 Recognized Expert Contributor
hehehe in that case i play around with it and possibly leave it with dots :)

btw what is "ndx"... i cant find any reference to it

Thx

K
See line 20 in my code for ndx.

The "_ _ _ _" could also easily be achieved by not echo'ing $template, but printing it letter by letter with blanks in between. Not soo complicated ...
May 17 '07 #14
keyvanrahmadi
57 New Member
See line 20 in my code for ndx.

The "_ _ _ _" could also easily be achieved by not echo'ing $template, but printing it letter by letter with blanks in between. Not soo complicated ...
was wondering what stands for :)

K

ps..i will try the to get the - - - sorted and let you know.
May 17 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
8261
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but does not print the file to the printer. It displays the following as the return from Runtime...: java.lang.UNIXProcess@1034bb5
3
2678
by: William Park | last post by:
I'm very excited to announce shell interface to GTK+2 (2.6.1) for Bash. It read XML syntax describing the widget layout, and returns user selection as shell variable or runs shell command as callback. It's designed for simple GUI dialog or layout, with the emphasis on getting the user data back into shell. For the moment, the shell variable and command are disabled. It just prints to stdout, instead. But, you can change it easily.
11
3000
by: Magnus Jonneryd | last post by:
Hi, I'm planning on writing a program that interactively is fed input via a shell (bash). I also want to be able to write a shell script that executes various commands related to my program. In short i want to provide input to a program using some (or all) of the functionality found in bash. It's mainly the format of the file I'm having trouble with. I wanted to be able to write something like this: #!/bin/bash for x in xs
7
5574
by: chakkaradeepcc | last post by:
HI all, How to execute bash scripts from python (other than using os.popen) and get the values that those bash scripts return. I would be happy if someone could help me out in this.. thanks in advance.. With Regards,
4
1886
by: 4zumanga | last post by:
I have a bunch of really horrible hacked-up bash scripts which I would really like to convert to python, so I can extend and neaten them. However, I'm having some trouble mapping some constructs easily, and was wondering if anyone know of a guide to mapping simple uses of command line programs to python. For an example, the kind of thing I am thinking of are things like (yes, this is horrible code). # These are a run of a program I...
16
3956
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of the normal bash commands (e.g. print instead of echo). My main reason for asking is that I like using Python for everything, and if I don't need to learn the bash 'language', then I won't just yet. Thanks.
10
4461
by: Isaac Gouy | last post by:
$ ./test_fail 1 Floating point exception - but this leaves 'somefile' zero size $ ./test_fail 1>somefile Floating point exception
1
3282
by: Fredrik Lundh | last post by:
John Lawrence wrote: doesn't exactly work for Python scripts, though: $ cat env.py #!/usr/bin/env python import os os.environ = "hello"
6
1877
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions I need to write a program that creates real application/FTP accounts
0
9810
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9656
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7773
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6973
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.