Tuesday, 30 June 2020

BASH associative array printing

printf "%s\n" "${!array[@]}"
a2
a1
f50
zz
b1

printf "%s\n" "${array[@]}"
2
1
abcd
Hello World
bbb

printf "%s\n" "${!array[@]}" "${array[@]}" | pr -2t
a2                              2
a1                              1
f50                             abcd
zz                              Hello World
b1                              bbb

from : https://unix.stackexchange.com/questions/366581/bash-associative-array-printing

Tuesday, 23 June 2020

Run jq in docker

# Get Slack_name from a.json
declare -A buildUserId2SlackNameDic
while IFS='|' read -r key value; do
  key=$(echo $key | tr -d '"')
  value=$(echo $value | tr -d '"')
  if test ! -z "$value" ; then
  buildUserId2SlackNameDic[$key]=$value
  fi
done< <(docker run -i stedolan/jq <a.json '.[] | "\(.user)|\(.Slack_name)"')

# Get SLACK_USER
if test "${buildUserId2SlackNameDic[$SLACK_USER_ID]+isset}" ; then
    SLACK_USER=${buildUserId2SlackNameDic[$SLACK_USER_ID]}
else
    SLACK_USER=${BUILD_USER_FIRST_NAME,,}
fi

Convert json array of objects to bash associative array

{
"Parameters": [
    {
        "Name": "/path/user_management/api_key",
        "Type": "SecureString",
        "Value": "1234",
        "Version": 1
    },
    {
        "Name": "/path/user_management/api_secret",
        "Type": "SecureString",
        "Value": "5678",
        "Version": 1
    }
]
}

#!/usr/bin/env bash
# declare an associative array, the -A defines the array of this type
declare -A _my_Array

# The output of jq is separated by '|' so that we have a valid delimiter
# to read our keys and values. The read command processes one line at a 
# time and puts the values in the variables 'key' and 'value'
while IFS='|' read -r key value; do
    # Strip out the text until the last occurrence of '/' 
    strippedKey="${key##*/}"
    # Putting the key/value pair in the array
    _my_Array["$strippedKey"]="$value"
done< <(jq -r '.Parameters[] | "\(.Name)|\(.Value)"' json)

# Print the array using the '-p' or do one by one
declare -p _my_Array

Print array elements on separate lines in Bash?

rintf '%s\n' "${my_array[@]}"

from : https://stackoverflow.com/questions/15691942/print-array-elements-on-separate-lines-in-bash

Easiest way to check for an index or a key in an array?

if test "${myArray['key_or_index']+isset}"
    then
        echo "yes"
    else
        echo "no"
fi;

from : https://stackoverflow.com/questions/13219634/easiest-way-to-check-for-an-index-or-a-key-in-an-array

Sunday, 21 June 2020

How to set current working directory to the directory of the script?

#!/bin/bash
cd "$(dirname "$0")"

How do you store a list of directories into an array in Bash (and then print them out)?

#! /bin/bash

declare -a dirs
i=1
for d in */
do
    dirs[i++]="${d%/}"
done
echo "There are ${#dirs[@]} dirs in the current path"
for((i=1;i<=${#dirs[@]};i++))
do
    echo $i "${dirs[i]}"
done
echo "which dir do you want?"
echo -n "> "
read i
echo "you selected ${dirs[$i]}"

Another way:
$ awk -F= '!a[$1]++' first.properties second.properties
The input to this awk is the content of first file followed by second file. !a[$1]++ prints only the first occurence of a particular key, hence removing duplicates apparing in the 2nd file.

from : https://stackoverflow.com/questions/14017577/merge-two-properties-file-using-shell-scripts

Tuesday, 16 June 2020

Loop through an array of strings in Bash?

You can use it like this:
## declare an array variable
declare -a arr=("element1" "element2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also
Also works for multi-line array declaration
declare -a arr=("element1" 
                "element2" "element3"
                "element4"

from : https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash

How to check if a file contains a specific string using Bash

 In case if you want to check whether file does not contain a specific string, you can do it as follows. if ! grep -q SomeString "$File...