Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
bash script - is a argument a string or a number?
Results 1 to 6 of 6

Thread: bash script - is a argument a string or a number?

  1. #1
    Senior Member
    Join Date
    May 2001
    Posts
    472

    bash script - is a argument a string or a number?

    One of the guys I work with wants to write a bash script that will accept a argument or two. *I know to access the args as $1, $2, $N - but how can I check if that argument is a number or a string? I know bash variables are untyped, but if $1 contains a number I want to do one thing if it is a string I want to do another.

  2. #2
    Mentor
    Join Date
    Jun 2001
    Posts
    1,672

    Re: bash script - is a argument a string or a numb

    I know nothing about bash scripting, but can you use Regular Expressions in bash?

  3. #3

    Re: bash script - is a argument a string or a numb

    You could use egrep to get your regular expressions:

    Code:
    if $(echo $1 | egrep '[0-9]'); then
    (untested, but should work).

  4. #4

    Re: bash script - is a argument a string or a numb

    You could use egrep to get your regular expressions:

    Code:
    if $(echo $1 | egrep '[0-9]'); then
    (untested, but should work).
    heh, famous last words, those
    That would pass anything that had a mixture of numbers and other stuff. You could change it to

    Code:
    if $(echo $1 | egrep '^[0-9]*$'); then
    also untested - it might work. :P

  5. #5

    Re: bash script - is a argument a string or a numb

    Oops, yeah heh

    Actually, since you used *, that'll match an empty string, too. You should make it '^[0-9]+$', so that it only matches if there is at least one number. Untested, but it ought to work

  6. #6
    Senior Member
    Join Date
    May 2001
    Posts
    472

    Re: bash script - is a argument a string or a numb

    Code:
    if (echo $1 | egrep '[0-9]' > /dev/null)
    then
     *echo number
    else
     *echo NaN
    fi
    This works... *Thanks to all for the help.

Similar Threads

  1. Help with bash script
    By woodson2 in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 12-30-2010, 07:19 PM
  2. SSH and bash script
    By vwgtiturbo in forum Programming
    Replies: 7
    Last Post: 12-25-2008, 05:40 AM
  3. Bash script
    By linuxuser in forum Programming
    Replies: 1
    Last Post: 09-01-2005, 03:21 PM
  4. a script to give me the last argument !!!
    By rameshvl in forum Programming
    Replies: 9
    Last Post: 04-30-2003, 07:05 PM
  5. Help with bash script
    By tolstoy in forum Programming
    Replies: 5
    Last Post: 05-21-2002, 11:17 AM

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
  •