Search code examples
bashshellhttpcookiessession-cookies

How do i write bash shell script for cookie


Here is bash version GNU bash, version 5.0.17

Here is the cat -A cookie_jar

    ITRUE^I/^IFALSE^I0^ITS01e14722^I01786344cc8175e2661caaa80
    ITRUE^I/^ITRUE^I0^IUserFirmId^I1$
    IFALSE^I/^ITRUE^I0^IPF^IfqTGnM9LIjxqrsn
    IFALSE^I/^ITRUE^I0^BIGipS~NP_QA_QF~LQAS0_7011   !qaE44xdbX2OjQtdL9Ez/f7vw2P/dxPd2WvZ9xQ==

I have a cookie_jar file below is the content of that cookie_jar file

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

.cn.com TRUE    /   FALSE   0   TS21xx72R2  01786344cc36119d024ed021fc31dad790cc200981f044
.cn.com TRUE    /   TRUE    0   UserxxxId   1we9edfauoefklare
.cn.com TRUE    /   TRUE    0   BUILD0  1teji23jksdfas
#HttpOnly_cn.com    FALSE   /   TRUE    0   BIGipS~NP_QA_QF~LQAS0_7011  !qaE44xdbX2OjQtdL9Ez/f7vw2P/dxPd2WvZ9xQ==
.cn.com TRUE    /   FALSE   0   TS01dda1cb  01786344cc027084e046d692cedc2bbedc95e2512d8557aedca2

I want to have all above cookiename=cookievalue should have in one script and all the cookievalue should show in one line

Tell me How to write a script for above to get cookivalue with there cookiename

Example of i have written some script of it

#!/bin/bash
TS21xx72R2="$(grep -m 1 "TS21xx72R2" cookie_jar | awk -F' ' '{print $7}')" 
BUILD0="$(grep -m 1 "BUILD0" cookie_jar | awk -F' ' '{print $7}')" 
TS01dda1cb="$(grep -m 1 "TS01dda1cb" cookie_jar | awk -F' ' '{print $7}')" 
BIGipS~NP_QA_QF~LQAS0_7011="$(grep -m 1 "BIGipS~NP_QA_QF~LQAS0_7011" cookie_jar | awk -F' ' '{print $7}')" 
UserxxxId="$(grep -m 1 "UserxxxId" cookie_jar | awk -F' ' '{print $7}')"

echo $TS21xx72R2 $UserxxxId $TS01dda1cb $BIGipS~NP_QA_QF~LQAS0_7011 $BUILD0

Here is the of correct output

/cookie.sh: BIGipS~NP_QA_QF~LQAS0_7011  !qaE44xdbX2OjQtdL9Ez/f7vw2P/dxPd2WvZ9xQ==: No such file or directory
1teji23jksdfas 1we9edfauoefklare 01786344cc027084e046d692cedc2bbedc95e2512d8557aedca2 01786344cc36119d024ed021fc31dad790cc200981f044 ~NP_QA_QF~LOGINQAS0_7011

NOTE:- BIGipS~NP_QA_QF~LQAS0_7011 (when i execute the script i got error that "No such file or directory") but every word is correct in both script and my file

Output im getting correct but only thing is it need to be format in way correct that should be in shell script

New Script will be great helpful


Solution

  • 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 array 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==