0

I have an environment variable:

export qua="key={1234}"

How do I call $qua from within a Bash script that is running a Chocolatey command?

#!/bin/bash
salt computername chocolatey.install qualys install_args='"\"$qua\""'

I cannot get Chocolatey to read the environment variable properly. I have also tried:

install_args='"$qua"'

and

install_args=$qua

The command works if I run:

#!/bin/bash
salt computername chocolatey.install qualys install_args="key={1234}"
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Kade Williams
  • 1,041
  • 2
  • 13
  • 28
  • Doesn't `install_args="$qua"` work? How are you running the script? – Benjamin W. May 27 '20 at 18:57
  • No, that doesn't work either. I'm running it like "sudo ./script.sh" and "sudo bash ./script.sh" I think it has something to do with Saltstack actually. – Kade Williams May 27 '20 at 19:12
  • Well, `sudo` runs in a completely different environment (compare `env` and `sudo env`). You could add the value as a parameter: call `sudo ./script.sh "$qua"`, and within the script use `install_args="$1"`. – Benjamin W. May 27 '20 at 19:24
  • I'm actually doing that. I'm just stumped on getting it to work without passing an argument. – Kade Williams May 27 '20 at 19:38
  • The purpose of `sudo` is to allow a regular user do administrative tasks like starting/stopping services, installing/updating software etc. **Do not** do your daily tasks using `sudo` and **do not** do development using `sudo`; you don't need `sudo` for such things. More than for not helping with anything, using `sudo` for non-administrative tasks produces a lot of harm. Once you started using `sudo`, all the files you create with it are owned by `root` and changing or deleting them requires using `sudo` again. It's a never ending spiral that grows bigger and bigger. – axiac May 27 '20 at 20:14
  • I'm currently only using sudo for SaltStack. "While the default setup runs the master and minion as the root user, some may consider it an extra measure of security to run the master as a non-root user. Keep in mind that doing so does not change the master's capability to access minions as the user they are running as. Due to this many feel that running the master as a non-root user does not grant any real security advantage which is why the master has remained as root by default." https://docs.saltstack.com/en/latest/ref/configuration/nonroot.html – Kade Williams May 28 '20 at 19:12

1 Answers1

0

As somebody pointed out, sudo creates a new environment that does not retain your exported variables. Observe:

$ export this=that
$ env | grep this
this=that
$ sudo env | grep this
$

To pass the variable through, you need something like:

$ sudo env this=$this | grep this
SUDO_COMMAND=/usr/bin/env this=that
this=that

So in your case, your script could be something like:

#!/bin/bash
salt computername chocolatey.install qualys install_args="$ARG"

without extra quotes. And you would call it like:

sudo env ARG="$qua" ./script.sh
SiKing
  • 10,003
  • 10
  • 39
  • 90