473,810 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on Bash Scripting

keyvanrahmadi
57 New Member
Hello there guys,

i think i am at the end of this, which i have to edmit has been a huge learning curve for me, but i still have couple of problems, which i was hoping you can help me. I think tbh its a very simple thing which i am over looking and just require a pair of fresh eyes to look at. I greatly appriciate if you could have a look at the script and let me know what you think. if you have any solution, or advise just email me or pm here..

player 1 option scripts:


Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2.  
  3. function stored_word()
  4. {
  5.   case $(( $$ % 8 )) in
  6.     0 ) echo "energy"    ;;  1 ) echo "touch" ;;
  7.     2 ) echo "climbing" ;; 3 ) echo "declare" ;;
  8.     4 ) echo "marry"  ;;    5 ) echo "relax"   ;;
  9.     6 ) echo "bugs"     ;;    7 ) echo "inaccessible" ;;
  10.   esac
  11. }
  12.  
  13. function startup() {
  14.  
  15. echo "Choose number of Players:"
  16. read players
  17.  
  18. if [ $players -eq 1 ] ; then
  19.         echo " A Random word has been chosen... Enjoy the Game"
  20. elif [ $players -eq 2 ] ; then
  21.         echo "choose a word for player two to guess:"
  22.         read player2word
  23. else
  24.         echo "incorrect option..."
  25.         replay
  26. fi
  27. }
  28.  
  29. function replay() {
  30.  
  31. echo " Do you wish to play again? TYPE \"y\" to play again or \"n\" to quit"
  32. read choice
  33.  
  34. if [ $choice == y ] ; then
  35. startup
  36. else
  37. echo "Hope you enjoyed the game"
  38. exit 0
  39. fi
  40. }
  41.  
  42. startup
  43. life=10
  44. word=$(stored_word)
  45. letters=$(echo $word | wc -c)
  46. letters=$(( $letters - 1 ))
  47. template="$(echo $word | tr '[a-z A-Z 0-9]' '.')"
  48. remaining=$letters
  49.  
  50. echo "  ****The word you are trying to guess has \"$letters\" letters ****"
  51.  
  52. while [ $template != $word ] ; do
  53.         echo""
  54.         echo -n "Word is: $template Please choose a Letter: " ;
  55.         read guess
  56.          guesses=$(( $guesses + 1 ))
  57.          echo " you have chosen $guess"
  58.      if echo $guessed | grep -i $guess > .temp1 ; then
  59.          echo "You've already guessed that letter. Try again!"
  60.         guessed="$guessed$guess"
  61.         echo $letter
  62.      elif ! echo $word | grep -i $guess > .temp1 ; then
  63.          echo "Sorry, the letter \"$guess\" is not in the word."
  64.          guessed="$guessed$guess"
  65.          badguesses=$(( $badguesses + 1))
  66.          echo "$guessed"
  67.          life=$(( $life -1 ))
  68.          echo " you have $life life left"
  69.      else
  70.           echo "Good going! The letter $guess is in the word!"
  71.           guessed="$guessed$guess"
  72.           echo $letter
  73.      fi
  74.  
  75. if [ $life == 0 ]; then
  76.         echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""
  77.         replay
  78.         exit 0
  79.  
  80. fi
  81.  
  82.       lettercount=0
  83.       template2=""
  84.       while [ $lettercount != $(( $letters )) ] ; do
  85.           val1=${word:lettercount:1}
  86.  val2=${template:lettercount:1}
  87.   if [ "$val1" == "$guess" ] ; then
  88.           template2="$template2$guess"
  89.           elif [ "$val2" != '.' ] ; then
  90.           template2="$template2$val2"
  91.           else
  92.           template2="$template2."
  93.           fi
  94.           lettercount=$(( $lettercount + 1 ))
  95.       done
  96.       template=$template2
  97. done
  98.  
  99. echo -n "Congratulations! You guessed \"$word\" in \"$guesses\" guesses"
  100. echo " with $badguesses incorrect guesses"
  101. replay
  102. exit 0
  103.  
The problem is when asked if you want to play again, when you say yes, it kicks out of the script after printing the followig line:

Expand|Select|Wrap|Line Numbers
  1. echo " A Random word has been chosen... Enjoy the Game"
  2.  

player 2 option scripts:


Expand|Select|Wrap|Line Numbers
  1. startup
  2.  
  3. life=10
  4. word=$player2word
  5.  
Thats the only line which is different everything else is the same . The script again kicks out after it has asked you choose a word for player 2 to guess, during replay.

I created 3 function:

1- stored_words: words used for 1-player game.
2- Replay
3- Startup

so to sumerise and stop boring a hell out of you, the problems are:

1- the looping so the players can continue if they decide to play again.

2- combine both scripts into one

K
May 21 '07
17 1664
keyvanrahmadi
57 New Member
I'm not sure I understand: if you call "exit 0;" somehwere in the program it will exit immediately. Instead of calling exit, you should jump back to the beginning of the loop if choce is 'y'. Use the "continue" command, for instance.

HTH,
arne
I used continue, what it will do is contiune the script even when life is = 0

I am not even sure what i need to do next, as i have tried everything i know now.

K
May 22 '07 #11
arne
315 Recognized Expert Contributor
I used continue, what it will do is contiune the script even when life is = 0

I am not even sure what i need to do next, as i have tried everything i know now.

K
Expand|Select|Wrap|Line Numbers
  1. if [ $life -eq 0 ]; then     
  2.   echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""     
  3.   replay
  4.   continue
  5. fi
  6.  
will continue the loop. Make sure to reset 'life' to 10, to clear the template and to choose another word.

HTH,
arne
May 23 '07 #12
keyvanrahmadi
57 New Member
Expand|Select|Wrap|Line Numbers
  1. if [ $life -eq 0 ]; then     
  2.   echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""     
  3.   replay
  4.   continue
  5. fi
  6.  
will continue the loop. Make sure to reset 'life' to 10, to clear the template and to choose another word.

HTH,
arne
I have managed to set the life counter to 10, by creating a function and calling it when i need to. The only problem now is that not sure why it keeps on calling the same word over and over again when 1 player option is chosen. is there a way to clear the template?

K
May 23 '07 #13
arne
315 Recognized Expert Contributor
I have managed to set the life counter to 10, by creating a function and calling it when i need to. The only problem now is that not sure why it keeps on calling the same word over and over again when 1 player option is chosen. is there a way to clear the template?

K
What about defining a function that will select a new word randomly and set the template accordingly?

arne
May 23 '07 #14
keyvanrahmadi
57 New Member
What about defining a function that will select a new word randomly and set the template accordingly?

arne
Expand|Select|Wrap|Line Numbers
  1. stored_word()
  2. {
  3.   case $(( $$ % 10 )) in
  4.     0 ) echo "energy"    ;;  1 ) echo "touch" ;;
  5.     2 ) echo "climbing" ;;   3 ) echo "declare" ;;
  6.     4 ) echo "marry"  ;;     5 ) echo "relax"   ;;
  7.     6 ) echo "bugs"     ;;   7 ) echo "inaccessible" ;;
  8.     8 ) echo "country" ;;    9 ) echo "folder"
  9.   esac
  10. }
  11.  
  12.  
thats what the first funtion dose. I think the main problem is that once the $word is set, it remains the same through out the cycle of the program and only once you exit the game it resets to zero.

have i misunderstood you?

K
May 23 '07 #15
arne
315 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. stored_word()
  2. {
  3.   case $(( $$ % 10 )) in
  4.     0 ) echo "energy"    ;;  1 ) echo "touch" ;;
  5.     2 ) echo "climbing" ;;   3 ) echo "declare" ;;
  6.     4 ) echo "marry"  ;;     5 ) echo "relax"   ;;
  7.     6 ) echo "bugs"     ;;   7 ) echo "inaccessible" ;;
  8.     8 ) echo "country" ;;    9 ) echo "folder"
  9.   esac
  10. }
  11.  
  12.  
thats what the first funtion dose. I think the main problem is that once the $word is set, it remains the same through out the cycle of the program and only once you exit the game it resets to zero.

