Get value from yaml array in bash [duplicate] - bash

This question already has answers here:
How can I parse a YAML file from a Linux shell script?
(23 answers)
Closed 3 years ago.
Is there a "simple" way (without functions or packages) to get the value of a YAML file in a terminal (bash/sh)?
I wanna extract the value of users -> user("kube-admin-local") -> client-certificate-data
This is the YAML example:
users:
- name: "kube-admin-local"
user:
client-certificate-data: 0FURS0tLS0tCk1JSUM2VENDQWRHZ0F3SUJBZ0lJT2wyZ0NHL1BnTWd3RFFZSktvWklodmNOQVFFTEJRQ
- name: kube-admin
user:
client-certificate-data: LS0tLS1CRUd=0FURS0tLS0tCk1JSUM2VENDQWRHZ0F3SUJBZ0lJT2wyZ0NHL1BnTWd3RFFZSktvWklodmNOQVFFTEJ

This won't work in all systems, but
sed -n '/name: "kube-admin-local"/,/name:/s/.*client-certificate-data: \(.*\)/\1/p'
Should do it.
-n: don't print unless explicitly stated
/name: "kube-admin-local"/,/name:/: lines between those matches
rest : substitute data and print

Related

How to get keyword args in Makefile script? [duplicate]

This question already has answers here:
Makefile - Get arguments passed to make
(2 answers)
Closed 4 months ago.
Is there any way to get key word args in Makefile ?
i have tried giving args and accessing using $1, $2 etc, but how can we give it with a keywords.
You can directly call just like env vars:
file syntax:
build:
cat $(file)
command to run:
make file=my_file.txt

Bash: adding a key/value pair to an existing config file [duplicate]

This question already has answers here:
How to append output to the end of a text file
(13 answers)
Closed 8 months ago.
I have an existing config config.ini file with the following content:
VALUE_A=a
VALUE_B=b
Using Bash, I'd like to add a new key-value pair VALUE_C=c to get the following:
VALUE_A=a
VALUE_B=b
VALUE_C=c
Is there a concise way to do this with Bash (ideally a one liner)?
As suggested in the comments, the answer is simply:
echo VALUE_C=c >> config.ini

Append to a list in a JSON document from bash [duplicate]

This question already has answers here:
how to add json object to json file using shell script
(2 answers)
Closed 2 years ago.
I have a file static-nodes.json, which has the following content >>>
[
"enode://70399c3d1654c959a02b73acbdd4770109e39573a27a9b52bd391e5f79b91a42d8f2b9e982959402a97d2cbcb5656d778ba8661ec97909abc72e7bb04392ebd8#127.0.0.1:21000?discport=0&raftport=50000",
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
]
I want to add another enode >>>
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50002"
But when I am trying to append it using the following command—>>>>>
Echo "enode to be appended " >> static-nodes.json
It is appended outside of the brackets as shown below
[
"enode://70399c3d1654c959a02b73acbdd4770109e39573a27a9b52bd391e5f79b91a42d8f2b9e982959402a97d2cbcb5656d778ba8661ec97909abc72e7bb04392ebd8#127.0.0.1:21000?discport=0&raftport=50000",
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
]
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
How should I fix this.?
Please have a look at
how to add json object to json file using shell script
what you can do it
Option 1:
create a temp json file using and then merge the temp json to nodes-json
toBeAppended = "enode to be appended"
echo "[ $toBeAppended ]" > tempNodes.json
jq -s add tempNodes.json static-nodes.json
Option 2. go with sed, awk, grep utilities

How to use wildcard with variable [duplicate]

This question already has answers here:
Ansible Command module says that '|' is illegal character
(2 answers)
Closed 5 years ago.
I want to list the files as per the host name.But problem is i not able to use the wildcard with variable properly.Can someone suggest me on this.
---
- hosts: local
become_user: yes
vars:
filename: /root/stuff
tasks:
- name: list files
action: command ls -lrt {{ filename }}/'*{{ansible_hostname}}'
register: listfiles
- debug: var=listfiles
If your question is why * doesn't expand?, then:
command module:
The command module takes the command name followed by a list of space-delimited arguments. The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work
shell module:
The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.
So if you need any shell tricks, like wildcard expansion or access to environment variables, use shell module.

Put pipe value into variable [duplicate]

This question already has an answer here:
How to redirect grep output to a variable?
(1 answer)
Closed 8 years ago.
I'm new in bash scripting world...
I'm trying to get value from pipe action to a var.
Something like:
result = $(ls /usr/bin | dmenu)
the idea is put files list into a standar menu (dmenu) so, when user select
a choice, i want to know wich one is selected and work with this option
to for example, execute a file.
the $result is not getting any value.
Thanks for your help
Remove whitespaces before and after =:
result=$(ls /usr/bin | dmenu)

Resources