'bash'에 해당되는 글 20건

  1. 2022.08.17 [Shell Script] bash script background run
  2. 2022.05.24 [Shell] Bash String Split - tr
  3. 2022.03.14 [Shell] Bash String Replacement.
  4. 2021.12.30 [Bash] jq -r raw output (no quote)
  5. 2021.11.23 [cURL] cURL in Bash Script
  6. 2021.11.23 [Shell] Bash Random Number & Substring
  7. 2021.11.18 [Shell] for loop example
  8. 2021.07.27 [Bash] 문자열 비교 예제
  9. 2020.04.20 [Bash] Bash function, array parameters, str replace 등
  10. 2020.04.07 [Shell] Bash read, if then fi

[Shell Script] bash script background run

ITWeb/개발일반 2022. 8. 17. 19:21
$ bash run.sh 1>/dev/null 2>&1 &

$ bash run.sh >/dev/null 2>&1 & echo $! > run.pid

background 실행 및 pid 활용.

 

:

[Shell] Bash String Split - tr

ITWeb/개발일반 2022. 5. 24. 12:38

쉘 스크립트 내부에서 문자열을 구분자를 이용해서 분해 하는 방법입니다.

IFS, read 를 이용해서 하는 방법도 있으니 찾아 보시면 되겠습니다.

 

여기서는 tr 을 이용해서 하는 방법에 대해서 가볍게 작성 합니다.

TARGET="1,2,3,4,5"
targets=($(echo $TAGET | tr "," "\n"))
targetSize=${#targets[@]}

for i in "${targets[@]}"
do
  echo $i
done

코드에서 가끔 실수 하는 부분은 

- ($(echo $TARGET | tr "," "\n")) 에서  괄호가 두 번 사용 된다는 건데 가끔 제일 앞에 괄호를 잊고 사용 할 때가 있습니다.

 

:

[Shell] Bash String Replacement.

ITWeb/개발일반 2022. 3. 14. 12:14

docker-compose.yml 내 .env 를 이용한 활용이 안되는 경우 아래 처럼 그냥 string replacement 를 통해서 처리 할 수도 있습니다.

bash 에서  array, loop, replacement 에 대한 예제로 작성해 둡니다.

 

SERVICE_NAME='{{SERVICE_NAME}}=escs'
VERSION='{{VERSION}}=8.0.0'
CONTAINER_NAME='{{CONTAINER_NAME}}=escs-c'

DOCKER_VAR=( $SERVICE_NAME $VERSION $CONTAINER_NAME )

for var in "${DOCKER_VAR[@]}"; do
    kvs=($(echo $var | tr "=" "\n"))
    # linux
    sed -i "s/${kvs[0]}/${kvs[1]}/g" docker-compose.yml
    # osx
    sed -i '' "s/${kvs[0]}/${kvs[1]}/g" docker-compose.yml
done

 

:

[Bash] jq -r raw output (no quote)

ITWeb/개발일반 2021. 12. 30. 16:25

이런 것도 기억력 도움을 위해서 그냥 한 줄 남겨 봅니다.

 

$ jq .hits.total.relation 하면 

Output)

"eq"

이렇게 나오는데 저는 저 quotation 이 싫습니다.

 

그래서 아래 옵션 주고 하시면 됩니다.

 

$ jq -r .hits.total.relation

Output)

eq

 

:

[cURL] cURL in Bash Script

ITWeb/개발일반 2021. 11. 23. 15:35

Case 1)
response=`curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": 100, "message": "test" }'`

Case 2)
curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": 100, "message": "test" }'

Case 3)
size=100
message="test"

response=`curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": '$size', "message": "'"$message"'" }'`

Case 4)
size=100
message="test"

curl -s -X GET 'http://www.kakaopay.com' -H 'Content-Type: application/json' -d '{ "size": '$size', "message": "'"$message"'" }'

proxy 를 지정 하고자 한다면 아래 내용 추가 합니다.
curl -x 'http://PROXY-IP:PROXY-PORT' -s -X GET 'http://www.kakaopay.com'

 

:

[Shell] Bash Random Number & Substring

ITWeb/개발일반 2021. 11. 23. 15:33

