wildcards with if statement?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting wildcards with if statement?
# 8  
Old 03-05-2007
Quote:
Originally Posted by murtaza
Hello
i am trying to use the wildcards with the if statement but it is displaying the error like this one

if [ $1=[0-9]* | *[0-9]* | *[0-9] ]
Any body can help me to for using the wild card option in the if case but i have used this code and working well with the case statement to enter the name without the integer number checks.
I thank in advance,
bye
You cannot use wildcards with test (a.k.a. [ ); use a case statement instead:

Code:
case $1 in
   [0-9]*|*[0-9]*| *[0-9] )
       ## do something here
        ;;
   *) ## do something else here
       ;;
esac

In fact, you don't need all three tests; the second one will catch all cases:

Code:
case $1 in
   *[0-9]* )
       ## do something here
        ;;
   *) ## do something else here
       ;;
esac

(Your if statement wouldn't work in any case; there needs to be space around the "=".)
# 9  
Old 03-06-2007
Quote:
Originally Posted by Perderabo
Example:
Code:
#! /usr/bin/ksh

needval=1

while ((needval)) ; do
        read val?'Enter a number - '
        if [[ $val = ?(+|-)+([0-9]) ]] ; then
                needval=0
        else
                echo $val is not an integer
        fi
done

echo val = $val
exit 0

This is the reverse of what you want, but it's a good example. An integer is some digits possibly preceded by one minus sign or one plus sign. We loop until we get what we want.
-------------------------------------------------------------------------
Thanks for reply. When I try to run above code then it displays the following syntax error. I tried many times to correct the syntax error but could not. Furthermore I want to tell u that i am using the bash shell.
Thanks in advance

ifwildcard: line 5: syntax error in conditional expression: unexpected token `('
ifwildcard: line 5: syntax error near `?(+'
ifwildcard: line 5: ` if [[ $val = ?(+|-)+([0-9]) ]] ; then'
# 10  
Old 03-06-2007
Quote:
Originally Posted by murtaza
-------------------------------------------------------------------------
Thanks for reply. When I try to run above code then it displays the following syntax error. I tried many times to correct the syntax error but could not. Furthermore I want to tell u that i am using the bash shell.
Thanks in advance

ifwildcard: line 5: syntax error in conditional expression: unexpected token `('
ifwildcard: line 5: syntax error near `?(+'
ifwildcard: line 5: ` if [[ $val = ?(+|-)+([0-9]) ]] ; then'

It works fine for me!!!!!
Checked in bash and ksh
# 11  
Old 03-06-2007
Quote:
Originally Posted by jacoden
It works fine for me!!!!!
Checked in bash and ksh
In bash it should give the errors that murtaza mentioned. (Unless you leave the first line intact which would cause the kernel to feed the script to ksh even when launched by bash.)

in bash:
Code:
#! /usr/local/bin/bash

needval=1

while ((needval)) ; do
        echo -n 'Enter a number - '
        read val
        if [[ $val =~ ^[-+]{0,1}[0-9]{1,}$ ]] ; then
                needval=0
        else
                echo $val is not an integer
        fi
done

echo val = $val
exit 0

# 12  
Old 03-07-2007
Quote:
Originally Posted by Perderabo
In bash it should give the errors that murtaza mentioned. (Unless you leave the first line intact which would cause the kernel to feed the script to ksh even when launched by bash.)

in bash:
Code:
#! /usr/local/bin/bash

needval=1

while ((needval)) ; do
        echo -n 'Enter a number - '
        read val
        if [[ $val =~ ^[-+]{0,1}[0-9]{1,}$ ]] ; then
                needval=0
        else
                echo $val is not an integer
        fi
done

echo val = $val
exit 0

Thanks u very much. Now it is working on bash. Can u recommend me the material to understand behind this logic.

once again thanks to solve my problem.
# 13  
Old 03-07-2007
hello brother! can we use the * wildcard with if and if yes then how?
# 14  
Old 03-07-2007
Problem: using * wildcard with if statement

Hello every body:
I am trying to use the * wildcard with if statement by this way
if [[ $fullname =~ *[0-9]* ]].
Can any body help me that how can use * with in the ifstatement?
Thanks in advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. UNIX for Advanced & Expert Users

Wildcards

These 2 websites do a GREAT job of explaining different types of wildcards. I learned about the categories of characters which I never knew about at all. GNU/Linux Command-Line Tools Guide - Wildcards GREP (1 Reply)
Discussion started by: cokedude
1 Replies

3. Shell Programming and Scripting

Using wildcards in "if" statement

hey guys, what i am doing is that i would like the program to check if there was anything inputted. If nothing is imputed, it is suppose to display a message. echo -n "Enter Author:" read Author #echo -n "Enter Title:" #read Title if ] ; then echo "you enter something" else echo... (9 Replies)
Discussion started by: gregarion
9 Replies

4. Shell Programming and Scripting

wildcards in "if then" statement

Hello, I would like to use a simple "if then" test to check if an argument to a command begins with "http://" as follows: if http://* ]]; then command fi but the wildcard just seems to be ignored, ie., it will only execute the command if the expression is strictly "http://" with... (5 Replies)
Discussion started by: Allasso
5 Replies

5. Shell Programming and Scripting

wildcards in "if then" statement

Hello, I would like to use a simple "if then" test to check if an argument to a command begins with "http://" as follows: if http://* ]; then command fi but the wildcard just seems to be ignored, ie., it will only execute the command if the expression is strictly "http://" with nothing... (0 Replies)
Discussion started by: Allasso
0 Replies

6. UNIX for Dummies Questions & Answers

wildcards NOT

Hi All Please excuse another straightforward question. When creating a tar archive from a directory I am attempting to use wildcards to eliminate certain filetypes (otherwise the archive gets too large). So I am looking for something along these lines. tar -cf archive.tar * <minus all *.rst... (5 Replies)
Discussion started by: C3000
5 Replies

7. UNIX for Dummies Questions & Answers

ls with wildcards

ok, I'm trying to write a script file that lists files with specific elements in the name into a txt file, it looks like this ls s*.dat > file_names.txt can't figure out whats wrong with that line, any ideas? thanks in advance (10 Replies)
Discussion started by: benu302000
10 Replies

8. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies

9. Shell Programming and Scripting

How can I make an if [ -f ] statement with wildcards?

Normally you would have something like.. if then foo bar fi but what if you wanted to do something like if then foo bar fi How do I get Unix to accept anything that matches a pattern of FILENAME with anything after it during an in if statement? (3 Replies)
Discussion started by: LordJezo
3 Replies

10. UNIX for Dummies Questions & Answers

How can I put wildcards in an if statement that uses variables?

With the if statement: if How can I make it so it accepts a wildcard after the ${CURR_DAY_MONTH} variable? Putting a -f /webtrends/SUN/mrw2/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* won't work, right? I think I need some kind of special character so it knows the wildcard is... (3 Replies)
Discussion started by: LordJezo
3 Replies
Login or Register to Ask a Question