Hello,

I have script to gather total FS usage size. I use to list of the FS's in another notepad file and this script will read from that file. finally it will give me output of total allocated space, used space, available space and percentage of space. This script works for me in solaris, but when i am trying to run in linux i am getting error. Please advise me on this.

script:

#!/bin/sh


# Get disk metrics and outpt in csv format


DMLIST=dmlist
DF=/tmp/df$$
HOSTNAME=`hostname`
totalspace=0
totalused=0
totalavailable=0
LOGDIR=/folk/rnalluri/vobscripts/data.out
LOGFILE=${LOGDIR}/`hostname``date +%m%d%y`.disk
cd /folk/rnalluri/vobscripts
if [ ! -d $LOGDIR ]
then
echo "Making $LOGDIR"
mkdir $LOGDIR
fi
#
#Grab list for this host from our config file
#
grep "^$HOSTNAME " $DMLIST |awk '{print $2}'|xargs df -k|grep -v Filesystem>$DF


totalspacelist=`cat $DF|awk '{print $2}`
totalusedlist=`cat $DF|awk '{print $3}`
totalavailablelist=`cat $DF|awk '{print $4}`


for i in $totalspacelist
do
totalspace=`expr $totalspace + $i`
done


for i in $totalusedlist
do
totalused=`expr $totalused + $i`
done


for i in $totalavailablelist
do
totalavailable=`expr $totalavailable + $i`
done


totalspace=`expr $totalspace / 1024000`
totalused=`expr $totalused / 1024000`
totalavailable=`expr $totalavailable / 1024000`
totalpercent=`echo $totalused $totalspace|awk '{printf "%2.0f",$1/$2*100}`
echo "server,total space,used space,available space, %used" | tee $LOGFILE
echo "$HOSTNAME,${totalspace}GB,${totalused}GB,${totalavailable}GB,$totalpercent"|tee -a $LOGFILE
/bin/rm $DF

Below is the error when i tried to run in linux i am getting. I am using RHEL 4 & 5.

sh-3.00$ ./dm1.sh
./dm1.sh: command substitution: line 24: unexpected EOF while looking for matching `''
./dm1.sh: command substitution: line 25: syntax error: unexpected end of file
./dm1.sh: command substitution: line 25: unexpected EOF while looking for matching `''
./dm1.sh: command substitution: line 26: syntax error: unexpected end of file
./dm1.sh: command substitution: line 26: unexpected EOF while looking for matching `''
./dm1.sh: command substitution: line 27: syntax error: unexpected end of file
./dm1.sh: command substitution: line 46: unexpected EOF while looking for matching `''
./dm1.sh: command substitution: line 47: syntax error: unexpected end of file
server,total space,used space,available space, %used

Thanks,
raja

Hello Salem,

Thank you for quick reply. After i changed those quotes from '' " to " '' still i am getting some errors. can u pls advise me. But my previous script is working fine in solaris environment. /tmp directory have 777 permissions.

My prmiary goal with this script is i need to get FS stats for almost 50 servers and i have hostnames and file system names on different txt files. Is there a script to do this. I need to get total available space, current usage, available space and percentage for each file system mentioned in text file.

errors i am getting now

./dm1.sh: line 25: /tmp/df16376: Permission denied
./dm1.sh: command substitution: line 35: syntax error: unexpected end of file
./dm1.sh: line 30: 0: command not found
./dm1.sh: line 46: unexpected EOF while looking for matching `''
./dm1.sh: line 50: syntax error: unexpected end of file

below is the modified script .

#!/bin/sh


# Get disk metrics and outpt in csv format


DMLIST=dmlist
DF=/tmp/df$$
HOSTNAME=`hostname`
totalspace=0
totalused=0
totalavailable=0
LOGDIR=/folk/rnalluri/vobscripts/data.out
LOGFILE=${LOGDIR}/`hostname``date +%m%d%y`.disk
cd /folk/rnalluri/vobscripts
if [ ! -d $LOGDIR ]
then
echo "Making $LOGDIR"
mkdir $LOGDIR
fi
#
#Grab list for this host from our config file
#
grep "^$HOSTNAME " $DMLIST |awk '{print $2}'|xargs df -k|grep -v Filesystem>$DF


totalspacelist=`cat $DF|awk '{print $2}'
totalusedlist=`cat $DF|awk '{print $3}'
totalavailablelist=`cat $DF|awk '{print $4}'


