1

The scripts' first for loop:

for e in "${hostnames[@]}"; do

goes through the array:

hostnames=( simpsons moes sideshow flanders )

and I want the workspace array to be conditional based on the host in the value $e

Right now I'm just using the array:

workspace_simpsons=( bart homer lisa marge releases rt-private simpsons-ws0 simpsons-ws1 simpsons-ws2 vsimpsons-ws )

in the following for loop:

for i in "${workspace_simpsons[@]}"; do

I can't figure out how to make the above for loop conditional based on the host that's in the value $e.

Please help.

today=`date +"%m-%d-%y"`
basename=/export/ws
output=/share/it-ops/Build_Farm_Reports
workspace_simpsons=( bart homer lisa marge releases rt-private simpsons-ws0 simpsons-ws1 simpsons-ws2 vsimpsons-ws )
workspace_moes=( barney carl lenny moes-ws2 )
workspace_sideshow=( bob mel sideshow-ws2 )
workspace_flanders=( flanders-ws0 flanders-ws1 flanders-ws2 maude ned rod todd to-delete )
hostnames=( simpsons moes sideshow flanders )
for e in "${hostnames[@]}"; do
if [ `hostname` == $e ] && [ ! -f $e.csv ]; then
echo ",,,,,$e" >> $e.csv
echo ",,," >> $e.csv
for i in "${workspace_simpsons[@]}"; do
echo "date_""$i"","$today >> $e.csv
echo "$i"``df -h /export/ws/$i | awk '{if (NR!=1) {print "_available"","$4}}' | sed '$s/.$//' | sed '1d'`` >> $e.csv
echo "$i"``df -h /export/ws/$i | awk '{if (NR!=1) {print "_used"","$3}}' | sed '$s/.$//' | sed '1d'`` >> $e.csv
echo ",,," >> $e.csv
done
elif [ `hostname` == $e ] && [ -f $e.csv ]; then
for b in "${workspace_simpsons[@]}"; do
num1=`df -h /export/ws/$b | awk '{if (NR!=1) {print ","$4}}' | sed '$s/.$//'i | sed '1d'`
c="$b"_available
sed -i "/^$c/ s/$/$num1/" $e.csv
num2=`df -h /export/ws/$b | awk '{if (NR!=1) {print ","$3}}' | sed '$s/.$//'i | sed '1d'`
d="$b"_used
sed -i "/^$d/ s/$/$num2/" $e.csv
v=date_"$b"
sed -i "/^$v/ s/$/,$today/" $e.csv
done
else
:
fi
done

Here's the output:

,,,,,simpsons
,,,
date_bart,09-13-14,09-13-14
bart_available,95,95
bart_used,63,95
,,,
date_homer,09-13-14,09-13-14
homer_available,100,100
homer_used,5.8,100
,,,
date_lisa,09-13-14,09-13-14
lisa_available,100,100
lisa_used,3.1,100
,,,
date_marge,09-13-14,09-13-14
marge_available,96,96
marge_used,7.0,96
,,,
date_releases,09-13-14,09-13-14
releases_available,87,87
releases_used,56,87
,,,
date_rt-private,09-13-14,09-13-14
rt-private_available,46,46
rt-private_used,1.1,46
,,,
date_simpsons-ws0,09-13-14,09-13-14
simpsons-ws0_available,99,99
simpsons-ws0_used,2.4,99
,,,
date_simpsons-ws1,09-13-14,09-13-14
simpsons-ws1_available,97,97
simpsons-ws1_used,37,97
,,,
date_simpsons-ws2,09-13-14,09-13-14
simpsons-ws2_available,18,18
simpsons-ws2_used,790,18
,,,
date_vsimpsons-ws,09-13-14,09-13-14
vsimpsons-ws_available,66,66
vsimpsons-ws_used,13,66
,,,
4
  • 1
    So add an if or case that looks at $(hostname)? What exactly do you want to do? There's way too much irrelevant information in this post. Please remove everything not related to your specific question. And then ask a specific question. Commented Sep 14, 2014 at 0:24
  • Write the names to files. Name the files by host. Read from the file with the name of the host into a single array. Commented Sep 14, 2014 at 1:56
  • Mark the hostname is in the for loop above it so $e is the hostname which gets written to $e.csv. That way there's four different files generated simpsons.csv moes.csv and so on. But the loop that iterates through the workspaces should be conditional somehow based on the value of $e from the previous loop. Commented Sep 14, 2014 at 2:00
  • Etan can you show an example? Commented Sep 14, 2014 at 2:12

