bash script does not enter loop
Hello
I am attempting a bash script that checks disk space on a host. What I'd like to be able to do is to iterate through a list of disks and report on the space free of the disks in the list. Here is what I was able to come up with: Code:
#!/bin/bash Code:
[db-host:root:~]#./check_ora_filesys.sh I notice that if I move the placement of the 'done' statement to terminate the loop before the code I'm hoping to execute, the looping succeeds. Code:
#!/bin/bash This is the output if I perform this test Code:
[db-host:root:~]#./check_ora_filesys.sh |
Quick guess: Those "exit" commands are taking you out of the for loop. Check the syntax and usage for "exit".
|
You may want to try only one disk at a time for now.
Try replacing your exit with Code:
echo $? If not, it will tell you at what step the error is. Good Luck ! :D |
Yes, the exit commands will break the script at that point, as long as the conditions match.
Probably the best you can do is save a "status" variable and exit with that at the end. It will only show the status of the last loop iteration though, unless you write something that stores and prints the outputs of all the drives individually. I would also change the script to use a single array instead of a brace expansion. Much cleaner. It would also be more efficient to store the output of df once, and parse that for the values you want. You can then use parameter substitutions to extract them more efficiently than with awk/sed/grep. The fewer calls to external tools the better, usually. Finally, when using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability. http://mywiki.wooledge.org/ArithmeticExpression http://mywiki.wooledge.org/BashFAQ/031 http://wiki.bash-hackers.org/commands/classictest http://wiki.bash-hackers.org/syntax/...nal_expression Here's the modified version I came up with: Code:
#!/bin/bash Notice also how I stored the output of df into an array too (after using cut to chop off the first line), making it easier to access the individual fields. Actually we could probably even skip using cut and just change it to extract fields 10 and 11 instead. |
All times are GMT -5. The time now is 05:11 PM. |