for i in $totalspacelist
do
totalspace=`expr $totalspace + $i'
done


for i in $totalusedlist
do
totalused=`expr $totalused + $i'
done


for i in $totalavailablelist
do
totalavailable=`expr $totalavailable + $i'
done


totalspace=`expr $totalspace / 1024000'
totalused=`expr $totalused / 1024000'
totalavailable=`expr $totalavailable / 1024000'
totalpercent=`echo $totalused $totalspace|awk '{printf "%2.0f",$1/$2*100}'
echo "server,total space,used space,available space, %used" | tee $LOGFILE
echo "$HOSTNAME,${totalspace}GB,${totalused}GB,${totalavailable}GB,$totalpercent"|tee -a $LOGFILE
/bin/rm $DF

Thanks

Hi,

I was able to troubleshoot that error, but now i am getting bellow error. Pls advise me.

"expr: non-numeric argument"

my modified script:

#!/bin/sh
# Get disk metrics and outpt in csv format
DMLIST=dmlist
DF=/tmp/df$$
HOSTNAME=`hostname`
totalspace=0
totalused=0
totalavailable=0
LOGDIR=/folk/rnalluri/vobscripts/data.out
LOGFILE=${LOGDIR}/`hostname``date +%m%d%y`.disk
cd /folk/rnalluri/vobscripts
if [ ! -d $LOGDIR ]
then
echo "Making $LOGDIR"
mkdir $LOGDIR
fi
#
#Grab list for this host from our config file
#
grep "^$HOSTNAME " $DMLIST |awk '{print $2}'|xargs df -k|grep -v Filesystem>$DF
totalspacelist=`cat $DF|awk '{print $2}'`
totalusedlist=`cat $DF|awk '{print $3}'`
totalavailablelist=`cat $DF|awk '{print $4}'`
for i in $totalspacelist
do
totalspace=`expr $totalspace + $i`
done
for i in $totalusedlist
do
totalused=`expr $totalused + $i`
done
for i in $totalavailablelist
do
totalavailable=`expr $totalavailable + $i`
done
totalspace=`expr $totalspace / 1024000`
totalused=`expr $totalused / 1024000`
totalavailable=`expr $totalavailable / 1024000`
totalpercent=`echo $totalused $totalspace|awk '{printf "%2.0f",$1/$2*100}'`
echo "server,total space,used space,available space, %used" | tee $LOGFILE
echo "$HOSTNAME,${totalspace}GB,${totalused}GB,${totalavailable}GB,$totalpercent"|tee -a $LOGFILE
/bin/rm $DF

Thanks,
raja

DMLIST=dmlist
This is where your error is.

dmlist is not a linux command. if it were, you need to DMLIST=`dmlist`

Hi All,

Thank you for your response. Script is working now. I worked another way and it is working fine in linux also.
this is the script working fine in linux.

DMLIST=dmlist
DF=/tmp/df$$
HOSTNAME=`hostname`
totalspace=0
totalused=0
totalavailable=0
LOGDIR=/folk/rnalluri/vobscripts/data.out
LOGFILE=${LOGDIR}/`hostname``date +%m%d%y`.disk
cd /folk/rnalluri/vobscripts
if [ ! -d $LOGDIR ]
then
        echo "Making $LOGDIR"
        mkdir $LOGDIR
fi
#
#Grab list for this host from our config file
#
grep "^$HOSTNAME " $DMLIST |awk '{print $2}'|xargs df -k|grep -v Filesystem|grep -v /dev>$DF

totalspacelist=`cat $DF|awk '{print $1}'`
totalusedlist=`cat $DF|awk '{print $2}'`
totalavailablelist=`cat $DF|awk '{print $3}'`

for i in $totalspacelist
do
        totalspace=`expr $totalspace + $i`
done

for i in $totalusedlist
do
        totalused=`expr $totalused + $i`
done

for i in $totalavailablelist
do
        totalavailable=`expr $totalavailable + $i`
done
totalspace=`expr $totalspace / 1024000`
totalused=`expr $totalused / 1024000`
totalavailable=`expr $totalavailable / 1024000`
totalpercent=`echo $totalused $totalspace|awk '{printf "%2.0f",$1/$2*100}'`
echo "server,total space,used space,available space, %used" | tee $LOGFILE
echo "$HOSTNAME,${totalspace}GB,${totalused}GB,${totalavailable}GB,$totalpercent"|tee -a $LOGFILE
#/bin/rm $DF
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.