他のコマンドでコマンド出力を使用する

他のコマンドでコマンド出力を使用する

私はLinuxを初めて使用し、次のスクリプトを書くのに助けが必要です。

  1. コマンドを実行して出力(例:ID#、

    docker exec -it scalelite-api bin/rake servers:add[https://bbb1.test.com/bigbluebutton/api,XYZ1234]
    

    これによりID#が返されます。

  2. 次に、出力ID#をスクリプトの他の行に挿入したいと思います。

    docker exec -it scalelite-api bin/rake servers:enable[ID#]
    

ベストアンサー1

XYZ1234 が実数値ではないと仮定します。

#!/bin/bash

# myscript.sh

# ${1} denotes the first argument of the shell script
export ID=$(docker exec -it scalelite-api bin/rake "servers:add[https://bbb1.test.com/bigbluebutton/api,${1}]")
# Puts the value of the command output into environment variable "ID"

echo "$ID"
# Outputs the value of environment variable ID
$ chmod +x myscript.sh
$ ./myscript.sh "XYZ1234"

もちろん、後でチェックを追加して、dockerコマンドが失敗したかどうかを確認できます。

おすすめ記事