이런것도 기억력을 돕기 위해 기록해 봅니다.

 

- Bash Random Number
$ echo $(( $RANDOM % 4 ))

 

nodes=( '1' '2' '3' '4' )

i=$(( $RANDOM % 4 ))

node=${nodes[${i}]}

- Bash Substring
${String:Position:length} 문자:인덱스:길이

 

:

[Shell] for loop example

ITWeb/개발일반 2021. 11. 18. 11:25

간혹 서버간 ssh tunneling 을 위해 pub key 를 등록 해야 할 일이 있습니다.

노가다 하기 귀찮으니까 instance 목록만 가지고 쭈욱 돌리면 되겠죠.

 

#!/bin/bash

PUB="abcdefg"
HOSTS=("ip1" "ip2" "ip3" ...)

for host in "${HOSTS[@]}"
do
  echo "ssh -o StrictHostKeychecking=no USER@$host \"echo '$PUB' >> .ssh/authorized_keys\""
  ssh -o StrictHostKeychecking=no USER@$host "echo '$PUB' >> .ssh/authorized_keys"
done

이런것도 맨날 기억을 못해서 기록을 합니다.

:

[Bash] 문자열 비교 예제

ITWeb/개발일반 2021. 7. 27. 08:11
#!/bin/bash

retry='retry'

if [ $retry = "retry" ]; then
  echo 'true - 1'
fi

if [[ $retry = "retry" ]]; then
  echo 'true - 2'
fi

if [[ "$retry" = "retry" ]]; then
  echo 'true - 3'
fi

세 예제 다 true 리턴 합니다.

:

[Bash] Bash function, array parameters, str replace 등

ITWeb/개발일반 2020. 4. 20. 11:18

bash script 에서 function 사용은 호출 위치 보다 위에 function 선언이 되어 있어야 합니다.

#!/usr/bin/env bash

function 함수명() {
...
}

## 예제

#!/usr/bin/env bash

function helloWorld() {
  echo 'Hello World!!'
}

helloWorld

 

function  에 다중 array paramters 를 넘기는 예제는 아래와 같습니다.

#!/usr/bin/env bash

function deployElasticStack() {
  local instances=($1)
  local targets=($2)

  for instance in ${instances[@]}
  do

    local hostIpUser=($(echo $instance | tr ":" "\n"))

    for target in ${targets[@]}
    do
...중략...
    done
  done
}

selfHost=$(hostname -I|cut -f1 -d ' ')

instanceArr=("xxx.xxx.xxx.xxx:ubuntu" "xxx.xxx.xxx.xxx:ec2-user" "xxx.xxx.xxx.xxx:ubuntu")
metricbeatArr=("xxx.xxx.xxx.xxx" "xxx.xxx.xxx.xxx" "xxx.xxx.xxx.xxx")

deployElasticStack "${instanceArr[*]}" "${metricbeatArr[*]}" "$selfHost" "metricbeat"

 

해당 장비의 IP 정보를 가져 오는 예제는 아래와 같습니다.

$ hostname -I

$ hostname -I|cut -f1 -d ' '

$ ip route get 8.8.8.8 | sed -n '/src/{s/.*src *\([^ ]*\).*/\1/p;q}'

 

file 내 특정 문자열을 치환 하는 예제는 아래와 같습니다.

$ sed -i "s/소스문자열/치환문자열/g" configuration.yml

# osx 에서는 아래와 같이 합니다.
$ sed -i "" "s/소스문자열/치환문자열/g" configuration.yml

 

:

[Shell] Bash read, if then fi

ITWeb/개발일반 2020. 4. 7. 20:07
#!/usr/bin/env bash

pwd

echo "Are you sure current directory? (Y/N)"
read answer

if [ "$answer" == "y" ] || [ "$answer" == "Y" ]
then
  echo "Yes!!"
fi

echo "done!!"
#!/usr/bin/env bash

pwd

echo "Are you sure current directory? (Y/N)"
read ans
answer=`echo $ans| tr a-z A-Z`

if [ "$answer" == "Y" ]
then
  echo "Yes!!"
fi

echo "done!!"

 

: