Skip to content

Commit

Permalink
Merge pull request #277 from silversword411/main
Browse files Browse the repository at this point in the history
Fixing scripting params and evn var examples
  • Loading branch information
silversword411 authored Aug 22, 2024
2 parents 88721ac + 43cfe31 commit 17404db
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
Binary file added docs/functions/images/script_args.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
95 changes: 78 additions & 17 deletions docs/functions/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ and `()` indicates a default parameter if none is specified.

Starting with 0.15.4 you can use environment variables to pass them too!

#### Environment Variables
#### Examples

Script Arguments

```
-hello world
-foo bar
-data {{agent.site.client.name}}
-data {{agent.public_ip}}
```

Expand All @@ -109,19 +108,48 @@ edata={{agent.hostname}}

```batch
@echo off
echo %hello%
echo %foo%
echo %data%
setlocal

rem Parameters. Batch only accepts positional params not named ones
set hello=%1
set foo=%2
set data=%3

echo Script Args
echo hello: %hello%
echo foo: %foo%
echo data: %data%

echo.
echo Environment Vars
echo ehello: %ehello%
echo efoo: %efoo%
echo edata: %edata%

endlocal
```

=== ":material-powershell: Powershell"

Script

```ps
Write-Output $env:hello
Write-Output $env:foo
Write-Output $env:data
param(
[string]$hello,
[string]$foo,
[string]$data
)

Write-Output "Script Args"
Write-Output "hello: $hello"
Write-Output "foo: $foo"
Write-Output "data: $data"

Write-Output ""
Write-Output "Environment Vars"
Write-Output "ehello: $env:ehello"
Write-Output "efoo: $env:efoo"
Write-Output "edata: $env:edata"
```

=== ":material-language-python: Python"
Expand All @@ -132,22 +160,55 @@ edata={{agent.hostname}}
#!/usr/bin/python3

import os

print(os.getenv("hello"))
print(os.getenv("foo"))
print(os.getenv("data"))
import argparse

#Note: named args in python require -- and = between name and value eg
# --hello=world
# --foo=bar
# --data={{agent.public_ip}}
# Parse command-line arguments
parser = argparse.ArgumentParser(description="Process some strings.")
parser.add_argument("--hello", type=str, help="Value for hello")
parser.add_argument("--foo", type=str, help="Value for foo")
parser.add_argument("--data", type=str, help="Value for data")

args = parser.parse_args()

# Script Args
print("Script Args")
print(f"hello: {args.hello}")
print(f"foo: {args.foo}")
print(f"data: {args.data}")

# Environment Vars
print("\nEnvironment Vars")
print(f"ehello: {os.getenv('ehello')}")
print(f"efoo: {os.getenv('efoo')}")
print(f"edata: {os.getenv('edata')}")
```

=== ":material-bash: Shell"

Script

```bash
#!/usr/bin/env bash

echo %hello%
echo %foo%
echo %data%
#!/bin/bash

#Bash only accepts positional params not named ones
hello="$1"
foo="$2"
data="$3"

echo "Script Args"
echo "hello: $hello"
echo "foo: $foo"
echo "data: $data"

echo ""
echo "Environment Vars"
echo "ehello: $ehello"
echo "efoo: $efoo"
echo "edata: $edata"
```

<div class="video-wrapper">
Expand Down

0 comments on commit 17404db

Please sign in to comment.