Using variable output in awk

Hi,

I am trying to use variable output in awk to append a string to a word in a line. But that is not happening. Could you please help me on this.

The below is the code

#!/bin/ksh
set -x
src=/users/oracle/Temp
dst=/ora49/dpdump/$1
if [ "$#" -ne 3 ]; then
  echo "Usage: Script_Name DB_Name Parfile EXP|IMP"
  exit 1
fi
if [ ! -d "$dst" ]; then
    echo "Source path: $dst doesn't exist"
    exit 1
fi
echo $1
var1=$(echo "$1" | tr '[:upper:]' '[:lower:]')
var2=$(echo "$2" | tr '[:upper:]' '[:lower:]')
var3=$(echo "$3" | tr '[:upper:]' '[:lower:]')
echo $var1
ORACLE_SID="$var1"
ORAENV_ASK=NO
. oraenv 2>&1
ORAENV_ASK=YES
if [ `ps -ef | grep smon | grep $var1 | awk -F "_" '{print $3}'` == $var1 ]; then
        if [ -f "$src/$var2" ]; then
                echo " Parfile exists"
                cp "$src/$var2" "$dst"
                chmod 700 "$dst/$var2"
                if [ "$var3" == "exp" ]; then
                        echo "Export"
                        if [[ `cat "$dst/$var2" | sed -e 's/[\t ]//g;/^$/d' | awk -F "=" 'tolower($1)=="userid"{print $2}'` == "appdbinstall" ]]; then
                                echo "appdbinstall exists"
                                vpd=`cat $src/encrypted.pwd`
                                echo "$vpd"
                                awk '/appdbinstall/ {$0=$0;for (i=1;i<NF;i++)t=t ;$0=$0 "/${vpd}"}1' "$dst/$var2" >> "$dst/${var1}_tmp.par"
								nohup expdp parfile="$dst/${var1}_tmp.par" &
                        else
                                echo "appdbinstall not exists"
                        fi
                elif [ "$var3" == "imp" ]; then
                        echo "Import"
                else
                        echo "Pass 3rd parameter EXP|IMP"
                fi
        else
                echo "Parfile does not exist"
        fi
else
        echo "Please pass value for DB name or DB is not running"
        exit 1
fi
$ cat test_10062019.par
userid = appdbinstall
job_name = test_export
tables = dbadmin.gg_test
directory = EXPDP
dumpfile=test_export.dmp
logfile=test_export.log

What I am trying to achieve is if userid in parfile is appdbinstall, i need to attach the password from encrypted.pwd like , userid = appdbinstall/<password>.

awk '/appdbinstall/ {$0=$0;for (i=1;i<NF;i++)t=t ;$0=$0 "/${vpd}"}1' "$dst/$var2" >> "$dst/${var1}_tmp.par"

:- This is not working . Could you please suggest on this.

Thanks,
Mani

A short but proper description the entire situation (how the script is called, what the positional parameters stand for, how parfile and test_10062019.par relate) would help people understand the problem and help you get faster and better answers. Meaningful indentation would improve readability and understandibility of your code.

Use the correct mechanism to convey a shell variable's contents into an awk script, like

awk -vPWD=$vpd '/appdbinstall/ {$0=$0;for (i=1;i<NF;i++)t=t ;$0=$0 "/" PWD}1' file

You might want to try sth. like this on your way to optimize your script:

awk '
NR == 1         {PWD = $0
                 next
                }
/appdbinstall/  {$NF = $NF "/" PWD
                 CHD = 1
                }
1
END             {if (CHD) print "appdinstall exists." > "/dev/stderr"
                 exit (!CHD)
                }
'  encrypted.pwd test_10062019.par

Check for the exit code to control further processing.

Sorry RudiC. Its my bad that i dint give a description about the code. I will definitely do this next time.

This script accepts 3 arguments ie, DB name, parfile and EXP|IMP.
This script will be used by application team and they will not able to access as sys user. That is why we are providing access to another user called appdbinstall. Right now password is stored in plain text and get the same during execution and attach to the userid value . I am trying to figure out some way to encrypt/decrypt password. But crypt is not installed in our system.

I will try your suggestion and let you know.

Thanks,
Mani