posted by 귀염둥이채원 2021. 4. 23. 01:15

expect는 다른 응용 어플리케이션과 상호대화(interactive)하는 프로그램을 만들기 위해 만들어진 프로그램이다.
expect를 이용하면 다른 어플리케이션과 상호대화를 할수 있게 됨으로 자동화된 프로그램을 만들수가 있다.

ssh 접속

#!/bin/sh
USER=testuser
IP=192.168.10.2
PW=testpw
expect <<EOF
set timeout 3
spawn ssh -o StrictHostKeyChecking=no $USER@$IP "hostname"
expect "password:"
    send "$PW\r"
expect eof
EOF

hue 신규 계정 생성

#!/bin/bash

sudo yum install -y expect

expect <<EOF
spawn /usr/lib/hue/build/env/bin/hue createsuperuser
expect "Username (leave blank to use 'hadoop'):" {send "hue\r"}
expect "Email address:" {send "hue@email.com\r"}
expect "Password" {send "Huereco1!\r"}
expect "Password (again)" {send "Huereco1!\r"}
expect eof
EOF

참고사이트

https://zetawiki.com/wiki/Bash_%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90_expect_%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8_%EB%84%A3%EA%B8%B0
https://must-thanks.tistory.com/156
https://zetawiki.com/wiki/%EB%A6%AC%EB%88%85%EC%8A%A4_expect
posted by 귀염둥이채원 2021. 4. 23. 00:51

IFS는 Internal Field Separator의 약자로 외부프로그램을 실행할 때 입력되는 문자열을 나눌 때 기준이 되는 문자를 정의하는 변수이다.
bash의 내부 필드 구분자(internal field separator)가 공백/탭/줄바꿈이다.
구분자는 IFS 변수에 설정되어 있다.

PROGRAMMING="
java programming
python programming
c programming
"

echo "-------------------------"
for p in $PROGRAMMING; do
echo $p
done

# IFS를 줄바꿈(newline)로 변경
echo "-------------------------"
OLD_IFS="$IFS"
IFS=$'\n'
for p in $PROGRAMMING; do
echo $p
done

IFS="$OLD_IFS"
$ bash test.sh
-------------------------
java
programming
python
programming
c
programming
-------------------------
java programming
python programming
c programming

# 참고사이트
https://m31phy.tistory.com/11
https://webterror.net/?p=1716
https://wooono.tistory.com/186

posted by 귀염둥이채원 2021. 4. 22. 19:44

시작날짜, 종료날짜를 입력해서 매주 일요일, 화요일만 수행하는 스크립트 샘플입니다.

 

CLUSTER_ID="5FFZMTA6C"
BATCH_START_DATE="20210228"
BATCH_END_DATE="20210420"

while true; do
  if [ $BATCH_START_DATE -gt $BATCH_END_DATE ]; then
      exit 0
  fi

  day_of_week=$(date -d "${BATCH_START_DATE}" "+%A")
  if [ $day_of_week == "Sunday" ] || [ $day_of_week == "Tuesday" ]; then
    DISP_START_DATE=$(date -d "${BATCH_START_DATE} +1days" "+%Y%m%d")
    DISP_END_DATE=$(date -d "${BATCH_START_DATE} +7days" "+%Y%m%d")
    echo "BATCH: $BATCH_START_DATE"
    echo "./backfill.sh $_CLUSTER_ID stat $DISP_START_DATE $DISP_END_DATE;"
  fi

  sleep 0.1
  BATCH_START_DATE=$(date -d "${BATCH_START_DATE} +1days" "+%Y%m%d")
done

 

$ bash test7.sh | grep backfill
./backfill.sh  stat 20210301 20210307;
./backfill.sh  stat 20210303 20210309;
./backfill.sh  stat 20210308 20210314;
./backfill.sh  stat 20210310 20210316;
./backfill.sh  stat 20210315 20210321;
./backfill.sh  stat 20210317 20210323;
./backfill.sh  stat 20210322 20210328;
./backfill.sh  stat 20210324 20210330;
./backfill.sh  stat 20210329 20210404;
./backfill.sh  stat 20210331 20210406;
./backfill.sh  stat 20210405 20210411;
./backfill.sh  stat 20210407 20210413;
./backfill.sh  stat 20210412 20210418;
./backfill.sh  stat 20210414 20210420;
./backfill.sh  stat 20210419 20210425;
./backfill.sh  stat 20210421 20210427;​