'cat file' but only if 'file' exist

'cat file' but only if 'file' exist

Post by William Par » Thu, 10 Jun 2004 15:26:09



I'm trying to run 'cat *.x' only if *.x files exist.  What is the
cleanest way of doing this?  I'm doing it by
    shopt -s nullglob
    for i in *.x; do cat $i; done
but this is aweful typing.

--

No, I will not fix your computer!  I'll reformat your harddisk, though.

 
 
 

'cat file' but only if 'file' exist

Post by Chris F.A. Johnso » Thu, 10 Jun 2004 15:39:28



Quote:> I'm trying to run 'cat *.x' only if *.x files exist.  What is the
> cleanest way of doing this?  I'm doing it by
>     shopt -s nullglob
>     for i in *.x; do cat $i; done
> but this is aweful typing.

set -- *.x
[ -f "$1" ] && cat -- *.x

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

'cat file' but only if 'file' exist

Post by Stephane CHAZELA » Thu, 10 Jun 2004 18:46:25


2004-06-9, 06:26(+00), William Park:

Quote:> I'm trying to run 'cat *.x' only if *.x files exist.  What is the
> cleanest way of doing this?  I'm doing it by
>     shopt -s nullglob
>     for i in *.x; do cat $i; done
> but this is aweful typing.

zsh -c 'cat ./*.x'

With bash:

shopt -s nullglob
files=(./*.x)


else
  printf >&2 '%s\n' "${0##*/}: no *.x files"
  false
fi

You could also do:

cat /dev/null ./*.x

--
Stephane

 
 
 

'cat file' but only if 'file' exist

Post by Bill Marcu » Thu, 10 Jun 2004 23:09:40


On 9 Jun 2004 06:26:09 GMT, William Park

> I'm trying to run 'cat *.x' only if *.x files exist.  What is the
> cleanest way of doing this?  I'm doing it by
>     shopt -s nullglob
>     for i in *.x; do cat $i; done
> but this is aweful typing.

cat *.x 2>/dev/null
will cat the files without producing a "No such file" error message, but
it will also ignore other errors.

--
Simulations are like miniskirts, they show a lot and hide the essentials.
                -- Hubert Kirrman

 
 
 

'cat file' but only if 'file' exist

Post by rakesh shar » Fri, 11 Jun 2004 00:15:34



> I'm trying to run 'cat *.x' only if *.x files exist.  What is the
> cleanest way of doing this?  I'm doing it by
>     shopt -s nullglob
>     for i in *.x; do cat $i; done
> but this is aweful typing.

set -- [*].x *.x
case $1$2 in
 '[*.x]*.x'):;;
 *) shift
    for i
    do
     [ -f "$i" ] && cat "$i";;
    done
esac
 
 
 

'cat file' but only if 'file' exist

Post by Stephane CHAZELA » Fri, 11 Jun 2004 01:27:03


2004-06-9, 08:15(-07), rakesh sharma:
[...]

Quote:> set -- [*].x *.x
> case $1$2 in
>  '[*.x]*.x'):;;

'[*].x*.x') ;;

Quote:>  *) shift
>     for i
>     do
>      [ -f "$i" ] && cat "$i";;

                      cat -- "$i";;

Quote:>     done
> esac

or use ./*.x

--
Stephane

 
 
 

'cat file' but only if 'file' exist

Post by William Par » Fri, 11 Jun 2004 04:10:11



> 2004-06-9, 06:26(+00), William Park:
> > I'm trying to run 'cat *.x' only if *.x files exist.  What is the
> > cleanest way of doing this?  I'm doing it by
> >     shopt -s nullglob
> >     for i in *.x; do cat $i; done
> > but this is aweful typing.
> cat /dev/null ./*.x

Nice!  Thanks Stephane.

--

No, I will not fix your computer!  I'll reformat your harddisk, though.

 
 
 

'cat file' but only if 'file' exist

Post by Chris F.A. Johnso » Fri, 11 Jun 2004 13:35:55




>> 2004-06-9, 06:26(+00), William Park:
>> > I'm trying to run 'cat *.x' only if *.x files exist.  What is the
>> > cleanest way of doing this?  I'm doing it by
>> >     shopt -s nullglob
>> >     for i in *.x; do cat $i; done
>> > but this is aweful typing.

>> cat /dev/null ./*.x

> Nice!  Thanks Stephane.

$ cat /dev/null ./*.x
cat: ./*.x: No such file or directory

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

'cat file' but only if 'file' exist

Post by William Par » Fri, 11 Jun 2004 14:44:47





> >> 2004-06-9, 06:26(+00), William Park:
> >> > I'm trying to run 'cat *.x' only if *.x files exist.  What is the
> >> > cleanest way of doing this?  I'm doing it by
> >> >     shopt -s nullglob
> >> >     for i in *.x; do cat $i; done
> >> > but this is aweful typing.

> >> cat /dev/null ./*.x

> > Nice!  Thanks Stephane.

> $ cat /dev/null ./*.x
> cat: ./*.x: No such file or directory

With 'shopt' of course. :-)

--

No, I will not fix your computer!  I'll reformat your harddisk, though.

 
 
 

'cat file' but only if 'file' exist

Post by rakesh shar » Fri, 11 Jun 2004 21:49:47



> 2004-06-9, 06:26(+00), William Park:
> > I'm trying to run 'cat *.x' only if *.x files exist.  What is the
> > cleanest way of doing this?  I'm doing it by
> >     shopt -s nullglob
> >     for i in *.x; do cat $i; done
> > but this is aweful typing.

...[snipped].....

Quote:> You could also do:

> cat /dev/null ./*.x

hi Stephane

Can you explain the rationale for the above? If some dir. ends in .x how
would it handle that? what is the function of /dev/null?

---
Rakesh.

 
 
 

'cat file' but only if 'file' exist

Post by Stephane CHAZELA » Fri, 11 Jun 2004 23:10:45


2004-06-10, 05:49(-07), rakesh sharma:
[...]

Quote:>> >     shopt -s nullglob
[...]
>> You could also do:

>> cat /dev/null ./*.x

> hi Stephane

> Can you explain the rationale for the above? If some dir. ends in .x how
> would it handle that? what is the function of /dev/null?

The above supposed the nullglob bash option was on.

This way, if there's no .x file, the command resolves to
cat /dev/null
that does nothing.

Without the /dev/null, it would resolve to:
cat
that reads stdin.

Sorry, my post was not clear.

--
Stephane

 
 
 

1. Xialog --title 'one two' works Xialog `cat file` doesn't

hi,

if i have (simplified) :   Xidialog --title 'One Two'     the shell
script works
if i put :   --title 'One Two' into a file & try
Xdialog `cat file`  the shell doesn not work
the program seems to consider the ' as a common character in the second
case

redhat 7.1,  bash
any ideas?  TIA

--

bye, leon


              Marietta, GA  30064    home 770/422-9355 www.compuwork.com

2. Jim Frost's Socket Tutorial

3. Even 'cat file >/dev/lp0' doesn't work

4. Will OpenBSD be ported to Intel Itanium?

5. Coverting '.so' files to '.o' files

6. what static routes are automtically added? and routed.

7. File size of 'tmp' files for 'vi'

8. Reading the Kernal?

9. "rm: can't unlink 'files'",can't delete files

10. Existing file can't be seen with 'ls'

11. mp3 'cat' and 'split' commands

12. How to do 'for line in `cat file`; do' in bourne shell?