To have more flexibility with the parameters values you can use can use the source
command or .
operator
Lets assume the file contains the following line
param_1=7412
param_2=1234
we can then import this variable using
#!/bin/bash
source <filename>
echo $param_1
echo $param_2
param_1=9999
param_2=7412
#check that new values are assigned to variables
echo $param_1
echo $param_2
Then use the echo
command to put the new values to a text file. Mind that you should take care of the file being deleted before or you need to choose if you want it to be appended or overwritten:
destdir=/some/directory/path/filename
if [ -f "$destdir"]
then
echo "param_1=$param_1" > "$destdir"
fi
The if
tests that $destdir
represents a file.
Note:
The >
replaces the text in the file.
If you only want to append the text in $param_1
to the file existing contents, then use >>
instead:
echo "param_2=$param_2" >> "$destdir"
Hope this helps.
References:
https://askubuntu.com/questions/367136/how-do-i-read-a-variable-from-a-file
Shell - Write variable contents to a file