Create a gist now

Instantly share code, notes, and snippets.

@kongchen /setprop.sh
Last active Jul 30, 2018

Embed
What would you like to do?
set properties file value by key via bash shell
#!/bin/bash
############################
#script function
############################
setProperty(){
awk -v pat="^$1=" -v value="$1=$2" '{ if ($0 ~ pat) print value; else print $0; }' $3 > $3.tmp
mv $3.tmp $3
}
############################
### usage: setProperty $key $value $filename
setProperty "store.folder" "/opt/current/store" "env.properties"
@MateusGabi

This comment has been minimized.

Show comment
Hide comment
@MateusGabi

MateusGabi Feb 17, 2018

Thank You!! It works very good!

MateusGabi commented Feb 17, 2018

Thank You!! It works very good!

@iyerusad

This comment has been minimized.

Show comment
Hide comment
@iyerusad

iyerusad Apr 26, 2018

A horrid "enhancement":

cat << 'EOF' > /usr/local/bin/setProperty
#!/bin/bash
#version: 0.1
### usage: setProperty key value filename

if [ -z "$1" ]; then
  echo "No parameters provided, exiting..."
  exit 1
fi
if [ -z "$2" ]; then
  echo "Key provided, but no value, breaking"
  exit 1
fi
if [ -z "$3" ] && [ -z "$setPropertyFile" ]; then
  echo "No file provided or setPropertyFile is not set, exiting..."
  exit 1
fi

if [ "$setPropertyFile" ] && [ "$3" ]; then
    echo "setPropertyFile variable is set AND filename in comamnd! Use only or the other. Exiting..."
    exit 1
else
  if [ "$3" ] && [ ! -f "$3" ]; then
    echo "File in command NOT FOUND!"
    exit 1
  elif [ "$setPropertyFile" ] && [ ! -f "$setPropertyFile" ]; then
    echo "File in setPropertyFile variable NOT FOUND!"
    exit 1
  fi
fi

if [ "$setPropertyFile" ]; then
  file=$setPropertyFile
else
  file=$3
fi

awk -v pat="^$1=" -v value="$1=$2" '{ if ($0 ~ pat) print value; else print $0; }' "$file" > "$file".tmp
mv "$file".tmp "$file"

EOF

chmod +x /usr/local/bin/setProperty

Usage:

setProperty key value file

or

export setPropertyFile="/tmp/testfile.config"
setProperty key1 value1
setProperty key2 value2
setProperty key2 value2

iyerusad commented Apr 26, 2018

A horrid "enhancement":

cat << 'EOF' > /usr/local/bin/setProperty
#!/bin/bash
#version: 0.1
### usage: setProperty key value filename

if [ -z "$1" ]; then
  echo "No parameters provided, exiting..."
  exit 1
fi
if [ -z "$2" ]; then
  echo "Key provided, but no value, breaking"
  exit 1
fi
if [ -z "$3" ] && [ -z "$setPropertyFile" ]; then
  echo "No file provided or setPropertyFile is not set, exiting..."
  exit 1
fi

if [ "$setPropertyFile" ] && [ "$3" ]; then
    echo "setPropertyFile variable is set AND filename in comamnd! Use only or the other. Exiting..."
    exit 1
else
  if [ "$3" ] && [ ! -f "$3" ]; then
    echo "File in command NOT FOUND!"
    exit 1
  elif [ "$setPropertyFile" ] && [ ! -f "$setPropertyFile" ]; then
    echo "File in setPropertyFile variable NOT FOUND!"
    exit 1
  fi
fi

if [ "$setPropertyFile" ]; then
  file=$setPropertyFile
else
  file=$3
fi

awk -v pat="^$1=" -v value="$1=$2" '{ if ($0 ~ pat) print value; else print $0; }' "$file" > "$file".tmp
mv "$file".tmp "$file"

EOF

chmod +x /usr/local/bin/setProperty

Usage:

setProperty key value file

or

export setPropertyFile="/tmp/testfile.config"
setProperty key1 value1
setProperty key2 value2
setProperty key2 value2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment