Simple shell script function


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Simple shell script function
# 1  
Old 02-27-2019
Simple shell script function

Hello, Trying to look



Code:
vgcheck() {

   lsvg -o|
             while read vg; do
                        lsvg -p $vg|grep Missing
                       if [ $? -ne "0" ];then
                          echo "OK:PASSED"
                       else
                          echo "FAIL"
                       fi

}

function looks at every VG(volume group on servers having multiple) and if there are disks in missing state says OK. if it doesnt gives error.

Here is output I get :
Code:
OK:PASSED
OK:PASSED


I would like the script to give single output of "OK:PASSED" instead of each time. how do i do it?

Iterate through and gives one OK:PASSED if there is no missing in each of the VG.




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-28-2019 at 04:52 AM.. Reason: Added CODE tags.
# 2  
Old 02-28-2019
Code:
if [ $? -ne 0 ]; then
    :
else
    echo "FAIL"
    return
fi
echo "OK:PASSED"

Only I don't understand a little why not 0 is not an error? And there is no operator "done"

--- Post updated at 10:08 ---

Code:
vgcheck() {
        lsvg -o | 
                while read vg; do
                        lsvg -p $vg | grep Missing
                        if [ $? -ne 0 ]; then
                                :   
                        else
                                echo "FAIL"
                                return #if you need exit
                        fi  
                done
        echo "OK:PASSED"
}

# 3  
Old 02-28-2019
You might want to return an exit code other that 0.


How about a slightly different approach, like (untested; verify the grep options)



Code:
vgcheck() {  lsvg -o | while read vg; do lsvg -p $vg; done  | grep -qm1 "Missing"  && echo "Fail" || echo "OK:PASSED"; }

Add an exit code to taste...
# 4  
Old 02-28-2019
Quote:
Originally Posted by kvosu
function looks at every VG(volume group on servers having multiple) and if there are disks in missing state says OK.
Not quite: lsvg -o in AIX (i suppose this is AIX, yes?) gives a list of volume groups in varyon state, not "every volume group". If there is a volume group but you did a varyoffvg <vg> then it will not be included in this list, although it will be displayed in lsvg.

Also notice that in HACMP-clusters nowadays (HACMP 7.1.x and 7.2) VGs are in a "somewhat varyon" state on the passive node(s) if you get your disks from a SAN and the LUNs are in the "no_reserve" state. I am not sure if a lsvg -p would succeed on such a VG even though the disks are there.

Quote:
Originally Posted by kvosu
I would like the script to give single output of "OK:PASSED" instead of each time. how do i do it?
Wouldn't it make a lot of sense to include the name of the VG with problems if there is one? You also should not use "echo" in a ksh (you do use ksh, no?) but print, which is a shell-builtin. If you want to be 101% POSIX-compliant use neither but printf.

Anyway, consider this, change the error message to your liking and/or remove the -u2 if you want to redirect the error message to stdout instead of stderr:

Code:
vgcheck ()
{
typeset chVG=""

lsvg -o | while read chVG ; do
     if ! lsvg -p "$chVG" | grep -qi "missing" ; then
          print -u2 "Error: ($chVG) disks missing"
          return 1
     fi
done
print "OK:PASSED"
return 0
}

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on simple shell script

Hello, I need to create one very simple shell script that checks if the first character of the file ./pump.txt is 0 and in that case gives a message. If the first character is instead 1, it does give a different message. I have written: irr= head -c 1 ./pump.txt if ]; then echo... (4 Replies)
Discussion started by: dcaccount
4 Replies

2. Shell Programming and Scripting

Help on simple shell script

Hello, I am running openmediavault on my Raspberry and I would like to use it as a backup FTP server of snapshots taken from my IP cams. So I get the network recorder to upload every 3 seconds a snapshot to the Raspberry. Everything works perfectly. I would need now a simple script that... (5 Replies)
Discussion started by: dcaccount
5 Replies

3. Shell Programming and Scripting

Help with simple Shell Script

Hi , I am in need of simple shell script that has one input file containing some words Input file 1 : ****ALEX***JOHN*******VIRGIL***** CHRITINE*****FAISAL*****DON***** ****ALEX***JOHN*******VIRGIL***** CHRITINE*****FAISAL*****DON***** ****ALEX***JOHN*******VIRGIL*****... (6 Replies)
Discussion started by: kmanjuna
6 Replies

4. Shell Programming and Scripting

Help me with simple shell script.

Hello forum members, I have to redirect a output of command into a text file inside a script file but iam getting an errors.so please see below script and suggest me for corrections. #!/bin/ksh read IP_ADD echo nslookup $IP_ADD 2>&1| tee log1.txt cat /amex/gcst/siva/Testr/log1.txt... (6 Replies)
Discussion started by: rajkumar_g
6 Replies

5. Shell Programming and Scripting

Simple Shell Script

Hello Friends, I am writing a shell script which will grab a file if it exists and copies it to another folder and will append with current date. I have written but gives me error, plz help: -------------------------------------------- #!/usr/bin/sh source=/home/dev4rice/naveen/test1... (4 Replies)
Discussion started by: ganesh123
4 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. Shell Programming and Scripting

Simple Shell Script using function

Hi all, I am new to shell scripting.I have made a simple shell script which will give me number of records in a database table. The SQL statement is working fine and there are 11 rows in this table. But problem is that it is not printing this value and fucntion does not get called. Please see the... (5 Replies)
Discussion started by: 33junaid
5 Replies

8. Shell Programming and Scripting

simple shell - how to get a parameter typed in a shell script

Hi, I am new to unix and using linux 7.2. I would like to create a script that would make it easyer for me to run my java programms. At the moment I have to type java myJavaprogram I am trying to write a script that will allow me to type something like this "myscript myJavaprogram" or maybe... (4 Replies)
Discussion started by: cmitulescu
4 Replies

9. Shell Programming and Scripting

Simple Shell Script Need Help

I M TRYING TO DO SUM THING I M A NEW LEARNER TO SHELL SCRIPT PLZZZ HELP ME FRIENDS I M TRY TO DO SUM THING C IT AND HELP ME First open a .ctl file Copy the file into temp file Add A EXTRA FIELD “ext_date” with keeping value “##**&&” EG- EXT_DATE CONSTANT"******" Each time... (2 Replies)
Discussion started by: kulbir
2 Replies

10. Shell Programming and Scripting

need a simple shell script

Hi, I am new to unix as well as shell programming. Any body can provide me a simple shell script which should copy/transfer/fetch a file(using FTP)from remote server to local system.and it should log the details when it was fetched.If there is any error,the error msg should log in log... (1 Reply)
Discussion started by: Mar1006
1 Replies
Login or Register to Ask a Question