Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Bash loops and variable scope Post 302503696 by 1skydive on Friday 11th of March 2011 02:01:22 PM
Old 03-11-2011
Bash loops and variable scope

Hi All,

I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and grep command.

Code:
#!/bin/bash
export date=`date '+%m/%d/%Y'`
ALERT=90
ADMIN=me@me.com
DIR="/tmp"
COUNT=0

function type1(){

  local host=$1
  ssh $host df -hP --exclude-type=nfs | grep -E "sd[bcd]|mapper" | grep -v VGsys | awk "{print \$5 \" \" \$1}" | while read output;
  do
    usep=$(echo $output | awk "{ print \$1}" | cut -d'%' -f1  )
    partition=$(echo $output | awk "{ print \$2 }" )
    if [ "$usep" -ge "$ALERT" ]; then
      USAGE=$(echo -e "$partition ($usep%)")
      TOTAL="$TOTAL $USAGE"
      echo $TOTAL
    fi
  done
  echo $TOTAL
  echo -e "$host\n$TOTAL\n"  >> "$DIR/servers.txt"

}

for i in server1 server2 server2; do
  type1 $i;
done


mail -s "$ALERT% Storage Report for $date" $ADMIN  < "$DIR/servers.txt"

rm "$DIR/server.txt"

As you can see, the TOTAL is available within the while loop but is then empty after the loop exits.

Any suggestions appreciated.

Thanks for you help.

---------- Post updated at 02:01 PM ---------- Previous update was at 10:35 AM ----------

Hah! So why is it, you bash (no pun intended) your head against a wall for a couple of days, trying every possible combination you can think of. No sooner you post the question, the light bulb goes off.

So, I moved the ssh command to the for loop output to a file and then used redirection for the while loop. Its not perfect, but at least I have my variables now and can work on the other issues I have.

This is what I ended up doing:

Code:
#!/bin/bash
export date=`date '+%m/%d/%Y'`
ALERT=90
ADMIN=me@me.com
DIR="/tmp"
COUNT=0

function type1(){

  local host=$1
  while read output
  do
    usep=$(echo $output | awk "{ print \$1}" | cut -d'%' -f1  )
    partition=$(echo $output | awk "{ print \$2 }" )
    if [ "$usep" -ge "$ALERT" ]; then
      USAGE=$(echo -e "$partition ($usep%)")
      TOTAL="$TOTAL$USAGE"
      echo $TOTAL
    fi
  done < "$DIR/hosts.txt"
  echo -e "$host\n$TOTAL\n"  >> "$DIR/servers.txt"

}

for i in server1 server2 server2; do
  ssh $host df -hP --exclude-type=nfs | grep -E "sd[bcd]|mapper" | grep -v VGsys | awk "{print \$5 \" \" \$1}" >> "$DIR/hosts.txt"
  type1 $i;
done


mail -s "$ALERT% Storage Report for $date" $ADMIN  < "$DIR/servers.txt"

rm "$DIR/server.txt"
rm "$DIR/hosts.txt"

Hope it helps someone else out.
 

10 More Discussions You Might Find Interesting

1. Programming

C++ variable scope and mutexes

I've been wondering if I can make mutexes much easier to use in C++ with creative use of a locking class and variable scope, but I'm not sure if things happen in the order I want. Here's pseudocode for something that could use the class: int someclass::getvalue() { int retval; ... (0 Replies)
Discussion started by: Corona688
0 Replies

2. Shell Programming and Scripting

problem with shell variable's scope

Hi, I am stuck while developing a shell sub-routine which checks the log file for "success" or "failure". The subroutine reads the log file and checks for key word "success", if found it set the variable (found=1). It returns success or failure based on this variable. My problem is, I can... (2 Replies)
Discussion started by: cjjoy
2 Replies

3. Shell Programming and Scripting

scope of the variable - Naga

Hi All, I am new to unix shell scripting, in the below script "num" is an input file which contains a series of numbers example : 2 3 5 8 I want to add the above all numbers and want the result finally outside the while loop. it prints the value zero instead of the actual expected... (13 Replies)
Discussion started by: nagnatar
13 Replies

4. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

5. Shell Programming and Scripting

Help with retaining variable scope

