Script returning 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script returning 0
# 1  
Old 02-13-2013
Script returning 0

hello i write a script which calculate free space but he always is 0 thats wrong with my script?

Code:
getFileSystemPerformanceData()
{


  if [ -r /etc/issue ] ; then
     if grep -q "Ubuntu" /etc/issue ; then
         CMD="df -lP | grep -v "\/home" | grep -v "\/dev/mapper/VolGroup-lv_root""
      elif grep -q "Mageia" /etc/issue; then
        CMD="df -lP | sed -e '2d'"
       elif grep -q "Fedora" /etc/issue; then
          CMD="df -lP | grep -v "\/home" | grep -v "\/dev/mapper/VolGroup-lv_root""
       else
       CMD="df -lP"
       fi
   else
  CMD="df -lP"
  fi
  OUTPUT=`$CMD`
    echo "$OUTPUT" |
    ( # Skip the header-line
        read IGNORED
        SUM=0
        SIZE=0
        while read FS SIZE USED AVAIL PCT MOUNTED ; do
            SIZE=`echo "$AVAIL 1024" | awk '{print $1/$2}'`
            SUM=`echo "$SUM $SIZE" | awk '{print $1 + $2}'`
        done
        PERFCLASS=LogicalDisk;PERFINSTANCE=$FS;PERFMETRIC='Avail Megabytes';PERFINT=0;PERFSAMP=1;PERFAVG=$SUM;PERFMIN=$PERFAVG;PERFMAX=$PERFAVG;writePerformanceDataXml
    )
    
}


Last edited by Scrutinizer; 02-13-2013 at 07:15 PM.. Reason: moved closing code tag
# 2  
Old 02-13-2013
Try to run your script in debug mode.
Code:
bash -x ./yourscript

# 3  
Old 02-13-2013
Well, I think you - after assigning variables to and fro - echo "$CMD", read that , ignore it, and skip the while loop as there's nothing more to read, leaving all variables = 0:
Code:
.
.
.
CMD="df -lP"
fi                    # from somewhere above
OUTPUT=`$CMD`         # assign literal $CMD to OUTPUT
echo "$OUTPUT" |      # echo $CMD literally - one word in one single line
read IGNORED          # ignore literal $CMD, input stream ends, skip loop
.
.
.


Last edited by RudiC; 02-13-2013 at 04:02 PM..
# 4  
Old 02-13-2013
Perhaps your should try:

