Unix Shell - Check input

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2005 > Check input





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Check input
Diane

2005-10-24, 3:45 pm

Does anyone know how can I check in a shell script that my input file
was the correct one? Thanks!

Ian Wilson

2005-10-24, 3:45 pm

Diane wrote:
> Does anyone know how can I check in a shell script that my input file
> was the correct one? Thanks!
>


The answer is yes.

Now please tell us what the question is!

Start by reading this:
http://www.catb.org/~esr/faqs/smart-questions.html

Especially:
http://www.catb.org/~esr/faqs/smart....html#beprecise
Diane

2005-10-24, 3:45 pm

I would like my input to be always a file with the extension .hdr for
example. How can I check that my input is e.g. testing.hdr and not
myfile.drt?

Eric Moors

2005-10-24, 3:45 pm

Diane wrote:

> I would like my input to be always a file with the extension .hdr for
> example. How can I check that my input is e.g. testing.hdr and not
> myfile.drt?


You can use the case construct

case $input in
*.hdr) echo "$input ends in .hdr"
;;
*) echo "$input has no .hdr extension"
;;
esac

But why you think you need this I don't know.
It brings you nothing with respect to being certain that
the file contents are as you expect.

Eric
Diane

2005-10-24, 3:45 pm

Do you think that it would be better if I checked the file contents in
a similar way?

Loki Harfagr

2005-10-24, 3:45 pm

Le Mon, 24 Oct 2005 06:27:13 -0700, Diane a écrit_:

> I would like my input to be always a file with the extension .hdr for
> example. How can I check that my input is e.g. testing.hdr and not
> myfile.drt?


Many ways to achive that...
A simple one could be to compare the basename of the file
(no ext) with the basename of the file (and ext).

basename ${file}
basename ${file} .hdr

if they are equal you lose :-)

Or, if you know which file you want :

$ [[ $(basename ${_wantedfile}) == $(basename ${_inputfile}) ]] && echo "WIN" || echo "LOSE"

(mind the spacing for " == " !
Ian Wilson

2005-10-24, 3:45 pm

Diane wrote:
> Do you think that it would be better if I checked the file contents in
> a similar way?
>


Diane,

You really should post some context, as a google groups user you may be
unaware that newsgroups existed long before google groups, most people
read and reply without using google groups. Older articles can expire
and may not be available to go back to, new postings may show up in some
parts of the world earlier than the previous postings. So its helpful to
quote a bit of what you are talking about

http://groups.google.com/googlegrou...ting_style.html

In google groups, ignore the "reply" link at the bottom, use the "show
options" link at the top and choose the "reply" link that is then shown.
Add your reply at the bottom.

And to answer your question: yes you should validate the contents of
your input file.

If you need more detailed help then you should provide more detailed
information about the file contents and what you are trying to do with it.

Loki Harfagr

2005-10-24, 3:45 pm

Le Mon, 24 Oct 2005 06:42:45 -0700, Diane a écrit_:

> Do you think that it would be better if I checked the file contents in
> a similar way?


If you have a signature of the wanted file you could check
that the re-issueing of the signature on the working file
correspond. Use md5sum or whatever but even a datestamp/size
would be better than just the name and ext :-)
Diane

2005-10-24, 3:45 pm

Thanks a lot!


Loki Harfagr wrote:

> Le Mon, 24 Oct 2005 06:42:45 -0700, Diane a =E9crit :
>
>
> If you have a signature of the wanted file you could check
> that the re-issueing of the signature on the working file
> correspond. Use md5sum or whatever but even a datestamp/size
> would be better than just the name and ext :-)


Enrique Perez-Terron

2005-10-24, 3:45 pm

On Mon, 24 Oct 2005 15:02:57 +0200, Diane <froufrou_00@hotmail.com> wrote:

> Does anyone know how can I check in a shell script that my input file
> was the correct one? Thanks!



Diane,

what is the actual situation?

What makes some input files be "the correct one"?

Why would, for instance, "/etc/passwd" be the wrong one?

Suppose your are writing a script, and you intend to run the script like
this:

my-script < my-file.hdr


and you want to assure that the input file has a name ending in ".hdr".

In that case it is quite hard to equip you script with commands to check the
name of the input. It could be done on Linux systems, by doing

myArray=( $(ls -l /proc/$$/fd/0) )
myinput=${myArray[11]}

and then you can proceed to check the name in $myinput:

case $myinput in
*.hdr)
echo "Hooray!!";;
*)
echo "Oh, no!!";;
esac

But his could still fail if the input file has a space in the file name.

-Enrique
Diane

2005-10-25, 5:53 pm

Thank you so much Rich! Very helpful advice!


Rich Gibbs wrote:
> Diane said the following, on 10/24/05 09:02:
>
> In addition to the suggestions you've already received, you might have a
> look at the file(1) man page, especially if the files you want have a
> structured format.
>
>
> --
> Rich Gibbs
> richg74@gmail.com
> "You can observe a lot by watching." -- Yogi Berra


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com