bash script

변수 특정 글자 제거

${변수%제거할문자}

#!/bin/bash

# Call this as:
#   ./test.sh one/ two/ three/ 
#
# Output:
#  one two three

echo ${@%/}
$ for i in */; do echo ${i%/}; done

배열 다루기

#!/bin/bash
VAR=(
a
b
c
d
e
)

for i in ${VAR[*]}
do
	echo $i;
done

Swich case Loop

특정 작업 처리용 스크립트로서 입력을 받아 switch문으로 구분된 각각의 작업을 처리하며 현재 진행된 상태 표시 기능포함

#!/bin/bash

CURR="0"
while true; do

    echo "########################";
    echo -n "1) bootstrap"; if [ $CURR == 1 ]; then echo -n " <== Current"; fi; echo "";
    echo -n "2) precheck"; if [ $CURR == 2 ]; then echo -n " <== Current"; fi; echo "";
    echo -n "3) deploy"; if [ $CURR == 3 ]; then echo -n " <== Current"; fi; echo "";
    echo -n "4) destroy"; if [ $CURR == 4 ]; then echo -n " <== Current"; fi; echo "";
    echo -n "5) purge images"; if [ $CURR == 5 ]; then echo -n " <== Current"; fi; echo "";
    echo -n "6) reboot nodes"; if [ $CURR == 6 ]; then echo -n " <== Current"; fi; echo "";
    echo "exit) quit";
    echo "########################";
    echo "Choice) ";

    read x
    CURR=$x;
    case $x in
        exit) break ;;

        1) echo "Bootstraping...";
        kolla-ansible -i ./multinode bootstrap-servers;
        ;;

        2) echo "Prechecking...";
        kolla-ansible -i ./multinode prechecks;
        ;;

        3) echo "Deploying...";
        kolla-ansible -i ./multinode deploy;
        ;;

        4) echo "Destroying..";
        kolla-ansible -i ./multinode destroy --yes-i-really-really-mean-it;
        ;;

        5) echo "Purging images..";
        ansible -m shell -a 'docker rmi $(docker images -q)' -i multinode all;
        ;;

	6) echo "Reboot nodes..";
	ansible -m shell -a 'sync;reboot' -i multinode control;
	ansible -m shell -a 'sync;reboot' -i multinode compute;
	;;

        *) echo "Unknown response, enter a number or type 'exit' to quit" ;;
    esac
done