• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

Almost-as-easy: How to test for the existence of a filename within a list.

Almost as easy, but still very easy (i think) --

This is the same example i gave from "Easy-Peasy", but there's an aspect that i still need answered:

: > FileList
find ./ -name XXX.YYY |
while read "b"
        do
        c=`dirname "$b"`
        d=`sed -n '/"$c"/p' FileList`
        if [ "$d" = "$c" ]
                then
                        continue
                else
                        echo "$c" >> List
                fi
        done

What i'm interested in is taking that sed command, that defines the "d", and then testing to see if it exists within a given file (or stream).    I then want to build case- or if-loops off of that.

How do i do it?  I was under the impression here that sed'd be printing any line that already had the $c pattern in it, and then the script would continue;  whereas if the line didn't exist within the FileList then it'd get echoed into the list and continue.

Script didn't work out that way, though.  So i also tried this:

if [ "$d" = "" ]
    then
            echo "$c" >> List
    else
            continue
fi

So i'm stymied;  how do i work out this "test" command?
0
kyle_in_taiwan
Asked:
kyle_in_taiwan
  • 4
  • 2
1 Solution
 
kyle_in_taiwanAuthor Commented:
And, oh, yeah -- a typo's been introduced since i transferred it to these boards.  The second "List" originally read "FileList".  That's just an editing mistake.  Sorry.
0
 
ahoffmannCommented:
> d=`sed -n '/"$c"/p' FileList`
i guess you want to write

  d=`sed -n /"$c"/p FileList`
0
 
kyle_in_taiwanAuthor Commented:
Hm.  I tried that, and got this error (for each line in the first set of directories):

sed: -e expression #1, char 5: extra characters after command

And that then turned to this (which has a couple of characters truncated from the beginning):

sed: can't find label for jump to `QAnnReturn/p'
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kyle_in_taiwanAuthor Commented:
My understanding is that the regex portions of sed scripts need to be escaped with single quotes so they can be properly interpreted by the shell;  but perhaps there are different ways of finagling the shell profile?  

0
 
TintinCommented:
I assume this is just a learning exercise as I can make any sense of why you'd want to do this (whatever it maybe achieving) with this script.

Part of the problem is the sed delimiter

Say you have XXX.YYY sitting in a subdirectory foobar

find ./ -name XXX.YYY

will output

./foobar/XXX.YYY

doing a dirname on that will result in

./foobar

which when you use in sed, comes out as

sed /./foobar/p

You need to change the sed delimiter to not clash with /'s, eg:

sed "#$c#p"

However, why use this script (unless it's purely for learning), when you simply could do

find ./ -name XXX.YYY -exec dirname {} >>FileList \;

0
 
ahoffmannCommented:
> sed: -e expression #1, char 5: extra characters after command
sounds like your $c contains spaces or other meta characters, try

d=`sed -n '/'"$c"'/p' FileList`
0
 
kyle_in_taiwanAuthor Commented:
Heh.

Yeah, i already said on another thread that this is a learning exercise;  i'm *very* new to playing around with this stuff.

Thanks for the heads-up on the find command; you're right, and i'm slapping myself in the head for not thinking about it before (never having used the -exec flag before, it's a light slap).

Thanks both.  I'll get back to this later this evening.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now