# execコマンド

## `exec` コマンド <a href="#exec-command-1" id="exec-command-1"></a>

テンプレート化した環境変数を、実行時にシークレットの値へ置き換えるには、**`ksm exec`** コマンドを使用します。

**パラメータ:**

置き換えられた環境変数を使用して、実行するシステムコールまたはスクリプト

形式: **`ksm exec -- <SYSTEM CALL OR SCRIPT>`**

#### Linux bashスクリプト例 <a href="#example-linux-bash-script" id="example-linux-bash-script"></a>

{% code title="my\_script.sh" %}

```bash
#!/bin/bash

# Keeperシークレットを取得するBashスクリプト 

connect_db() {
  echo "Database Password:" $DB_PASSWORD
}

call_stripe() {
  echo "API Key:" $API_KEY
}

connect_db
call_stripe
```

{% endcode %}

2つの環境変数を設定して、スクリプトを実行します。

```bash
$ export DB_PASSWORD="keeper://XXX/field/password"
$ export API_KEY="keeper://XXX/custom_field/API Key"

$ ksm exec -- ./my_script.sh

Database Password: ksv33110sbnb7W@b3VGCHb
API Key: sk_test_MY2A30Ofg6Ukkq2NjMQVo87c
```

#### Windowsバッチファイルの例 <a href="#example-windows-batch-file" id="example-windows-batch-file"></a>

```
C:\> ksm exec -- my_script.bat
```

#### PowerShellスクリプトの例 <a href="#example-powershell-script" id="example-powershell-script"></a>

```
PS C:\> ksm exec -- powershell my_script.ps1
```

## 環境変数の置換 <a href="#environment-variable-replacement" id="environment-variable-replacement"></a>

環境変数は、以下のようにCLIが正しく検索および置換できるようにテンプレート化する必要があります。

```bash
export DB_PASSWORD="keeper://XXX/field/password"
export API_KEY="keeper://XXX/custom_field/API Key"
```

{% hint style="info" %}
表記法を使用したクエリの形式と機能の詳細について、[Keeper表記法のページ](/keeperpam/jp/secrets-manager/about/keeper-notation.md)をご参照ください。
{% endhint %}

## シェルスクリプトの例 <a href="#example-shell-script" id="example-shell-script"></a>

以下はLinuxのbashスクリプトの例です。 アプリケーションを起動する前、またはコマンドを実行する前に、**`keeper://`** で始まる環境変数は、ボルトのシークレット値に置き換えられます。XXXXは、シークレットのレコードUIDに置き換えてください。

```
export MY_PASSWORD=keeper://XXXX/field/password
export MY_OTHER_PASSWORD=keeper://XXXX/field/password[0]
export MY_LAST_NAME=keeper://XXXX/custom_field/Name 2[last]
export MY_SECOND_PHONE=keeper://XXXX/custom_field/phone[1][number]
```

以下は、シークレットをコンソールに出力する簡単なbashスクリプトです。

```
#!/usr/bin/env bash
echo
echo "My Password = \${MY_PASSWORD}"
echo "Other Password = \${MY_OTHER_PASSWORD}"
echo "My Last Name = \${MY_LAST_NAME}"
echo "My Second Phone = \${MY_SECOND_PHONE}"
```

以下は、**`ksm exec`** を使用してbashスクリプトを実行した結果です。

```
$ ksm exec -- ./my_script.sh

My Password = $71387feh24fE%4416ffFHA
Other Password = YYFash328f^F^@#Fsdfjhsgblqef'f;
My Last Name = Smith
My Second Phone = 123-456-7890
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keeper.io/keeperpam/jp/secrets-manager/secrets-manager-command-line-interface/exec-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
