3
\$\begingroup\$

I have tried to come with below bash script which is at least working for me, i know its not perfect and that why i want some review and suggestion to make it more aligned with bash practices. I'm trying to read bash manuals and seeking help from google and stackoverflow questions and answers.

I'm not able to explain well thus not getting fruitful help.. but that's the way it works :-) .

Below is my working code which basically have a array_list of volumes like array_fxn1 ... which i'm taking from a file file and then building them and further making sure if ldap_volList is not empty at the same time and then doing some for loop over the array_list further doing check and then ssh to the Server to run the command and do some sum of the certain columns.

As you may see, its always relative to array_list or say volume_list, for an example if its array_fxn1= then its dbcl101, if its array_fxn2= then its dbcl201 and so on ..

   array_fxn1 then fxn101
   array_fxn2 then  fxn201
   array_fxn3 then  fxn301
   array_fxn4 then fxn401

but i am not getting that logic build so come to the below one which can work for me being not an expert.

Any suggestion, will be much much appreciated.

my code:

#!/bin/bash
#
read -rp "Please Enter LDAP Project Name: " ldap_proj
ldap_volList=$(cat ldap-project-nismap.csv |grep "$ldap_proj" | tr "," "\t"| awk '{print $NF}'| tr -d '"')
# Build an array list of desired Volumes beforehand to make it more precise
array_fxn1=$(echo "$ldap_volList" | grep ^fxn1)
array_fxn2=$(echo "$ldap_volList" | grep ^fxn2)
array_fxn3=$(echo "$ldap_volList" | grep ^fxn3)
array_fxn4=$(echo "$ldap_volList" | grep ^fxn4)
array_fxn6=$(echo "$ldap_volList" | grep ^fxn6)
array_fxn7=$(echo "$ldap_volList" | grep ^fxn7)
array_fxn8=$(echo "$ldap_volList" | grep ^fxn8)

# Place a check to ensure the Volumelist is not empty and then loop over the volume array's
if [[ ! -z "$ldap_volList" ]]; then
     for i in $array_fxn1;
     do
       volstyle=$(ssh dbcl101 "row 0; vol show $i -fields volume-style-extended" |awk '/fxn/{print $3}')
       if [[ "$volstyle" == "flexvol" ]];then
         echo -n "$ldap_proj:$i  "
         ssh dbcl101 "row 0;set -unit MB; \
         vol show-footprint $i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"||awk '/^stv/{s1+=$3; s2+=$4; s3+=$5 } END {print s1, s2, s3}'
       fi
     done
     for i in $array_fxn2;
     do
       volstyle=$(ssh dbcl201 "row 0; vol show $i -fields volume-style-extended" |awk '/fxn/{print $3}')
       if [[ "$volstyle" == "flexvol" ]];then
         echo -n "$ldap_proj:$i  "
         ssh dbcl201 "row 0;set -unit MB; \
         vol show-footprint $i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"||awk '/^stv/{s1+=$3; s2+=$4; s3+=$5 } END {print s1, s2, s3}'
       fi
     done
     for i in $array_fxn3;
     do
       volstyle=$(ssh dbcl301 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}')
       if [[ "$volstyle" == "flexvol" ]];then
         echo -n "$ldap_proj:$i  "
         ssh dbcl301 "row 0;set -unit MB; \
         vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
       fi
     done
     for i in $array_fxn4;
     do
       volstyle=$(ssh fxn401 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}')
       if [[ "$volstyle" == "flexvol" ]];then
         echo -n "$ldap_proj:$i  "
         ssh dbcl401 "row 0;set -unit MB; \
         vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
       fi
     done
     for i in $array_fxn6;
     do
       volstyle=$(ssh dbcl601 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}')
       if [[ "$volstyle" == "flexvol" ]];then
         echo -n "$ldap_proj:$i  "
         ssh dbcl601 "row 0;set -unit MB; \
         vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
       fi
     done
     for i in $array_fxn7;
     do
       volstyle=$(ssh dbcl701 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}')
       if [[ "$volstyle" == "flexvol" ]];then
         echo -n "$ldap_proj:$i  "
         ssh dbcl701 "row 0;set -unit MB; \
         vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
       fi
     done
     for i in $array_fxn8;
     do
       volstyle=$(ssh dbcl804 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}')
       if [[ "$volstyle" == "flexvol" ]];then
         echo -n "$ldap_proj:$i  "
         ssh dbcl804 "row 0;set -unit MB; \
         vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
       fi
     done
fi

Output:

$ bash new_test.sh
Please Enter LDAP Project Name: web3
web3:fxn1002_web3_wiki  14030MB 13292MB 0MB
web3:fxn1002_web3_wvg   690194MB 679021MB 0MB
web3:fxn1002_web3_wso   70MB 38MB 0MB

UPDATE ON THE POST:

CVS DATA Sample:

Location,ProjectName,NisMap,"Total(GB)","Used(GB)","Used_Percentage",Quality,Id,volumeName
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle_pd2/q",1000,"761.87",76,economy,"eaeagle_pd2","dbc2002_dnpc_eaeagle_pd2"
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_7/q",4096,"3536.99",86,scratch,"eaeagle2_7","dbc2002_dnpc_eaeagle2_7"
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_6/q",4096,"2976.74",73,scratch,"eaeagle2_6","dbc2002_dnpc_eaeagle2_6"
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_5/q",4096,"3111.61",76,economy,"eaeagle2_5","dbc2002_dnpc_eaeagle2_5"
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_4/q",4096,"3595.16",88,economy,"eaeagle2_4","dbc2002_dnpc_eaeagle2_4"
"DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_3/q",4096,"2296.67",56,economy,"eaeagle2_3","dbc2002_dnpc_eaeagle2_3"

NetAPP command data:

1)

$ ssh dbcl101 "row 0; vol show fxn1002_dbdesk_scratch  -fields volume-style-extended" |awk '/fxn/{print $3}'

flexvol

the above command I am making sure if the output returns flexvol.

$ ssh dbcl101 "row 0; vol show fxn1002_dbdesk_scratch  -fields volume-style-extended"

Last login time: 7/17/2022 21:38:02
  (rows)

vserver volume                     volume-style-extended
------- -------------------------- ---------------------
fxn1002 fxn1002_dbdesk_scratch flexvol

The above one is full command which have volume type like we have in previous command which contains the flexvol and that's what we are getting store via awk to a variable.

  1. The below NetApp command returns the size of a volume.

--

$ ssh dbcl101 "row 0; vol show-footprint fxn1002_dbdesk_scratch -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"

Last login time: 7/17/2022 21:39:26
  (rows)

vserver volume                     total-footprint volume-blocks-footprint-bin0 volume-blocks-footprint-bin1
------- -------------------------- --------------- ---------------------------- ----------------------------
fxn1002 fxn1002_dbdesk_scratch 880.4GB         18.91GB                      854.8GB
fxn1002 fxn1002_dbdesk_scratch 880.4GB         18.91GB                      854.8GB

$ ssh dbcl101 "row 0; vol show-footprint fxn1002_dbdesk_scratch -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
880.4GB 18.91GB 854.8GB

The above one again the same command which does the print the field 2, 3 & 4.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ I made a guess as to what the script does and retitled the question according to our site guidelines. It would be helpful if you added a sample of what your CSV contains and what the NetApp command output looks like. \$\endgroup\$ Jul 16, 2022 at 17:05
  • 1
    \$\begingroup\$ @200_success, thanks for the remark, let me update the Question. \$\endgroup\$
    – user294110
    Jul 18, 2022 at 4:54

2 Answers 2

4
\$\begingroup\$

I have no idea of what you are trying to achieve, but still here we go.

  • Less indentation is better than more indentation. Compare

      if condition; then
          do_plenty_of_work
      fi
    

    vs

      if not condition; then
          return
      fi
      do_plenty_of_work
    

    Second version is much more readable. I don't have to scroll down to see that there is no else.

  • Don't Repeat Yourself (aka DRY). All your for loops are identical; only the server name, and the array name differ, and they differ in a very regular way. Lift the common code into a function:

      do_stuff() {
          array=$(echo "$ldap_volList" | grep ^fxn${1})
          server = "dbcl"${1}"01"
          for i in $array; do
              volstyle=$(ssh ${server} "row 0; vol show $i -fields volume-style-extended" |awk '/fxn/{print $3}')
              if [[ "$volstyle" == "flexvol" ]];then
                  echo -n "$ldap_proj:$i  "
                  ssh ${server} "row 0;set -unit MB; \
                  vol show-footprint $i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
              fi
           done
    

    and call it in a loop:

      for i in {1..8}: do
          do_stuff ${i}
      done
    
\$\endgroup\$
2
  • \$\begingroup\$ @ vnp , thanks for the answer, i am updating my question to make it little more clear. \$\endgroup\$
    – user294110
    Jul 18, 2022 at 4:55
  • \$\begingroup\$ I have updated my Post in case it make it more clear. \$\endgroup\$
    – user294110
    Jul 18, 2022 at 15:58
1
\$\begingroup\$

While going through your post and your requirement as I understand where you are looking for below string match, for this to solve better I would use regexp matches =~ and Bash logical (&&) operator where && lets you do something based on whether the previous command completed successfully - that's why you tend to see it chained as do_something &&

   array_fxn1 then fxn101
   array_fxn2 then fxn201
   array_fxn3 then fxn301
   array_fxn4 then fxn401

Below is something I'll try if I would be you, I'm not a Storage expert and NetAPP access hence can not try any more, please give it a try ..

#!/bin/bash
##
read -rp "Please Enter LDAP Project Name: " ldap_proj
ldap_volList=$(grep  "$ldap_proj" ldap-project-nismap.csv | tr "," "\t"| awk '{print $NF}'| tr -d '"')
for vols in $(echo "$ldap_volList");
 do
    [[ $vols =~ ^fsx1 ]] && ssh_server="dbcl101";
    [[ $vols =~ ^fsx2 ]] && ssh_server="dbcl201";
    [[ $vols =~ ^fsx3 ]] && ssh_server="dbcl301";
    [[ $vols =~ ^fsx4 ]] && ssh_server="dbcl401";
    [[ $vols =~ ^fsx5 ]] && ssh_server="dbcl501";
    [[ $vols =~ ^fsx6 ]] && ssh_server="dbcl601";
    [[ $vols =~ ^fsx7 ]] && ssh_server="dbcl701";
    [[ $vols =~ ^fsx8 ]] && ssh_server="dbcl804";
    volstyle=$(ssh "$ssh_server" "row 0; vol show $vols -fields volume-style-extended" |awk '/fsx/{print $3}')
    if [[ "$volstyle" == "flexvol" && ! -z "$ssh_server" ]];then
      echo -n "$ldap_proj:$vols "
      ssh $ssh_server "row 0;set -unit MB; \
      vol show-footprint $vols -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint" \
      |awk '/fsx/{print $3,$4,$5}'
    fi
  done

Now, you don't need to run for loop multiple times.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.