Object matching with Wildcards


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Object matching with Wildcards
# 1  
Old 06-21-2012
Object matching with Wildcards

Greetings,

I am facing difficulty in pattern matching when I am trying to identify an object:

Below is the code

Code:
 
if [[ -f $XFER/${FILE}20120621??????_sas.sig ]]
then 
print "its there" 
else
print "its not there"
fi

Somehow this code is going to the place "its not there", even if the file exists. If i do this:

Code:
 
if [[ -f $XFER/${FILE}20120621012402_sas.sig ]]
then 
print "its there" 
else
print "its not there"
fi

Then the code works fine. I guess the problem lies in '??????'


Any help is appreciated!
# 2  
Old 06-21-2012
Try the following command:
Code:
echo "$XFER/${FILE}"20120621??????_sas.sig

What you see as a result is what the shell will use to replace $XFER/${FILE}20120621??????_sas.sig with, before it runs the -f test
# 3  
Old 06-21-2012
I think I didnt got the solution here.

what I tried is:
Code:
 
 
if [[ -f `echo "$XFER/${FILE}"20120621??????_sas.sig` ]]
 then
 print "its there"
 else
 print "its not there"
 fi

But its still not doing what it is suppose to do.
# 4  
Old 06-21-2012
It was not meant as a solution, but to show you what would happen...

It would only work with single brackets, and only if there are zero or one files. With two or more files the test will fail. This is because the shell expands the wildcards (?) first:

Code:
touch abc123456 
$ if [ -f abc?????? ]; then  echo hello; fi
hello
$ touch abc123456 abc123421 abc765432
$ if [ -f abc?????? ]; then  echo hello; fi
-bash: [: too many arguments

If we use set -x, it becomes clear why:
Code:
$ if [ -f abc?????? ]; then  echo hello; fi
+ '[' -f abc123421 abc123456 abc765432 ']'
-bash: [: too many arguments
$

within double bracket the wildcards do not get expanded so the test will always fail unless a file with the literal name exists:
Code:
$ touch 'abc??????'
$ if [[ -f abc?????? ]]; then  echo hello; fi
hello
$

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-21-2012
This makes perfect sense!!!

Thank-you Scrutinizer!!
# 6  
Old 06-21-2012
Your welcome! If you want to test if any file with that pattern is present you could try something like this, which was written by cfajohnson:
Code:
file_exists()
{
  for _file; do
    [ -f "$_file" ] && return
  done
  return 1
}

Code:
$ if file_exists abc??????; then echo hello; fi
hello
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to initialize an object with another object of different class?

How to initialize an object of class say "A", with an object of type say "B". The following code give the error message "error: conversion from âAâ to non-scalar type âBâ requested" #include <iostream> using namespace std; class B; class A{ public: A() { cout <<"\nA()" << endl; } ... (1 Reply)
Discussion started by: techmonk
1 Replies

2. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

3. Shell Programming and Scripting

Grep wildcards

Hi all I want to search for number in file presented with wildcard as shown below. cat file.txt 1405 1623 1415 ....... ....... How to search for the number 141526 for example? If the number exist print "Number 141526 exist" if no, print "The number not exist" Thank you in advance. (3 Replies)
Discussion started by: vasil
3 Replies

4. 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

5. UNIX for Dummies Questions & Answers

SED and wildcards

I am using this code to locate and modify one particular ID in a file containing thousands of entries sed 's/^>OldID/>NewID/g' Infile > Outfile How can I modify the code so I can rename all old IDs to a new unique ID? I tried this sed 's/^>*/>NewID/g' Infile > Outfile but it did not... (10 Replies)
Discussion started by: Xterra
10 Replies

6. UNIX for Dummies Questions & Answers

Object reference not set to an instance of an object

I am new to PHP and UNIX. I am using Apache to do my testing on a Windows Vista machine. I am getting this error when I am trying to connect to a web service. I did a search and did not see any posts that pertain to this. Here is my function: <?php function TRECSend($a, $b, $c, $d,... (0 Replies)
Discussion started by: EddiRae
0 Replies

7. 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

8. 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

9. 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

10. UNIX for Dummies Questions & Answers

Wildcards in VI

I'm trying to delete lines from a large text file using VI. Every line that I am wanting to delete start with 'S' - all others do not. (A list of users) I've tried using * but doesn't seem to like it...any ideas... Doesn't have to be VI - but I'm better with VI than sed/awk. (8 Replies)
Discussion started by: peter.herlihy
8 Replies
Login or Register to Ask a Question