Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bash shell script question (what's wrong in this script?)

64 views
Skip to first unread message

The Derfer

unread,
May 14, 2012, 10:27:10 PM5/14/12
to
This is a script I wrote in Oracle Enterprise Linux 5.

This script yields the errors that follow:

#!/bin/bash
CWD=/directory
cd $CWD
for filename in `ls *.zip` do
s=$(ls -lah $filename | awk '{ print $5}');
if [ $s -eq 0 ] ; then
echo "File $filename has zero file size in $CWD" | mailx -s " ERROR:
zero size file found" x...@yz.com
fi
done


bash-3.2$ ./check2.sh
./check2.sh: line 5: syntax error near unexpected token `s=$(ls -lah
$filename | awk '{ print $5}')'
./check2.sh: line 5: `s=$(ls -lah $filename | awk '{ print $5}');'


Don't understand why it errors out on that particular line.
If I do this on a single command line, there's no problem with the
syntax of the
"s=" line:
bash-3.2$ s=$(ls -lah $filename | awk '{ print $5}'); echo $s


Ideas?

Thanks in advance.

Barry Margolin

unread,
May 15, 2012, 12:47:17 AM5/15/12
to
In article
<3e6abacd-6955-4f1f...@b26g2000vbt.googlegroups.com>,
The Derfer <der...@gmail.com> wrote:

> This is a script I wrote in Oracle Enterprise Linux 5.
>
> This script yields the errors that follow:
>
> #!/bin/bash
> CWD=/directory
> cd $CWD
> for filename in `ls *.zip` do

You need a semicolon or newline before "do". Also, there's no reason to
use "ls", you can just write:

for filename in *.zip; do

> s=$(ls -lah $filename | awk '{ print $5}');
> if [ $s -eq 0 ] ; then
> echo "File $filename has zero file size in $CWD" | mailx -s " ERROR:
> zero size file found" x...@yz.com
> fi
> done
>
>
> bash-3.2$ ./check2.sh
> ./check2.sh: line 5: syntax error near unexpected token `s=$(ls -lah
> $filename | awk '{ print $5}')'
> ./check2.sh: line 5: `s=$(ls -lah $filename | awk '{ print $5}');'
>
>
> Don't understand why it errors out on that particular line.

It's expecting "do" on the line after "for", but instead it got s=$(...).

> If I do this on a single command line, there's no problem with the
> syntax of the
> "s=" line:
> bash-3.2$ s=$(ls -lah $filename | awk '{ print $5}'); echo $s
>
>
> Ideas?
>
> Thanks in advance.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

DennisW

unread,
May 15, 2012, 11:29:35 AM5/15/12
to
Don't parse ls, use stat to get the size of the file. See
http://mywiki.wooledge.org/ParsingLs

Quote your variables. See http://mywiki.wooledge.org/Quotes

Ralf Goertz

unread,
May 15, 2012, 12:30:51 PM5/15/12
to
The Derfer wrote:

> This is a script I wrote in Oracle Enterprise Linux 5.
>
> This script yields the errors that follow:
>
> #!/bin/bash
> CWD=/directory
> cd $CWD
> for filename in `ls *.zip` do
> s=$(ls -lah $filename | awk '{ print $5}');
> if [ $s -eq 0 ] ; then
> echo "File $filename has zero file size in $CWD" | mailx -s " ERROR: zero size file found" x...@yz.com
> fi
> done

Why don't you use "find"?

untested:

find $CWD -maxdepth 1 -name \*.zip -size 0 -exec printf "File %s has zero file size in $CWD\\n" {} \; | mailx -s " ERROR: zero
size file found" x...@yz.com

Note that this gives just one mail for all empty files.

DennisW

unread,
May 15, 2012, 2:02:28 PM5/15/12
to
On May 14, 9:27 pm, The Derfer <derf...@gmail.com> wrote:
#!/bin/bash
dir=/directory
cd $dir
for filename in *.zip
do
if [[ ! -s $filename ]]
echo "File $filename has zero file size in $CWD"
fi
done | mailx -s " ERROR: zero size file(s) found" x...@yz.com
0 new messages