我已经尝试了下面的bash脚本,这至少是为我工作,我知道它不完美,这是为什么我想要一些审查和建议,使它更符合bash实践。我试图阅读bash手册,并寻求帮助从谷歌和堆叠溢出的问题和答案。
我不能很好地解释,因此得不到有成效的帮助。但这就是它的工作方式:-)。
下面是我的工作代码,它基本上有一个卷的array_list,比如array_fxn1 ...,我从一个文件文件中提取它,然后构建它们,并进一步确保ldap_volList不是同时为空,然后在array_list上做一些for循环,进一步检查,然后ssh到服务器运行命令,并对某些列执行一些和。
正如您可能看到的,它总是相对于array_list或者说volume_list,例如,如果它的array_fxn1=那么它的dbcl101,如果它的array_fxn2=那么它的dbcl201等等。
array_fxn1 then fxn101
array_fxn2 then fxn201
array_fxn3 then fxn301
array_fxn4 then fxn401但我没有得到这种逻辑构建,所以来下面的一个,它可以为我工作,因为我不是一个专家。
任何建议,都将不胜感激。
my代码:
#!/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 #qcStackCode#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 #qcStackCode#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 #qcStackCode#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 #qcStackCode#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 #qcStackCode#i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}'
fi
done
fi输出:
$ 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:
CVS数据样本:
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命令数据:
1)
$ ssh dbcl101 "row 0; vol show fxn1002_dbdesk_scratch -fields volume-style-extended" |awk '/fxn/{print $3}'
flexvol上面的命令确保输出是否返回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上面的命令是完全命令,它有volume type,就像我们在前面的命令中一样,它包含了flexvol,这就是我们通过awk存储给变量的内容。
--
$ 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上面的一个同样的命令,用于打印字段2, 3 & 4。
发布于 2022-07-17 02:28:24
我不知道你在努力实现什么,但我们还是这样做的。
else。for循环都是相同的;只有服务器名称和数组名称不同,而且它们有很大的区别。将公共代码提升到一个函数中: do_stuff() {array=$(回显"$ldap_volList“\ grep ^fxn${1}) server = "dbcl"${1}"01”,用于i in $array;do volstyle=$(ssh ${server} "row 0;volume $i -fields卷式-Extended“x awk '/fxn/{print $3}') if [];然后回送-n "$ldap_proj:$i“ssh ${server}”行0;设置-unit MB;“显示足迹”$i -fields卷-块-足迹-bin0,卷-块-足迹-bin1 1,总足迹“total”/fxn/{print $3,$4,$5} fi完成,并在一个循环中调用它: for i in {1..8}:do_stuff ${i} do发布于 2022-07-19 17:24:31
在浏览您的帖子和需求时,我了解您在string匹配下面寻找的位置,为了更好地解决这个问题,我将使用regexp匹配=~和Bash逻辑(&&)运算符,其中&&允许您根据前面的命令是否成功完成执行一些操作--这就是为什么您倾向于将其链接为do_something &&
array_fxn1 then fxn101
array_fxn2 then fxn201
array_fxn3 then fxn301
array_fxn4 then fxn401下面是一些我会尝试的东西,如果我是你,我不是一个存储专家和NetAPP访问因此不能再尝试,请试一试。
#!/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现在,您不需要多次运行for loop。
https://codereview.stackexchange.com/questions/278123
复制相似问题