Code:
eval "$CMD" | ( # skip the header-line
    read IGNORED
    ...

# 5  
Old 02-14-2013
I have to revise my comment: I've taken the backtics for single quotes due to sloppy reading - sorry for that.
So, OUTPUT will contain the desired info from df -lP line by line, which you can and do read into several variables to compute with - in a subshell! You can't have subshell variables returned to the calling script except by echoing them there and reading them here.
If I read your script correctly (now, finally!), all you want is the overall available size in MB. Why don't you try to get that in an one-liner:
Code:
df -lP | awk 'NR>1 {sum+=$4/1024} END {print sum}'
30569.2

# 6  
Old 02-14-2013
yes you get it correct about that i want to do . i cant use df -lP because script schoud work for many different linux mashine and in some mashine pvz
Code:
Filesystem            Size  Used Avail Use% Mounted on
rootfs                9.0G  3.6G  4.9G  43% /
udev                  368M     0  368M   0% /dev
tmpfs                 375M  248K  375M   1% /dev/shm
tmpfs                 375M  552K  375M   1% /run
/dev/mapper/VolGroup-lv_root  9.0G  3.6G  4.9G  43% /
tmpfs                 375M     0  375M   0% /sys/fs/cgroup
tmpfs                 375M     0  375M   0% /media
/dev/sda1             485M   31M  430M   7% /boot
/dev/mapper/VolGroup-lv_root  9.0G  3.6G  4.9G  43% /tmp
/dev/mapper/VolGroup-lv_root  9.0G  3.6G  4.9G  43% /var/tmp
/dev/mapper/VolGroup-lv_root  9.0G  3.6G  4.9G  43% /home

and with your command i get
Code:
[root@localhost ~]# df -lP | awk 'NR>1 {sum+=$4/1024} END {print sum}'
27220.8

but mashine have only 12000MB

thats why i little modiffied df -lP command

Last edited by Franklin52; 02-14-2013 at 11:14 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script not returning what I am expecting

Hi there I am trying to create a script where I am checking the process is being run by the correct user, when I go to run the script it is not returning what I am expecting and I am not 100% sure why!!! First off I have determined what the process user should be at the top of the script ... (3 Replies)
Discussion started by: simpsa27
3 Replies

2. UNIX for Dummies Questions & Answers

Not returning from a sub script?

My problem in brief is that I execute a script from another script and I can not pick up the return code from that script, or otherwise I am not returning from that script. I have an echo in the executed script and we get a response code of 0 and exit that script with the return code. I then try to... (1 Reply)
Discussion started by: Charles Swart
1 Replies

3. Shell Programming and Scripting

Child exiting and not returning to parent script

I am having a parent scripts which reads a file with child scripts name. I need to read one by one child script , execute it and 1. If child script fails send mail to the team with the log file 2. If the child script executes fine then proceed with the next child script execution. #!... (3 Replies)
Discussion started by: nw2unx123
3 Replies

4. Shell Programming and Scripting

Returning to shell from previous script

Found myself stuck on this seemingly trivial issue. I have this script which call other shell files to do their deeds. The shell files in turn call some other programs as well. My problem is that in two of these shell files, the control doesnt return to next command in script unless the Enter key... (2 Replies)
Discussion started by: DoesntMatter
2 Replies

5. Shell Programming and Scripting

Script returning null results

Hi, The following shell script returning null results could you please tell me whats the problem in script, ********************************* #!/bin/ksh . $HOME/conf/systemProperties/EnvSetup.properties a=`date +"%y%m%d"` set -x for i in `cat... (2 Replies)
Discussion started by: shivanete
2 Replies

6. UNIX for Advanced & Expert Users

Returning a value to a calling script

Hi. I'm trying to call a script named checkForFile.sh from within my main script named master.sh. In checkForFile.sh I want to set a value to the variable fileExist, but then I want to reference the value in the master.sh script. Whenever I attempt this the echo statement is just blank. ... (5 Replies)
Discussion started by: buechler66
5 Replies

7. AIX

The shell script is not returning proper result

Can anybody pls look into this script and tell me where I went wrong. After running this script, it is showing like "Trying to overlay current working directory ABORT!!!" :-( ARGCNT=$# if then echo "Two parameters are needed for this shell " echo "Please try again with... (1 Reply)
Discussion started by: clnsharma123
1 Replies

8. Shell Programming and Scripting

returning to the parent shell after invoking a script within a script

Hi everybody, I have a script in which I'm invoking another script which runs in a subshell. after the script is executed I want to return to the parent shell as some variables are set. However i'm unable to return to my original shell as the script which i'm calling inside is invoked in... (5 Replies)
Discussion started by: gurukottur
5 Replies

9. Shell Programming and Scripting

Returning to begining of a script

Dear all, When i ask a question in my script if the answer is not correct i need to goback to the begning of the script and ask the question again. ex - What is your name ? saman Name is not correct ...try again What is the name ? Nimal Thank you.... (2 Replies)
Discussion started by: Nayanajith
2 Replies

10. Shell Programming and Scripting

Returning Values (shell Script)

I have an application on Informix 4GL, and I am invoking the shell script from the program, but I need to know if the shell script work fine or not, in order to continue the process. I know that we can use $? to get the final status but this is on unix command. How can I return a value from the... (3 Replies)
Discussion started by: jennifer01
3 Replies
Login or Register to Ask a Question