0

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!

Paul Haldane
  • 4,517
  • 1
  • 21
  • 32
Stoupsi
  • 1
  • 1
  • 3
  • 2
    What's the 9th value you're expecting? can you please display the output of `ls -l /etc/domain.conf | grep ^l`? Thanks. – Itai Ganot Mar 11 '15 at 16:56
  • lrwxr-xr-x 1 root sys 3 Jul 25 2014 udcedcgi101 -> dev/ lrwxr-xr-x 1 root sys 3 Jul 25 2014 udcedcgi102 -> dev/ lrwxr-xr-x 1 root sys 3 Jul 25 2014 udcedcgi103 -> dev/ lrwxr-xr-x 1 root sys 3 Jul 25 2014 udcedcgi104 -> dev/ lrwxr-xr-x 1 root sys 3 Jul 24 2014 udcedcgi105 -> dev/ – Stoupsi Mar 11 '15 at 17:05
  • the can actually end with HOSTNAME-print_manifest.txt, or HOSTNAME-print_inventory.txt. sometime, it could apen the hostname get the FQDN. We do not succeed but thanks for your time. Any idea anybody else? – Stoupsi Mar 11 '15 at 18:51

2 Answers2

1

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

tripleee
  • 1,416
  • 3
  • 15
  • 24
0

You need to move the second quote in the ls within the loop to before the *

Change

ls -l "/var/opt/apache/htdocs/support/print_manifest/$i*"; 

to

ls -l "/var/opt/apache/htdocs/support/print_manifest/$i"*; 

You probably don't need the quotes at all. Having the * inside the double quotes stops it being expanded.

Paul Haldane
  • 4,517
  • 1
  • 21
  • 32
  • While removing quotes appears to be safe in this individual case, missing quotes is one of the major sources of confusion and bugs in shell scripts. What if you change one of the paths in the future so that it does require quoting? – tripleee Dec 15 '19 at 07:17