I am trying to use ~/Development/Project in a YAML file but it doesn't get expanded to /Users/revolt/Development/Project:
repos:
- repo: ~/Development/Project
can it somehow access the bash's $HOME variable?
As #tinita mentioned here, YAML is just a serialization language, so it's not possible.
Functionality like this must be implemented in the software that uses the respective file.
Related
The real case:
Every time I set up my environment, I'd like to check and – create if are not existing – certain environment variables. So, instead of doing it manually all the time, I thought it would be great if I can have a file which stores the environment name env_N and environment value env_V pairs. Amongst all text file formats, the yaml looks the simplest and the more natural to store that info.
So what I thought it would be great if I suck in the yaml file with my environmental variables using yq and create associative array ready to be iterated over by zsh foreach loop:
foreach entry in my_assoc_arr
do
check_and_create(entry.env_N, entry.env_V)
done
with the final result of:
$ echo $env_N1
env_V1
$ echo $env_N2
env_V2
$ echo $env_N3
env_V3
...
The problem I'm having is to get my yaml to associative array using yq in shell zsh script.
After applying each suggestion from the comments, I was unable to create my associative array from yaml with yq. I had errors from yq like bad syntax or script worked or not depends on whether I have #!/bin/zsh switched on or commented out.
I got impression that my task is simple, but somehow I cant achieve this.
What I'm doing wrong here?
PS: I'm using zsh on macOS
Why would you want to use Yaml for this, when it’s much easier to write a simple shell script for it?
Simply create a text file with the following line for each env var:
# Set $FOO to 'bar' if $FOO does not yet exist.
export ${FOO=bar}
Then source this file from your shell (or another script).
See https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion
I'd like to read a yaml file and find a specific variable, then store that variable's value.
So it's something like:
- variable1:
variable2:
- "value"
variable3:
- ...
and I want to grab variable2's value, which also happens to be a file path. I'd like to be taken to that file path as well, so using the value to locate the value at that file path. Is that possible in shell script without a 3rd party library/plugin?
If not, what's the best approach to act on a yaml config with this objective?
Any help would be highly appreciated. Cheers!
Only way to parse yaml with shell could be that you need to implement your own script to parse yaml. Following is a helpful blog.
https://linuxhint.com/parse-yaml-file-bash/
I have a set of text files and a set of GoLang files. The GoLang files contain directives such as the following:
//go:embed hello.txt
var s string
I want to write a bash script which takes the above code and substitutes the following in its place:
var s string = "<contents of hello.txt>"
Specifically, I want to bash script to go through all GoLang source files and replace all go:embed/string declaration pairs with a string defined to be the contents of the file specified in the embed directive.
I'm wondering if there is an existing program which can be configured to do the above. Otherwise, I'm planning on writing the algorithm myself.
Further explaination:
I am trying to replicate GoLang's embed directive (https://tip.golang.org/pkg/embed/).
We are not yet on GoLang 1.16, so we cannot use this functionality, but we are replicating it as closely as possible so that moving over to the standard implementation is as painless as possible.
Below is an attempt at solving your problem:
for i in file1 file2; do
awk '/^\/\/go:embed /{f=$2;next}/^var/&&f{printf"%s = \"",$0;system("cat "f);print"\"";f=0;next}1' < "$i" > "$i.new"
done
The awk script prints all normal lines, only if it encounters the embed directive this line will be skipped (and the file name remembered in variable f). A subsequent line starting with var will then be extended by the content of the file with the remembered name (using the system call "cat").
Beware, there are no error checks at all, no attempt to fix quotes and whatever. So for practical use - unless the file contents you are about to embed are known to be good-natured - you probably have to take a more sophisticated approach.
I use bash to configure many of my build tools.
I want to use variables to set certain things so that the build tools can be used in many environments.
Currently I do
exports var=val
and then I do
$var
when I need to use it.
Is this the best way to go about it, as I know there are many ways to do things in bash.
**Example**
#!/bin/bash
path_bash="$HOME/root/config/bash/"
source "${path_bash}_private.sh"
source "${path_bash}config.sh"
source "${path_bash}utility.sh"
source "${path_bash}workflow.sh"
source "${path_bash}net.sh"
source "${path_bash}makeHTM.sh"
#
#
#
# Divider - Commands
#
#
#
cd ~/root
Skip the export unless you really need it (that is, unless you need that variable to propagate to unrelated (=execed) processes that you execute).
If you do export, it's usually a good idea to capitalize the variable (export VAR=val) to make it apparent that it will spread to executed binaries.
When you refer to a shell variable, you usually want to double quote it ("$var") unless you need glob expansion and whitespace-splitting (splitting on $IFS characters, to be exact) done on that variable expansion.
sparrow automation framework gives you an opportunity to configure bash scripts in many ways :
passing command line named parameters
configuration files in yaml format
configuration files in json format
configuration files in config::general format
Of course you need to pack your bash scripts into sparrow plugins first to get this behavior , but this is not a big deal . Follow https://github.com/melezhik/sparrow#configuring-tasks for details .
PS. disclosure - I am the sparrow tool author .
as the title say, I have many variables, like 200, and I'd like to have a script just containing the declarations, appart from my bash with execute code. Would it be possible with my bash that execute code to just call a script that create the variable and that exist after?
EDIT : The site proposed that answer the questions explain the same situation, however there are really good details the people who answered here gave.
When the declarations are in /usr/local/lib/myvars, start your script (after the SHEBANG line) with sourcing that file using the dot notation:
. /usr/local/lib/myvars
When you have so much vars, you must have a lot of code and some general functions. Put those in one file and include that one:
. /usr/local/lib/my_utils
And know you might be wondering: 2 includes in every scriptfile? No, you can source the myvars file in the my_utils file.
Be aware you are introducing global variables, they can be changed everywhere.
You can export the variables to be available externally and source that file.
Example. Contents of variables.sh
export VARIABLE1=Value1
export VARIABLE2=Value2
.
.
export VARIABLE200=Value200
Contents of main script:
#!/bin/bash
source /Pathtosourcefile/variables.sh
echo $VARIABLE1
This would print out:
Value1