LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2006, 09:27 AM   #16
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Cool


Code:
 (IFS=" ";id|xargs -n1)
uid=53828(701567017)
gid=10545(mkgroup-l-d)
groups=0(root),544(Administrators),545(Users),10545(mkgroup-l-d)
 
Old 10-19-2006, 10:12 AM   #17
tizedboy
LQ Newbie
 
Registered: Oct 2006
Location: Germany
Distribution: Suse 10.1
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,

Quote:
Originally Posted by druuna
Hi,

Here are 2 ways to solve this problem.

1) Use grep and the exit status to determine if it's a number:
Code:
#!/bin/bash

echo -n "Give number (1-40) : "
read input

echo $input | grep "[0-9][0-9]*" > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
  echo "Use a number please"
fi
I tried this solution and it works excellent, BUT one problem. If a user types a number and a letter eg. 2a or w4, the script can not yet hadle this. How should I add another "checker" to prevent this?
 
Old 10-19-2006, 10:44 AM   #18
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

That's a minor slip on my side, you just need to add 2 characters to the script.

This: "[0-9][0-9]*"
Should be: "^[0-9][0-9]*$"

The last forces it to be a number (must start (^) with a number [0-9], followed by 0 or more numbers ([0-9]*) at the end of the line ($).

Sorry about the slip-up.
 
Old 10-20-2006, 07:10 AM   #19
tizedboy
LQ Newbie
 
Registered: Oct 2006
Location: Germany
Distribution: Suse 10.1
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,
I integrated your solutions to the the script and I guess it is ready. Could you perhaps try to find something that is missing?

------------------------------------------------------------------------------------------------------------------------------
# Guessing game
# !/bin/bash
clear
times=0# Guessing game
# !/bin/bash
clear
times=0
guess=$[$RANDOM * 40 / 32767]
try=0

echo "You have 10 tries ..... good luck! "
echo
while [ $times -ne $guess ]
do
echo
echo -n "Please enter a number between 1 and 40 : "
read input
try=$[$try+1]
if [ $try = 10 ]
then
echo "You have exhausted all tries"
echo
exit
else

echo $input | grep "^[1-9][0-9]*$" > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
echo "Please only integers (1-40) "
else

if [ $input -eq $guess ]
then
echo "Congratulation "
echo
exit
else

if [ $input -lt $guess ]
then
echo "Too small "
else
echo "Too big "
fi
fi
fihttp://search.yahoo.com/search?p=intergrate&prssweb=Search&ei=UTF-8&fr=yfp-t-501&x=wrt
fi
done
guess=$[$RANDOM * 40 / 32767]
try=0

echo "You have 10 tries ..... good luck! "
echo
while [ $times -ne $guess ]
do
echo
echo -n "Please enter a number between 1 and 40 : "
read input
try=$[$try+1]
if [ $try = 10 ]
then
echo "You have exhausted all tries"
echo
exit
else

echo $input | grep "^[1-9][0-9]*$" > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
echo "Please only integers (1-40) "
else

if [ $input -eq $guess ]
then
echo "Congratulation "
echo
exit
else

if [ $input -lt $guess ]
then
echo "Too small "
else
echo "Too big "
fi
fi
fi
fi
done
-------------------------------------------------------------------------------------------------------------

Last edited by tizedboy; 10-20-2006 at 07:11 AM.
 
Old 10-20-2006, 08:39 AM   #20
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The above code (the relevant part of it) works. But why the 'double' bash code and the URL?

You did not use the 'code' tags in your post, so the following could be done by you but removed by the lack of code tags:
It is common practise to indent code for better readability and easier debugging (true for most, if not all programming languages).

After removing the extra/unneeded stuff and some indenting the 'final' code looks like this:
Code:
# !/bin/bash
clear
times=0
guess=$[$RANDOM * 40 / 32767]
try=0

echo "You have 10 tries ..... good luck! "
echo
while [ $times -ne $guess ]
do
  echo
  echo -n "Please enter a number between 1 and 40 : "
  read input
  try=$[$try+1]
  if [ $try = 10 ]
  then
    echo "You have exhausted all tries"
    echo
    exit 0
  else
    echo $input | grep "^[1-9][0-9]*$" > /dev/null 2>&1
    if [ "$?" -ne 0 ]
    then
      echo "Please only integers (1-40) "
    else
      if [ $input -eq $guess ]
      then
        echo "Congratulation "
        echo
        exit 0
      else
        if [ $input -lt $guess ]
        then
          echo "Too small "
        else
          echo "Too big "
        fi
      fi
    fi
  fi
done
If things get more complicated it's also a good idea to add some comments to explain what the script does and/or why you did it a certain way.
 
Old 10-20-2006, 07:15 PM   #21
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
You might like to know about functions:

Code:
#!/bin/bash

function cando
{
  echo cando
  return 0
}

cando

if [ $? -ne "0" ]; then  echo this will never happen; exit 1; fi

function candomore
{
  echo function arguments = $*
}

candomore and more and more
Best wishes, Meinbein

Last edited by Tischbein; 10-20-2006 at 08:14 PM.
 
Old 10-20-2006, 08:10 PM   #22
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
After "please only integers" slap a "continue". This will start the next loop, skipping the rest of the current loop which contains only irrelevant tests. "break" breaks out of the loop altogether, but you don't need that.


Regards, Hinkelbein
 
Old 10-21-2006, 11:27 AM   #23
tizedboy
LQ Newbie
 
Registered: Oct 2006
Location: Germany
Distribution: Suse 10.1
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,

thanks for your advice. Actually I am suprised the way my post came out. As I am new here I dodnt know there was an option of to format a post to look good. The error in my post I guess was due to the fact that I pasted from Linux enviroment. Anyway lets go on.Now I know
Quote:
Originally Posted by druuna
Hi,

The above code (the relevant part of it) works. But why the 'double' bash code and the URL?

You did not use the 'code' tags in your post, so the following could be done by you but removed by the lack of code tags:
It is common practise to indent code for better readability and easier debugging (true for most, if not all programming languages).


If things get more complicated it's also a good idea to add some comments to explain what the script does and/or why you did it a certain way.
 
Old 10-21-2006, 11:50 AM   #24
tizedboy
LQ Newbie
 
Registered: Oct 2006
Location: Germany
Distribution: Suse 10.1
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,
Ok now the code looks like as shown below.I commented it and I found something interesting. The user has 10 tries, that is ok. But the user doesn´t get rewarded at the last try(the tenth)even if they guessed correctly. In other words the program exits after the 10th try and doesnt inform whether there was a guess or not even if the guess was there.

Code:
# !/bin/bash
clear                         #just clear for me the screen 
times=0                       #a variable to help me check  the while loop
guess=$[$RANDOM * 40 / 32767] #my randomly generated number to  be guessed
try=0                         #this will set my counter to count number of guesses  

echo "You have 10 tries ..... good luck! "
echo
while [ $times -ne $guess ]   #while true; as long as everything is ok
do
  echo
  echo -n "Please enter a number between 1 and 40 : "
  read input
  try=$[$try+1]               #start counting number og guesses
  if [ $try = 10 ]            #if the guess was the tenth, then.....
  then
    echo "You have exhausted all tries"
    echo
    exit 0
  else
    echo $input | grep "^[1-9][0-9]*$" > /dev/null 2>&1 #check to see that only integers a used
    if [ "$?" -ne 0 ]         #if my special variable $? stored a value other than 0, it means a letter or #another character or combination of letters and characters were used. So tell the user to use 1-40       
    then
      echo "Please only integers (1-40) "
    else
      if [ $input -eq $guess ]#if the user used a number equal to that which was randomly generated
      then
        echo "Congratulation " #then the user has won
        echo
        exit 0
      else
        if [ $input -lt $guess ]#else the user used a smaller number
        then
          echo "Too small "
        else
          echo "Too big " #or the user used a greater number than the one needed
        fi
      fi
    fi
  fi
done
 
Old 10-21-2006, 12:15 PM   #25
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Quote:
Originally Posted by tizedboy
Ok now the code looks like as shown below.
Looks a lot better. Personally I like to place the comments above the command, but that's a taste issue.
It does matter where the # is placed. On certain lines there's no space between the last character of the command and the # (bash will complain and fail).

Quote:
I commented it and I found something interesting. The user has 10 tries, that is ok. But the user doesn´t get rewarded at the last try(the tenth)even if they guessed correctly. In other words the program exits after the 10th try and doesnt inform whether there was a guess or not even if the guess was there.
You can solve this by changing this:

if [ $try = 10 ] #if the guess was the tenth, then.....

by

if [ $try -gt 10 ] #if the guess was the tenth, then.....

The -gt operator stands for greater than. See bash manpage for all the possibilities (the CONDITIONAL EXPRESSIONS section).
 
Old 10-21-2006, 01:06 PM   #26
tizedboy
LQ Newbie
 
Registered: Oct 2006
Location: Germany
Distribution: Suse 10.1
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna
Hi,


Looks a lot better. Personally I like to place the comments above the command, but that's a taste issue.
It does matter where the # is placed. On certain lines there's no space between the last character of the command and the # (bash will complain and fail).


You can solve this by changing this:

if [ $try = 10 ] #if the guess was the tenth, then.....

by

if [ $try -gt 10 ] #if the guess was the tenth, then.....

The -gt operator stands for greater than. See bash manpage for all the possibilities (the CONDITIONAL EXPRESSIONS section).
What I wanted is(I know from other languages it is possible).

if [ $try -eq 10 ] AND [ $try -eq $guess]
#if the guess was the tenth(the last), and it was the correct guess then inform of this and the quit.....

Last edited by tizedboy; 10-21-2006 at 01:22 PM.
 
Old 10-21-2006, 01:27 PM   #27
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Insert the bold line shown below.

Code:
      if [ $input -eq $guess ] #if the user used a number equal to that which was randomly generated
      then
      [ $try -eq 10 ] && echo "And on the very last guess......."
        echo "Congratulation " #then the user has won
        echo
        exit 0
      else
This: [ $try -eq 10 ] && echo "And on the very last guess......." is a short way of saying:
Code:
if [ $try -eq 10 ]
then
  echo "And on the very last guess......."
fi
Only if the first part (before &&) is true, the second part is executed.
 
Old 10-21-2006, 04:55 PM   #28
tizedboy
LQ Newbie
 
Registered: Oct 2006
Location: Germany
Distribution: Suse 10.1
Posts: 13

Original Poster
Rep: Reputation: 0
Hi,

Quote:
Originally Posted by druuna
Hi,

Insert the bold line shown below.

Code:
      if [ $input -eq $guess ] #if the user used a number equal to that which was randomly generated
      then
      [ $try -eq 10 ] && echo "And on the very last guess......."
        echo "Congratulation " #then the user has won
        echo
        exit 0
      else
I applied your suggestions and the do exactly what I wanted. I discover now that the more succesful you become the more you want :-)
I now wanted to check if a user just pressed <enter> without typing anything. So far this works without adding any code, but
I just wanted to check this and I tried the following but I got errors.