have i misunderstood you?

K

Yes, but this function (or another one) may also set the template, since if you run out of lifes and you want to play again, your program will reuse the template with the already guessed letters in it ...

arne
May 23 '07 #16
keyvanrahmadi
57 New Member
Yes, but this function (or another one) may also set the template, since if you run out of lifes and you want to play again, your program will reuse the template with the already guessed letters in it ...

arne
Ok i am officially raising my hand and putting a sign up saying i am too stupid and thick.. sorry :( i dont understand

K
May 23 '07 #17
arne
315 Recognized Expert Contributor
Ok i am officially raising my hand and putting a sign up saying i am too stupid and thick.. sorry :( i dont understand

K
Ok, the problem is that the continue jumps to the beginning of the inner loop. What we want is to jump to the beginning of the outer loop. So, try

Expand|Select|Wrap|Line Numbers
  1.      flag=0
  2.      if [ $life -eq 0 ]; then
  3.  
  4.      echo "THE WORD YOU WERE TRYING TO GUESS WAS \"$word\""
  5.  
  6.               replay
  7.           flag=1
  8.           break
  9.      fi
  10.  
and

Expand|Select|Wrap|Line Numbers
  1.      if [ $flag -eq 1 ]; then
  2.        continue;
  3.      fi
  4.  
  5.      echo -n "Congratulations! You guessed \"$word\" in \"$guesses\" guesses"
  6.  
I added some context so that you can see where to insert the changes.

BTW, try this stored_word function. If you use $$, i.e. the process PID, you will always choose the ssame word for a second game

Expand|Select|Wrap|Line Numbers
  1. stored_word()
  2.  {
  3.      number=$RANDOM
  4.      let "number %= 10"
  5.      case $number in     
  6.      0 ) echo "energy"    ;;  1 ) echo "touch" ;;     
  7.      2 ) echo "climbing" ;;   3 ) echo "declare" ;;     
  8.      4 ) echo "marry"  ;;     5 ) echo "relax"   ;;     
  9.      6 ) echo "bugs"     ;;   7 ) echo "inaccessible" ;;     
  10.      8 ) echo "country" ;;    9 ) echo "folder"         
  11.      esac     
  12.  }
  13.  
HTH,
arne
May 23 '07 #18

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

Similar topics

7
2070
by: Jeff Kish | last post by:
Greetings. I can't invest a large amount of time into this, but it would be very helpful if I could do this. I have a directory full of xml files I'd like to be able to query to find out things like: find all elements of type "<table" that contain as subelements (elements of type "row" that have attributes of type "lookup" which have value
11
2999
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
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
3954
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.
22
1506
by: sri | last post by:
Hi I am new to C language. Still I am using Turbo C++ 3 Compiler. Any New version is available for Windows XP platforms. Borland releases C++ 5.5 Command line tools. Is There any New C Compiler software is available..... Thanks for the maintaining the C- Gorup.
3
2333
by: gham | last post by:
I am a complete noob to linux and shell scripting please help this is what I am trying to do 1. Create a script that takes 1 argument being a file, read the inputted file, and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line efforts sofar #!/bin/bash echo "filename"
4
3582
by: melmack3 | last post by:
Hello My PHP script executes many bash/cmd commands. Functions like "exec()" or "system()" cause that new bash/cmd session is started, the command is executed and the session is closed. Unfortunately it is very slow process so I would like to increase performance and open one bash/cmd session on the begin of my script and execute the commands such as in normal system opened bash/cmd window and close it
4
1464
by: hgdien | last post by:
I am a complete newbie at Perl. I have just started getting from bash to Perl language. I am trying to do a simple perl scripting that takes three arguments: two integers and an operation (+, -, * or /) and outputs the computed value. Example: $ ./perl_calc.pl 12 -13 * -156 $ However, I don`t know how to turn that operation variable (+, -, * or /) back to an operation only, since when I put in $ARGV $ARGV $ARGV they are all treated either...
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
9603
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
10643
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...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
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
7664
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
6881
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
5550
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...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.