Hi, I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count. while read name do Tmp=`echo $name | awk '{print $9 }'` Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies

6. Shell Programming and Scripting

Variable scope in bash

Hello! Before you "bash" me with - Not another post of this kind Please read on and you will understand my problem... I am using the below to extract a sum of the diskIO on a Solaris server. #!/bin/sh PATH=/usr/bin:/usr/sbin:/sbin; export PATH TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies

7. Shell Programming and Scripting

Maintain Scope of the variable in UNIX

Hi All Is there is any way to maintain the scope of the variable in unix Example x=1 j=1 while do .. .... .... while do .. .. x=x+1 done #inner most while loop ends here done #outer loop ends here (8 Replies)
Discussion started by: parthmittal2007
8 Replies

8. Shell Programming and Scripting

BASH: variable and function scope and subscripts

Hi, I'm a Delphi developer new to linux, new to this forums and new to BASH programming and got a new task in my work: maintaining an existing set of BASH scripts. First thing I want to do is making the code more reliable as in my opinion it's really bad written. So here's the quest: I'm... (6 Replies)
Discussion started by: rse
6 Replies

9. Programming

Variable Scope in Perl

I have to admit that i have not used Perl at all and this is a singular occasion where i have to patch an existing Perl script. I dearly hope i do not have to do it again for the next 15 years and therefore try to avoid having to learn the programming language in earnest. The OS is AIX 7.1, the... (2 Replies)
Discussion started by: bakunin
2 Replies

10. UNIX for Beginners Questions & Answers

Bash Variable scope - while loop while reading from a file

Cope sample1: test.sh i=0 echo " Outside loop i = $i " while do i=$(( $i + 1)) echo "Inside loop i = $i " done echo " Out of loop i is : $i " When run output : Outside loop i = 0 Inside loop i = 1 Inside loop i = 2 Inside loop i = 3 Inside loop i = 4 Inside loop i = 5 Inside... (8 Replies)
Discussion started by: Adarshreddy01
8 Replies
break(1)							   User Commands							  break(1)

NAME
break, continue - shell built-in functions to escape from or advance within a controlling while, for, foreach, or until loop SYNOPSIS
sh break [n] continue [n] csh break continue ksh *break [n] *continue [n] ksh93 +break [n] +continue [n] DESCRIPTION
sh The break utility exits from the enclosing for or while loop, if any. If n is specified, break n levels. The continue utility resumes the next iteration of the enclosing for or while loop. If n is specified, resume at the n-th enclosing loop. csh The break utility resumes execution after the end of the nearest enclosing foreach or while loop. The remaining commands on the current line are executed. This allows multilevel breaks to be written as a list of break commands, all on one line. The continue utility continues execution of the next iteration of the nearest enclosing while or foreach loop. ksh The break utility exits from the enclosed for, while, until, or select loop, if any. If n is specified, then break n levels. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be exited. The continue utility resumes the next iteration of the enclosed for, while, until, or select loop. If n is specified then resume at the n- th enclosed loop. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be used. On this manual page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words that follow a command preceded by ** that are in the format of a variable assignment are expanded with the same rules as a variable assignment. This means that tilde substitution is performed after the = sign, and also that word splitting and file name generation are not performed. ksh93 break is a shell special built-in that exits the smallest enclosing for, select, while, or until loop. It also exits the nth enclosing loop if n is specified. Execution continues at the command following the loop or loops. If n is specified, it must be a positive integer >=1. If n is larger than the number of enclosing loops, the last enclosing loop is exited. continue is a shell special built-in that continues execution at the top of the smallest enclosing for, select, while, or until loop, if any; or of the top of the nth enclosing loop if n is specified. If n is specified, it must be a positive integer >=1. If n is larger than the number of enclosing loops, the last enclosing loop is used. On this manual page, ksh93(1) commands that are preceded by one or two + symbols are special built-in commands and are treated the follow- ing ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Built-in commands are not valid function names. 5. Words following a command preceded by ++ that are in the format of a variable assignment are expanded with rules as a variable assignment. This means that tilde substitution is performed after the = sign and field splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), exit(1), ksh(1), ksh93(1), sh(1), attributes(5) SunOS 5.11 8 Apr 2008 break(1)
All times are GMT -4. The time now is 02:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy