Home > Back-end >  How to add cookie jar file in bash script
How to add cookie jar file in bash script

Time:10-19

Here is my bash script

#!/bin/bash

cookie_value="'cookie: PF='$PF'; cwmsk='$cwmsk'; BIGipServerNP_QA_QFLOGINQAS0_7011='$BIGipServerNP_QA_QFLOGINQAS0_7011'; aatoken='$aatoken'; UserName='$UserName'; Auth='$Auth'; XSRF-TOKEN='$XSRF-TOKEN'; z='$z'; BN20='$BN20'; UserFirmId='$UserFirmId';  TS01a1bccb='$TS01a1bccb'; TS01e14722='$TS01e14722';" 

echo cookie_value

Please tell me where i need to add cookie_jar file. So that it can bring cookievalue (string it will return)

CodePudding user response:

Assumptions:

  • sole objective is to display cookie values on stdout where ...
  • cookie value is the last entry from any lines that contain the string TRUE or FALSE
  • NOTE: it's not apparent (to me) if there's an explicit ordering of the output

One awk idea:

awk '
/TRUE|FALSE/ { printf "%s%s",pfx,$NF; pfx=" " }
END          { print "\n" }
' cookie_jar

This generates:

01786344cc36119d024ed021fc31dad790cc200981f044 1we9edfauoefklare 1teji23jksdfas !qaE44xdbX2OjQtdL9Ez/f7vw2P/dxPd2WvZ9xQ== 01786344cc027084e046d692cedc2bbedc95e2512d8557aedca2

If OP needs to access these later in the script then I'm assuming the cookie name will also be required in which case I'd recommend storing the cookie name/value pairs in an associative array, eg:

unset cookies
declare -A cookies

while read -r cname cvalue
do
    cookies[${cname}]="${cvalue}"
done < <(awk '/TRUE|FALSE/ {print $(NF-1),$NF}' cookie_jar)

This produces the following array structure/contents:

$ typeset -p cookies
declare -A cookies=([BUILD0]="1teji23jksdfas" [TS01dda1cb]="01786344cc027084e046d692cedc2bbedc95e2512d8557aedca2" [TS21xx72R2]="01786344cc36119d024ed021fc31dad790cc200981f044" [UserxxxId]="1we9edfauoefklare" [BIGipS~NP_QA_QF~LQAS0_7011]="!qaE44xdbX2OjQtdL9Ez/f7vw2P/dxPd2WvZ9xQ==" )

From here OP can access the arry entries as needed, eg:

for i in "${!cookies[@]}"
do
    echo "name = ${i} / value = ${cookies[${i}]}"
done

Which generates:

name = BUILD0 / value = 1teji23jksdfas
name = TS01dda1cb / value = 01786344cc027084e046d692cedc2bbedc95e2512d8557aedca2
name = TS21xx72R2 / value = 01786344cc36119d024ed021fc31dad790cc200981f044
name = UserxxxId / value = 1we9edfauoefklare
name = BIGipS~NP_QA_QF~LQAS0_7011 / value = !qaE44xdbX2OjQtdL9Ez/f7vw2P/dxPd2WvZ9xQ==
  • Related