Odd Bash script inconsistancy...
Posted: Fri 22 Jan 2010, 19:06
I wrote a little script to add up the size of files selected by wildcard argument.
The lines of code work fine run individually in Xterm.
But in the script it only gets the first file, not the whole wildcard list.
The bad lines are "ls" or "du", and both give only the first line in the script.
If you run either line in Xterm it gives all the matching files and sizes.
The lines of code work fine run individually in Xterm.
But in the script it only gets the first file, not the whole wildcard list.
Code: Select all
#!/bin/sh
##### Get total size of files by wildcard argument.
if [ -z "$1" ];then exit ;fi
FILES=`du $1`
#FILES=`ls -1l $1 | awk '{print $5, $8}'`
echo '### FILES'
echo "$FILES"
SIZES=`echo "$FILES" | awk '{print $1}'`
echo '### SIZES'
echo "$SIZES"
ADD=`echo $SIZES | sed 's/ / + /g'`
echo '### $ADD'
echo "$ADD"
echo '============================================='
echo "$FILES"
echo '================================'
echo `expr $ADD`' Total of File Sizes.'
If you run either line in Xterm it gives all the matching files and sizes.