I have a bash dialog form that generates four variables. Each individual variable - if not empty - will lead to a sed command execution on the same file.
Bash Dialog Script that generates four variables var1, var2, var3, var4
#!/usr/bin/env bash
response=$(dialog \
--title "ini configure" \
--form "Configure php.ini" \
15 50 0 \
"Execution Time:" 1 1 "$exe_time" 1 10 20 0 \
"Memory Limit:" 2 1 "$mem_limit" 2 10 20 0 \
"Max File Size:" 3 1 "$max_file" 3 10 20 0 \
"Max Post Size:" 4 1 "$max_post" 4 10 20 0 \
3>&1 1>&2 2>&3 3>&-)
responsearray=($response)
var1=${responsearray[0]}
var2=${responsearray[1]}
var3=${responsearray[2]}
var4=${responsearray[3]}
How to combine these four conditional sed commands?
if [[ -n "$var1" ]]; then
sed -i 's/^;*\max_execution_time = .*/max_execution_time = "${var1}"/' /user/php8.0-fpm/php.ini
if [[ -n "$var2" ]]; then
sed -i 's/^;\memory_limit = ./memory_limit = "${var2}"/' /user/php8.0-fpm/php.ini
if [[ -n "$var3" ]]; then
sed -i 's/^;\max_file_size = ./max_file_size = "${var3}"/' /user/php8.0-fpm/php.ini
if [[ -n "$var4" ]]; then
sed -i 's/^;\max_post_size = ./max_post_size = "${var4}"/' /user/php8.0-fpm/php.ini
I know I can combine multiple sed commands with a semicolon, but it is the conditional that's creating the issue.
OS: Ubuntu 20.04 Headless
!
or you can point me to other sources where I can read about it - https://www.grymoire.com/Unix/Sed.html#toc-uh-32 has a little bit about it, but not in details with examples. – depar Sep 30 '21 at 12:40sed
command can be preceeded by a so-called address to filter the lines, where this command should be applied.3d
willd
elete only line number 3./foo/d
will delete lines that containfoo
. An!
invers the match, so3!d
deletes all lines except for line 3;/bar/!d
deletes all lines that do not containbar
. Please note that this kind of filtering I used in my answer is not the usage this is intended for, that's why I called it a trick. – Philippos Oct 01 '21 at 08:48