LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-08-2019, 09:27 AM   #1
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Rep: Reputation: Disabled
cgi & bash


Hi All,

If I have this cgi script:

Code:
#!/bin/bash

echo "Content-type: text/html"
echo ""
echo "<html><head><title>Test</title></head>"
echo "<body>"

echo "<p>Select:</p>"

echo "<form action="submit.sh" method="GET">"

echo "<input type="radio" name="letter" value="A">A<br>"
echo "<input type="radio" name="letter" value="B">B<br>"
echo "<input type="radio" name="letter" value="C">C<br>"
echo "<input type="submit">"

echo "</form>"

echo "</body></html>"
and submit.sh:

Quote:
#!/bin/bash

echo "Content-type: text/html"
echo ""

if [ -z "$QUERY_STRING" ]; then
exit 0
fi

if [ "$REQUEST_METHOD" != "GET" ]; then
echo "Error"
exit 1
fi

letter=$(echo "$QUERY_STRING" | sed -n 's/^.*letter=\([^&]*\).*$/\1/p' | sed "s/%20/ /g")

CONFIG_FILE="/home/$USER/www/testing.cfg"

if [ ! -e "$CONFIG_FILE" ];then
touch "$CONFIG_FILE"
fi

echo "LETTER="$letter"" > "$CONFIG_FILE"
How could it be done that after refreshing, the radio button automatically sets to the previously set letter? In other words I need to feed the config file into, but I don't know how to implement that.

Thanks,
 
Old 03-08-2019, 09:43 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Your submit script could reproduce the form and, since you are using bash, you could have the letter as the key in an array and set that element in the array to "checked".

However, be sure to tighten up the sanitization of the variable $letter before it gets passed to the system. You don't want letter to be a"; wget -O bad http:/...; sh bad; or anything similar.
 
1 members found this post helpful.
Old 03-08-2019, 09:46 AM   #3
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
Ok, what if I just hit F5 on the index page, then I'd like to read in the previously set variable, and be able to re-set it
 
Old 03-08-2019, 09:50 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
The web page sends data to the server. The server processes it then sends a response back which is then rendered in the browser.

The way I've usually done it, albeit in perl not shell, is to put everything into the submit script which then displays a blank form if it has not already received any data.

Remember HTTP, and thus HTTPS, is completely stateless and also that the server does the work.
 
Old 03-08-2019, 11:55 AM   #5
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
ok, but after the first selection, a config file is written. After that if I reload the page, I dont know what has been selected previously. So somehow when the page reloads, it should show what's been selected.
 
Old 03-08-2019, 12:02 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
There should no reloading of any pages. The form should send the data away and get, via the submit script produced by the submit script, a new form with the right button checked. It's easy to do but not so easy to explain.
 
1 members found this post helpful.
Old 03-08-2019, 12:39 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
It's NOT easy to explain, yes. Going to try anyway.
Your form has radio buttons.
Select one ("check" it) and send to the server.
The server-side script needs to capture which is checked and populate the form with the checked attribute when it sends it back. So the form has
Code:
<input type="radio" $chkdA name="letter" value="A"
etc.
and the script that sends the form needs something like
Code:
if $letter = 'A' then $chkdA = "checked" 
etc.
 
1 members found this post helpful.
Old 03-08-2019, 01:37 PM   #8
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
Thanks for the help Guys! I think I need to dive into this subject more.

I switched to haserl in the meantime, but I got an error if I include the config file:
Quote:
#!/usr/bin/haserl
content-type: text/html

<%in /home/$USER/www/cgi-bin/testing.cfg %>

<html>
<body>

<form action="submit2.sh" method="GET">

<input type="radio" name="letter" value="A">A<br>
<input type="radio" name="letter" value="B">B<br>
<input type="radio" name="letter" value="C">C<br>
<input type="submit" value="set">

</form>

</body>
</html>
the output is:

Quote:
haserl CGI Error

Unable to open file /home/$USER/www/cgi-bin/testing.cfg
I think I need to parse the config file before, to set it "checked"

Last edited by kzo81; 03-08-2019 at 01:43 PM.
 
Old 03-08-2019, 02:15 PM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by kzo81 View Post
Thanks for the help Guys! I think I need to dive into this subject more.

I switched to haserl in the meantime, but I got an error if I include the config file:


the output is:



I think I need to parse the config file before, to set it "checked"
What do you expect $USER to be?
I would expect it to be the apache user...where, actually, is the file located.
 
1 members found this post helpful.
Old 03-08-2019, 02:16 PM   #10
kzo81
Member
 
Registered: Aug 2014
Location: Hungary
Distribution: Debian, Linux Mint, CentOS
Posts: 197

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
What do you expect $USER to be?
Thanks!! I though it's the system user...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nagios: statusmap.cgi & Trends.cgi files missing wlchak Linux - Software 6 10-30-2009 06:47 AM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM
http://www.burstnet.com/cgi-bin/ads/ad7954a.cgi/3980/RETURN-CODE rverlander LQ Suggestions & Feedback 1 06-07-2002 07:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration