Bash scripting: Using wildcard

bashshell-scripting

I have two different folders.

The first one contains symlbolic links named with the names of my servers (for example: udcedpai101).
The second one contains the inventory of my servers where the files are named with the server name at the beginning and ending with different paterns. (that's the reason I use the wildcard)

The inventory file name always begins with the name of the servers (for example: udcedpai101-print_manifest.txt, legpspai101-print_inventaire.txt, legpspai101.myhome.qc.ca-print_inventaire.txt). But they can ending differently.

Here's the command i'm running:

for i in `ls -l /etc/domain.conf | grep ^l | awk {'print $9'}`*; do
 ls -l "/var/opt/apache/htdocs/support/print_manifest/$i*"; 
done 

(Partial) output …

> /usr/local/coreutils/bin/ls: cannot access
> /var/opt/apache/htdocs/support/print_manifest/udcedcgi101*: No such file or directory /usr/local/coreutils/bin/ls: cannot access
> /var/opt/apache/htdocs/support/print_manifest/udcedcgi102*: No such file or directory /usr/local/coreutils/bin/ls: cannot access
> /var/opt/apache/htdocs/support/print_manifest/udcedcgi103*: No such file or directory

I'm trying to use a wildcard (*) in my command but it always returns an error stating that the file isn't there but I can view the file even though the file is there:

ls -l /var/opt/apache/htdocs/support/print_manifest/udcedcgi103*
-rw-r----- 1 TOTO TOTO 69455 Mar  9 00:00 /var/opt/apache/htdocs/support/print_manifest/udcedcgi103-print_manifest.txt

Your assistance would be much appreciated!

Best Answer

You should not be parsing the output from ls. Instead, try something like this:

for i in /etc/domain.conf/*; do
    test -L "$i" || continue  # skip if not a symlink
    ls -l "/var/opt/apache/htdocs/support/print_manifest/${i%%*/}"* 
done

The second problem is that the asterisk was in double quotes, so the shell was looking for udcedcgi101* literally, not as a wildcard to expand.

The ${i%%*/} retrieves just the base name of the file, because the loop now iterates over full path names instead of relative paths within /etc/domain.conf/.