Bash: Update a Variable Within a File

Bash: Update a variable within a file

sed -i -e '/^ONBOOT=/s|.*|ONBOOT=yes|; /^BOOTPROTO=/s|.*|BOOTPROTO=static|' file

Also try:

sed -i -re 's|^(ONBOOT=).*|\1yes|; s|^(BOOTPROTO=).*|\1static|' file

Or

sed -i -e 's|^\(ONBOOT=\).*|\1yes|; s|^\(BOOTPROTO=\).*|\1static|' file

Updating variables by reference in bash script?

Updating variables by reference in bash script?

And similar to C++, once you assign the value of a variable, there is no way to track where from the value came from. In shell all variables store strings. You can store variable name as a string inside another variable, which acts as the reference. You can use:

Bash indirect expansion:

A="say"
B=A
echo "B is ${!B}"
A="say it"
echo "B is ${!B}"

Bash namereferences:

A="say"
declare -n B=A
echo "B is $B"
A="say it"
echo "B is $B"

Evil eval:

A="say"
B=A
eval "echo \"B is \$$B\""
A="say it"
eval "echo \"B is \$$B\""

Is this possible?

Yes - store the name of the variable in B, instead of the value.

envsubst from Lazy Evaluation in Bash. Is following the way to do it?

No, envsubst does something different.

Edit certain variables in a file using bash script

The 3rd argument to the gensub() function must be a count of replacement
such as 1 or g (global).
Would you please try instead:

#!/bin/bash

# user's inputs
read -p "FILE_NAME: " -r file_name
read -p "DISPLAY: " -r display
read -p "RT-HOST: " -r rt_host

awk -v file_name="$file_name" -v display="$display" -v rt_host="$rt_host" '
{
sub(/FILE_NAME *= *\"[^"]+/, "FILE_NAME = \"" file_name)
sub(/DISPLAY *[^:]+:/, "DISPLAY " display ":")
}
/RT-HOST / {
if (! count++)
sub(/RT-HOST *[^[:space:]]+ *=/, "RT-HOST " display " =")
else
sub(/RT-HOST *[^[:space:]]+ *=/, "RT-HOST " rt_host " =")
}
1
' SES.ses > SES.tmp && mv -f -- SES.tmp SES.ses

How to modify a global variable within a function in bash?

When you use a command substitution (i.e., the $(...) construct), you are creating a subshell. Subshells inherit variables from their parent shells, but this only works one way: A subshell cannot modify the environment of its parent shell.

Your variable e is set within a subshell, but not the parent shell. There are two ways to pass values from a subshell to its parent. First, you can output something to stdout, then capture it with a command substitution:

myfunc() {
echo "Hello"
}

var="$(myfunc)"

echo "$var"

The above outputs:

Hello

For a numerical value in the range of 0 through 255, you can use return to pass the number as the exit status:

mysecondfunc() {
echo "Hello"
return 4
}

var="$(mysecondfunc)"
num_var=$?

echo "$var - num is $num_var"

This outputs:

Hello - num is 4

Shell - Write variable contents to a file

Use the echo command:

var="text to append";
destdir=/some/directory/path/filename

if [ -f "$destdir" ]
then
echo "$var" > "$destdir"
fi

The if tests that $destdir represents a file.

The > appends the text after truncating the file. If you only want to append the text in $var to the file existing contents, then use >> instead:

echo "$var" >> "$destdir"

The cp command is used for copying files (to files), not for writing text to a file.

How to read a file into a variable in shell?

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh
value=`cat config.txt`
echo "$value"

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash
value=$(<config.txt)
echo "$value"

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - Specialities.

How to update a variable value from a loop A to another loop B - in bash

The line that increments the variable:

((counter_value++))

There's no need to put the second loop into the background. Saving the variable to a file is a little clumbsy, but it works.

#!/bin/bash

counter_value=1
file="/tmp/temp$$"
echo "counter_value=$counter_value" > "$file"

function Print_counter_value () {
echo $counter_value ; }

#_____FIRST LOOP_____
while : ; do # infinite loop
. "$file"
((counter_value++))
echo "counter_value=$counter_value" > "$file"
sleep 2
done &

#_____SECOND LOOP_____
while : ; do # infinite loop
. "$file"
Print_counter_value & sleep 1
done


Related Topics



Leave a reply



Submit