1 Answer 1

2

You could use a case statement to set workspaces to one of four arrays.

case $e in
    simpsons) workspaces=(bart homer lisa marge releases rt-private simpsons-ws0 simpsons-ws1 simpsons-ws2 vsimpsons-ws);;
    moes)     workspaces=(barney carl lenny moes-ws2);;
    sideshow) workspaces=(bob mel sideshow-ws2);;
    flanders) workspaces=(flanders-ws0 flanders-ws1 flanders-ws2 maude ned rod todd to-delete);;
esac

Then you can get rid of all the duplicated code and use a single loop with for ws in "${workspaces[@]}". With that improvement, plus some other cleanup, I guess it would look something like this:

case $HOSTNAME in
    simpsons) workspaces=(bart homer lisa marge releases rt-private simpsons-ws0 simpsons-ws1 simpsons-ws2 vsimpsons-ws);;
    moes)     workspaces=(barney carl lenny moes-ws2);;
    sideshow) workspaces=(bob mel sideshow-ws2);;
    flanders) workspaces=(flanders-ws0 flanders-ws1 flanders-ws2 maude ned rod todd to-delete);;
esac

if ! [ -f $HOSTNAME.csv ]; then
    {
        echo ",,,,,$HOSTNAME"
        echo ",,,"

        for ws in "${workspaces[@]}"; do
            echo "date_$ws,$today"
            echo "$ws$(df -h /export/ws/$ws | awk '{if (NR!=1) {print "_available," $4}}' | sed '$s/.$//' | sed '1d')"
            echo "$ws$(df -h /export/ws/$ws | awk '{if (NR!=1) {print "_used,"      $3}}' | sed '$s/.$//' | sed '1d')"
            echo ",,,"
        done
    } > $HOSTNAME.csv
else
    for ws in "${workspaces[@]}"; do
        num1=$(df -h /export/ws/$ws | awk '{if (NR!=1) {print "," $4}}' | sed '$s/.$//i' | sed '1d')
        num2=$(df -h /export/ws/$ws | awk '{if (NR!=1) {print "," $3}}' | sed '$s/.$//i' | sed '1d')

        sed -i "/^${ws}_available/ s/$/$num1/"   $HOSTNAME.csv
        sed -i "/^${ws}_used/      s/$/$num2/"   $HOSTNAME.csv
        sed -i "/^date_${ws}/      s/$/,$today/" $HOSTNAME.csv
    done
fi

I might be mistaken, but I don't believe the outermost for e in "${hostnames[@]}" loop is even needed, so I deleted it.

Sign up to request clarification or add additional context in comments.

8 Comments

That is beautiful it worked to perfection. I am going to have to study more about case statements and arrays but wow awesome. I have been beating my head out all day on this thanks so much.
Hey John I just had to put today=date +"%m-%d-%y" at the top and it works perfectly. :)
Glad I could help! I make no promises about this code working. This was purely off the top of my head with no actual testing performed...
If that was off the top of your head then I bow down to you. Your code formatting and use of curly braces and only doing one write at the end of the loop was amazing. All the extra code was eliminated and it worked perfectly right out of the box. Thanks again so much. Hey John I will be called upon to write many more scripts in this job. If in the future I get stuck again could I IM you? Or should I just wait to see if you show up?
Yeah, just post more questions. That's what this site is for after all. There are lots of knowledgeable shell scripting dudes here, I'm not the only one. You can try throwing an @JohnKugelman into a comment if you like. I'm not sure, but that might ping me. Cheers!
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.