Tech Support > Operating Systems > Linux / Variants > Problem with bash linux
Problem with bash linux
Posted by Macigno on October 17th, 2003


I need help:
i have a shell that works properly on Unix Aix. I have to make it work
on Linux Red Hat Server.
My problem is:

if (-f ./*.OK) then .....

But the line (-f ./*.OK) doesn't work. I receive the error "?too many
arguments". If i write (-f ./???.OK) which would be good for me) i
receive the error "?binary operator expected".
Thank you for your help.

Posted by Lyle H. Gray on October 17th, 2003


dcollina@dianoema.it (Macigno) wrote in news:73686a49.0310170436.39eb77b9
@posting.google.com:

Try

if [ -f ./*.OK ]; then


Posted by Eric Moors on October 17th, 2003


Use the same type of shell
does AIX have a ksh? if so install on for linux.
The linux default is bash

I assume you mean [] instead of ()
(I don't recognize the syntax you used)

set -x;if [ -f ./*.OK ];then echo "glob ok"
else echo "unexpected file glob";fi;set +x

Also use the set -x/set+x around the script to see what
the glob turns up with. Then find out why it isn't what you expected.

Eric

Posted by Chris F.A. Johnson on October 17th, 2003


On Fri, 17 Oct 2003 at 12:36 GMT, Macigno wrote:
What shell were you using on AIX? That is not the correct syntax
for any shell I know of.

To write shell scripts that can be run on different platforms and
even with different shells, one should use Bourne shell syntax,
with POSIX extensions when appropriate.

The line you show above looks more like csh syntax, but that shell
and its derivatives are very poor for writing scripts[1].

The Bourne syntax (bash is a Bourne-type shell) for the above line
would be:

if [ -f "FILE" ]
then
: .....
else
: .....
fi

But that will not work with a wildcard expression if there's more
than one matching file.

If you need to check for the existence of a file using a wildcard,
put the test in a function:

is_file() {
[ -f "$1" ] || false
}

Then you can use:

if is_file ./*.OK
then
: .....
else
: .....
fi


= = = = =
[1] <http://www.grymoire.com/Unix/CshTop10.txt>
<http://www.grymoire.com/Unix/Csh.html#uh-0>
<http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>

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

Posted by William Park on October 17th, 2003


Macigno <dcollina@dianoema.it> wrote:
Wrong syntax for Bourne shells (ie. Bash, Sh, Ash, Ksh). Also, you'll
have problem when there are more than 1 file. Try something like
if ls *.OK > /dev/null; then ...; fi

--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution for data management and processing.

Posted by Bill Unruh on October 17th, 2003


William Park <opengeometry@yahoo.ca> writes:

]Macigno <dcollina@dianoema.it> wrote:
]> I need help:
]> i have a shell that works properly on Unix Aix. I have to make it work
]> on Linux Red Hat Server.
]> My problem is:
]>
]> if (-f ./*.OK) then .....
]>
]> But the line (-f ./*.OK) doesn't work. I receive the error "?too many
]> arguments". If i write (-f ./???.OK) which would be good for me) i
]> receive the error "?binary operator expected".
]> Thank you for your help.

]Wrong syntax for Bourne shells (ie. Bash, Sh, Ash, Ksh). Also, you'll
]have problem when there are more than 1 file. Try something like
] if ls *.OK > /dev/null; then ...; fi

Yes, completely wrong syntax. To test things, use [ tests ] (remember
teh spaces befor and after the [ ]-- they are commands which need to be
separated from other commands by spaces. [ is a command --/usr/bin/[
which is an alias for test.
-f is the option, and then you give test the arguments.
But as he states, you want to give test only one filename as an
argument, not more than one, or less than one.

if is a command which tests the return code of the next command and is
true if that command suceeds, (returns 0) and flase otherwise.

Posted by Macigno on October 20th, 2003


unruh@string.physics.ubc.ca (Bill Unruh) wrote in message news:<bmpi9v$3nt$1@string.physics.ubc.ca>...
I'm sorry, i made a mistake writing my shell. Now i write it again:

if [ -f ./*.OK ]
then
rm -f filecup
rm -f lista.OK
rm -f listafile
ls -tr *.OK > lista.OK
cut -c1-6 lista.OK > listafile
for n in `cat listafile`
do
if [ -s ./$n.RCV ]
then
cat ./$n.RCV >> filecup
rm -f ./$n.RCV
rm -f ./$n.OK
fi
done
fi

i receive the error "?too many arguments". I will try your suggestions..
Thank you

Posted by Floyd Davidson on October 20th, 2003


dcollina@dianoema.it (Macigno) wrote:
The "[ -f file ]" command you are using results in multiple arguments
if the * expands to more than one file.

You probaby would be better off using a for loop, as in

for i in ./*.OK
do
...
done

If you need the data from any of the files, just uncomment the
appropriate line, otherwise just delete it.

#!/bin/bash

#rm -f filecup
#rm -f lista.OK
#rm -f listafile

for i in ./*.OK ; do
# echo "${i}" >> lista.OK
i=${i:0:6}
# echo "${i}" >> listafile
if [ -s ./${i}. RCV ]; then
# cat ./${i}.RCV >> filecup
rm -f ./${i}.RCV ./${i}.OK
fi
done

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com

Posted by bill davidsen on November 20th, 2003


In article <73686a49.0310200312.345b2cf2@posting.google.com>,
Macigno <dcollina@dianoema.it> wrote:
| unruh@string.physics.ubc.ca (Bill Unruh) wrote in message
| news:<bmpi9v$3nt$1@string.physics.ubc.ca>...

| I'm sorry, i made a mistake writing my shell. Now i write it again:
|
| if [ -f ./*.OK ]

This is bogus, it isn't garanteed to result in exactly one name. There
are several possible ways to do this, I might be tempted to try
something like
if [ -n "$(find . -maxdepth 1 -type f -name '*.OK')" ]
I just typed that, but it's close to right. Get the list of files in the
current directory and see if the string is non-zero.

| then
| rm -f filecup
| rm -f lista.OK
| rm -f listafile
| ls -tr *.OK > lista.OK
| cut -c1-6 lista.OK > listafile
| for n in `cat listafile`
| do
| if [ -s ./$n.RCV ]
| then
| cat ./$n.RCV >> filecup
| rm -f ./$n.RCV
| rm -f ./$n.OK
| fi
| done
| fi
|
| i receive the error "?too many arguments". I will try your suggestions..
| Thank you



Similar Posts