Skip to content

Instantly share code, notes, and snippets.

@smileart
Last active January 2, 2022 14:36
Show Gist options
  • Save smileart/4597fa6a8afdf659d1d6 to your computer and use it in GitHub Desktop.
Save smileart/4597fa6a8afdf659d1d6 to your computer and use it in GitHub Desktop.
A little bash functions set to work with *.ini files
#!/usr/bin/env bash
# param_1: string | param_2: file
is_option_in_file () {
if [[ ! -z "$1" && ! -z "$2" ]]; then
egrep -q "$1" "$2"
if [[ $? = '0' ]]
then
echo '1'
else
echo '0'
fi
fi
}
# param_1: string | param_2: file
get_option_line_number () {
if [[ ! -z "$1" && ! -z "$2" ]]; then
echo $(egrep -n "$1" "$2" | cut -f1 -d:)
fi
}
# param_1: option | param_2: value | param_3: file
ini_set_option () {
local ini_file="$3";
local ini_option="^$1.*$";
local original_option="$1"
local new_option_value="$2"
if [ $( is_option_in_file "$ini_option" "$ini_file" ) = "1" ]; then
echo "Changed $1 value to $2 in the lines: " $(get_option_line_number "$ini_option" "$ini_file")
sed -i -e "s/$ini_option/$original_option = $new_option_value/g" $ini_file
else
echo "$original_option = $new_option_value " >> $ini_file
fi
}
# param_1: option | param_2: file
ini_comment_option () {
local ini_file="$2";
local ini_option="^$1.*$";
if [ $( is_option_in_file "$ini_option" "$ini_file" ) = "1" ]; then
echo "Commented $1 option in the lines: " $(get_option_line_number "$ini_option" "$ini_file")
sed -i -e "s/\($ini_option\)/;\1/g" $ini_file
fi
}
# param_1: option | param_2: file
ini_uncomment_option () {
local ini_file="$2";
local ini_option="^;$1.*$";
if [ $( is_option_in_file "$ini_option" "$ini_file" ) = "1" ]; then
echo "Uncommented $1 option in the lines: " $(get_option_line_number "$ini_option" "$ini_file")
sed -i -e "/$ini_option/ s/^;//" $ini_file
fi
}
# =============== Examples ================
# Set any options' values by name || add if they aren't exist
ini_set_option 'soap.wsdl_cache_ttl' '100500' '/etc/php5/cli/php.ini'
ini_set_option 'soap.wsdl_cache_ttl' '"Hey there!"' '/etc/php5/cli/php.ini'
read -t 20 -p " ⏎ "
# Escape any special chars if you think they could interfere
ini_set_option 'soap\.wsdl_cache_ttl' '86400' "/etc/php5/cli/php.ini"
read -t 20 -p " ⏎ "
# Comment & uncomment options by name
ini_comment_option 'soap.wsdl_cache_ttl' "/etc/php5/cli/php.ini"
read -t 20 -p " ⏎ "
ini_uncomment_option 'soap.wsdl_cache_ttl' "/etc/php5/cli/php.ini"
#!/usr/bin/env bash
# param_1: string | param_2: file
is_option_in_file () {
if [[ ! -z "$1" && ! -z "$2" ]]; then
sudo egrep -q "$1" "$2"
if [[ $? = '0' ]]
then
echo '1'
else
echo '0'
fi
fi
}
# param_1: string | param_2: file
get_option_line_number () {
if [[ ! -z "$1" && ! -z "$2" ]]; then
echo $(sudo egrep -n "$1" "$2" | sudo cut -f1 -d:)
fi
}
# param_1: option | param_2: value | param_3: file
ini_set_option () {
local ini_file="$3";
local ini_option="^$1.*$";
local original_option="$1"
local new_option_value="$2"
if [ $( is_option_in_file "$ini_option" "$ini_file" ) = "1" ]; then
echo "Changed $1 value to $2 in the lines: " $(get_option_line_number "$ini_option" "$ini_file")
sudo sed -i -e "s/$ini_option/$original_option = $new_option_value/g" $ini_file
else
echo "$original_option = $new_option_value " | sudo tee -a $ini_file
fi
}
# param_1: option | param_2: file
ini_comment_option () {
local ini_file="$2";
local ini_option="^$1.*$";
if [ $( is_option_in_file "$ini_option" "$ini_file" ) = "1" ]; then
echo "Commented $1 option in the lines: " $(get_option_line_number "$ini_option" "$ini_file")
sudo sed -i -e "s/\($ini_option\)/;\1/g" $ini_file
fi
}
# param_1: option | param_2: file
ini_uncomment_option () {
local ini_file="$2";
local ini_option="^;$1.*$";
if [ $( is_option_in_file "$ini_option" "$ini_file" ) = "1" ]; then
echo "Uncommented $1 option in the lines: " $(get_option_line_number "$ini_option" "$ini_file")
sudo sed -i -e "/$ini_option/ s/^;//" $ini_file
fi
}
@alexxroche
Copy link

[example]
ini="can have multiple sections"
[soap]
ini="does not have to have unique keys in a file, only within a section"
wsdl_cache_ttl=3600
[REST]
wsdl_cache_ttl=1234
also = 'if [ ! -z "$1" ] && [ ! -z "$2" ]; then echo "more portable"; fi '

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment