Showing posts with label functions in shell scripts. Show all posts
Showing posts with label functions in shell scripts. Show all posts

Tuesday 28 July 2020

Shell script to find the total size of the files given as positional parameters using function


PROGRAM
#size function to find the total size of the files
size()
{
  s=0
  for i in $*
  do
    if test -f $i
    then
       sz=`ls -l $i|tr -s " "|cut -d " " -f5`
       s=`expr $s + $sz`
    else
       echo $i is not a file
    fi
  done
  return $s
}
size $*
ret=$?
echo "Total size is " $s



OUTPUT
sh sizFunc.sh fibt file fil2
fil2 is not a file
Total size is  159