It is my STRONG suspicion that you shouldn't be running a script to update your script at all. Instead, you should just call awk
from within your original script, and get the data you need that way. Then saker.txt
could simply be used as a config file (which it is) to change the behavior of your script, (instead of using it to change the output of your script-making-script).
I have very limited data on your script, obviously, but what it looks like is that you are using saker.txt
as a lookup table to set a variable in your script.
Specifically, the script snippet:
if [ "$STATUS" == "1923.12.312." ]
then
vem="Nikl"
fi
if [ "$STATUS" == "12391.123.123" ]
then
vem="Jo"
fi
if [ "$STATUS" == "12398123.123912" ]
then
vem="Ad"
fi
can be functionally reproduced with an inline awk script as follows:
vem=$( awk -v status="$STATUS" '$1 == status { printf "%s", $2 ; exit }' saker.txt )
This answers "How can I set a variable in bash
based on looking up another variable's value in a file?" which is what it seems you're trying to accomplish by running your script-to-update-your-script.
There are caveats, of course, such as "What if the value of $STATUS
isn't listed in saker.txt
at all?" "What if it's listed twice?" "How can I set a default value for vem
?" But you still have all those caveats, and moreso, with a script-updating-script.
EDIT: If you want to allow spaces in the value of vem
as given in the saker.txt
file, you're better off using something else for a delimiter, such as a tab:
$ cat saker.txt
1923.12.312. Nikl
12391.123.123 Jo
12398123.123912 Ad
Then use the following version of the awk
one-liner:
vem=$( awk -Ft -v status="$STATUS" '$1 == status { printf "%s", $2 ; exit }' saker.txt )
A caveat about tab delimiters: Be sure you don't use more than one tab. Unlike awk
's default "whitespace delimited" handling, when you explicitly set the delimiter as a TAB only, each tab is counted as a delimiter regardless of whether there is any text between the tabs. So 10000<tab><tab>John
will be regarded as THREE fields: "10000", "" (empty field), and "John".
while read
was a recommendation from a stackoverflow user. What is better to use for processing text files then? (In your opinion) – DisplayName Dec 01 '15 at 01:31saker.txt
. – DisplayName Dec 01 '15 at 01:36