?
Solved

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

Posted on 2006-11-21
7
Medium Priority
?
285 Views
Last Modified: 2010-05-18
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
Comment
Question by:kyle_in_taiwan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 5

Author Comment

by:kyle_in_taiwan
ID: 17987763
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17988084
> d=`sed -n '/"$c"/p' FileList`
i guess you want to write

  d=`sed -n /"$c"/p FileList`
0
 
LVL 5

Author Comment

by:kyle_in_taiwan
ID: 17988682
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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 5

Author Comment

by:kyle_in_taiwan
ID: 17988691
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
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 17990092
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17993909
> 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
 
LVL 5

Author Comment

by:kyle_in_taiwan
ID: 17994758
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question