일치하는 콘텐츠를 기반으로 파일 끝에 값을 추가합니다.

일치하는 콘텐츠를 기반으로 파일 끝에 값을 추가합니다.

내 스크립트는 다음과 같습니다

###BEGIN#
#!/bin/bash
### Daily Volume Growth
echo "Volume Name     Total Size   `date +%F`" >> volgrow

echo "`ssh 192.168.1.2 df -h Volume1 |head -2 | tail -1 | awk '{ print $1,$2 }'`" >> volgrow
echo "`ssh 192.168.1.2 df -h Volume2 |head -2 | tail -1 | awk '{ print $1,$2 }'`" >> volgrow
echo "`ssh 192.168.1.2 df -h Volume3 |head -2 | tail -1 | awk '{ print $1,$2 }'`" >> volgrow
###END#

내 출력은 다음과 같습니다

Volume Name     Total Size 2017-05-25
/vol/Volume1/ 5632GB
/vol/Volume2/ 2136GB
/vol/Volume3/ 5110GB

내 요구 사항은 다음과 같습니다

Volume Name     Total Size 2017-05-25  2017-05-26 2017-05-27
/vol/Volume1/ 5632GB  5633GB 5630GB
/vol/Volume2/ 2136GB  2137GB 2138GB
/vol/Volume3/ 5110GB 5109GB 5111GB

답변1

###BEGIN#
#!/bin/bash
### Daily Volume Growth
#### Will execute once when file not exist and add first entry in the file
if [ ! -f volgrow ]; then
      echo "Volume Name     Total Size   `date +%F`" >> volgrow
    echo "`ssh 192.168.1.2 df -h Volume1 |head -2 | tail -1 | awk '{ 
print $1,$2 }'`" >> volgrow
    echo "`ssh 192.168.1.2 df -h Volume2 |head -2 | tail -1 | awk '{ 
print $1,$2 }'`" >> volgrow
    echo "`ssh 192.168.1.2 df -h Volume3 |head -2 | tail -1 | awk '{ 
print $1,$2 }'`" >> volgrow
fi

#### get all data in different variables 
line1 = echo "`date +%F`"
line2 = "`ssh 192.168.1.2 df -h Volume1 |head -2 | tail -1 | awk '{ print $1,$2 }'`   "
line3 = "`ssh 192.168.1.2 df -h Volume2 |head -2 | tail -1 | awk '{ print $1,$2 }'`   "
line4 = "`ssh 192.168.1.2 df -h Volume3 |head -2 | tail -1 | awk '{ print $1,$2 }'`   "

###########  append variable value in line 1, 2, 3, 4
sed -i -e "1s%$%\t$line1%" volgrow
sed -i -e "2s%$%$line2%" volgrow
sed -i -e "3s%$%$line3%" volgrow
sed -i -e "4s%$%$line4%" volgrow
###END#

관련 정보