Code:
#just setting my variable returnchar to store the return key
retunchar=$'\r'
     echo $eingabe | grep "[1-9][0-9]*" > /dev/null 2>&1
	if [ "$?" -ne 0 ]
           then
               #here is where I get error "integer expression expected"
               ["$?" -eq $returnchar ] && echo "you didn't type anything(you only pressed the return key"
		echo " use only numbers please "
        fi

Last edited by tizedboy; 10-21-2006 at 08:08 PM.
 
Old 10-22-2006, 05:28 AM   #29
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Try:

Code:
     #If the input is not a number this will return the empty string
     #If it is a number it will return the number
     raffiniert=$(echo $eingabe | grep '^[1-9][0-9]*$')
        # -z and -n test for empty and non-empty strings respectively
	if [ -z "$raffiniert" ]
           then
		echo " enter a number please "
        else
        # ... code as before
        fi
Regards, Hinkelbein
 
Old 10-22-2006, 05:53 AM   #30
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

As you already noticed the code catches anything that is not a number and tells you so. But that's not what you wanted to know.

The return character is (most often) not part of the string that is given to bash. An empty input is just that: empty (NULL) and the return char is not present in that string.

Bash has a few ways to check if a string has a (non-) zero length: The -z and the -n operator (there are more, see man bash , the CONDITIONAL EXPRESSIONS section). -z checks to see if a string is zero, -n checks for a non-zero string. I.e:
Code:
$ foo=""

$ [ -z $foo ] && echo "foo is not set ($foo)"
foo is not set ()

$ foo="bar"

$ [ -z $foo ] && echo "foo is not set ($foo)"

$ [ -n $foo ] && echo "foo is set ($foo)"
foo is set (bar)
I'll leave it up to you to try to implement this in your current code.

Hope this clears things up a bit.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
starting xprogram without starting X server. bruse Linux - Newbie 9 04-26-2005 07:05 PM
Auto starting and/or scripting Infernal211283 Linux - Newbie 1 03-22-2005 12:41 PM
Won't Boot - Starting Printer Service - Starting CUPS jeansond Linux - Newbie 0 10-11-2004 06:39 PM
Starting apps after starting xfce?? gtgoku Slackware 2 08-21-2004 01:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration