0

A simple bash script that can set a cookie when executed via web :

#!/bin/bash
echo "Set-Cookie: eee=1"
echo "Content-type: text/html"
echo ""
echo "test"

I replaced the entire codes above with the following:

#!/usr/bin/env python
import os

print 'Set-Cookie: eee=1'
print 'Content-Type: text\n'

print '<html><body>'

a = os.environ.get('HTTP_COOKIE')
print a

print '</body></html>'

this one can now both set and retrieve a cookie.

but it is no longer a bash script. it is a python script.

the question is.. how to retrieve the cookie via bash script itself.. ?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Do read http://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters — especially if you're going to process data from a remote client. In a shell script, echo $VARIABLE does not print the value of the variable, it does further processing. – Gilles 'SO- stop being evil' Sep 02 '14 at 22:16

1 Answers1

1

This is your second script in bash:

#!/bin/bash

echo "Set-Cookie: eee=1"
echo "Content-Type: text"

echo "<html><body>"

printf '%s\n' "$HTTP_COOKIE"

echo "</body></html>"

os.environ.get('HTTP_COOKIE') gets the evironment variable called HTTP_COOKIE, within bash you can easly call the variable with "$HTTP_COOKIE".

chaos
  • 48,171