Bash変数の実行

Bash変数の実行

bashを学ぶために全力を尽くしています。この例では、パラメータなしでスクリプトを使用します。

restart_alfresco.sh
Connection to slql-fresc-bdd1 closed.

Usage: /root/bin/restart_alfresco.sh  status|start|stop|restart

問題は次のとおりです。

POSTGRES_STATUS=$(ssh -t root@slql-fresc-bdd1 "ps -f -u alfresco|grep postgres|grep -v UID")

処刑される。 $POSTGRES_STATUSを実行せずにどのように使用できますか?

これは私のスクリプトです。

FLOWER_STATUS="/etc/init.d/flowerGenesisPlugin status"
SOLR_STATUS=$(ps -f -u alfresco|grep -v UID)
ALFRESCO_STATUS=$(ps -f -u alfresco|grep Dalfresco|grep -v UID)
POSTGRES_STATUS=$(ssh -t root@slql-fresc-bdd1 "ps -f -u alfresco|grep postgres|grep -v UID")

usage() {
  # On display usage and exit.
  echo -e "\nUsage: ${0} \033[33m status|start|stop|restart\033[0m\n " >&2
  echo -e '\033[36m [status] \033[0m Permet de voir le status de alfresco, solr et flowergenesis' >&2
  echo -e '\033[36m [restart] \033[0m  Permet de restarter alfresco, solr et flowergenesis' >&2
  echo -e '\033[36m [stop] \033[0m  Permet de stopper solr, alfresco et flowergenesis' >&2
  echo -e '\033[36m [start] \033[0m  Permet de starter alfresco, solr et flowergenesis' >&2
  echo -e "\n"
}

status() {
echo -e "\n"
echo -e "$POSTGRES_STATUS"

if
        [[ -n "${POSTGRES_STATUS}" ]]
        then
        echo -e '\033[36m Postgres est bien start \033[0m' >&2
        else
        echo -e '\033[36m Postgres est eteint \033[0m' >&2
fi

echo -e "\n"
echo -e "$ALFRESCO_STATUS"
${ALFRESCO_STATUS}

if
        [[ -n "${ALFRESCO_STATUS}" ]]
        then
        echo -e '\033[36m Alfresco est bien start \033[0m' >&2
        else
        echo -e '\033[36m Alfresco est bien stop \033[0m' >&2
fi

echo -e "\n"
echo -e "$SOLR_STATUS"|grep -i --color=auto solr &&
${SOLR_STATUS}

if
        [[ -n "${SOLR_STATUS}" ]]
        then
        echo -e '\033[36m Solr est bien start \033[0m' >&2
        else
        echo -e '\033[36m Solr est arrete \033[0m' >&2
fi

echo -e "\n"
${FLOWER_STATUS}
FLOWER_EXIT_STATUS="${?}"

if
        [[ "${FLOWER_EXIT_STATUS}" -eq 0 ]]
        then
        echo -e '\033[36m flowerGenesisPlugin est bien start \033[0m' >&2
        else
        echo -e '\033[36m flowerGenesisPlugin est eteint \033[0m' >&2
fi
}


start() {
echo -e '\033[36m Demarrage de la Base Postgres en cours veuillez patientez \033[0m'
ssh -t root@slql-fresc-bdd1 "service postgres start" &&
service alfresco start &&
service solr start &&
/etc/init.d/flowerGenesisPlugin start
}

stop() {
service solr stop &&
service alfresco stop &&
/etc/init.d/flowerGenesisPlugin stop &&
echo -e "\033[36m Arret de la Base Postgres en cours veuillez patientez \033[0m"
ssh -t root@slql-fresc-bdd1 "service postgres stop"
}

case "$1" in
    status) status ;;
    start)   start ;;
    stop)    stop ;;
    restart) stop; start ;;
    *) usage >&2 ;;
       esac

if [[ "${UID}" -ne 0 ]]
then
  echo 'il faut executer ce script en tant que root' >&2
  usage
fi

# Expect the best
EXIT_STATUS='0'

if [[ "${EXIT_STATUS}" -ne 0 ]]
    then
      EXIT_STATUS=${EXIT_STATUS}
      echo "Execution du script a echoue." >&2
fi

ベストアンサー1

問題は、引数なしでスクリプトを実行するときにuse()関数のみを読みたいということです。

さて、スクリプトの構造とシェルが上記からスクリプトを実行していることを考えてみましょう。

FLOWER_STATUS="/etc/init.d/flowerGenesisPlugin status"
SOLR_STATUS=$(ps -f -u alfresco|grep -v UID)
ALFRESCO_STATUS=$(ps -f -u alfresco|grep Dalfresco|grep -v UID)
POSTGRES_STATUS=$(ssh -t root@slql-fresc-bdd1 "ps -f -u alfresco|grep postgres|grep -v UID")

usage() {
   ...
}

case "$1" in
    status) status ;;
    start)   start ;;
    stop)    stop ;;
    restart) stop; start ;;
    *) usage >&2 ;;
esac

台本の最初はPOSTGRES_STATUS友達に与える宿題だ。これにはコマンド置換が含まれます。割り当てるとき。 makeにあるように、シェルには怠惰な評価はありません。(装着したものを除いてeval、そこまで行かないでください。)

これを回避するには、パラメータチェックを一番上に移動し、割り当てをその下に移動します。

usage() {
   echo...
}

case "$1" in 
   ...
   *) usage; exit 1 ;;
esac

POSTGRES_STATUS=...

あるいは、関数に割り当てを入れて確認した後に関数を呼び出すこともできます。

usage() {
    echo...
}
check() {
    case "$1" in...
        *) usage; exit 1;;
    esac
}
set_globals() {
    POSTGRES_STATUS=...
}

check
set_globals

コードの主要部分を専用関数(main()通常は)に入れて最後に呼び出すこともできるので、関数定義以外のメインレベルにコードはありません。

おすすめ記事