Code:
old_deptno=$(awk -v FILE="$work_file" '
BEGIN {
FS = "="; OFS = "=";
}
$1 == "$$DEPTNO" {
print $2;
$2 = $2 % 40 + 10
}
{
print $0 >> FILE
}
' deptno.txt )
old_deptno=$(awk ..... )
Set the variable old_deptno to the result (stdout) of the awk command.
awk -v FILE="$work_file" ' ..... ' deptno.txt
Awk set an awk variable FILE and execute the inline script with deptno as input_file.
FILE is the name of the file where to write result of script.
The old value of $$DEPTNO will be written to stdout.
BEGIN { FS="=" ; OFS="=" }
Awk initialization, the code of the BEGIN pattern is excuted before opening the first file.
FS="=" set the internal FIELD SEPARATOR to "=", records will be split in fields that are delimited by FS.
$$DEPTNO=10 gives $1=$$DEPTNO and $2=10
OFS="=" set the internal OUTPUT FIELD SEPARATOR to "=", fields will be written with "=" as separator.
For example: print "aaa","bbb" will be printed as 'aaa=bbb'
$1 == "$$DEPTNO" { ..... }
The line select input records where field $1 is equal to "$$DEPTNO" and specify the code to execute for that record.
print $2;
Write to stdout the old value of $$DEPTNO
$2 = $2 % 40 + 10
Set the new value for $$DEPTNO, 10->20 20->30 30->40 40->10
The record ($0) is now '$$DEPTNO=new_value'
{ print $0 >> FILE }
This code is executed for every input record (no pattern specified).
The current record ($0) is written (append) to the file which name is in variable FILE.
If the record is the '$$DEPTNO' record, the new value had already be set by the previous pattern/action code.
Hope this help, like said PHV ...