How to read query string from shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read query string from shell script?
# 1  
Old 07-01-2013
How to read query string from shell script?

Hi,

I am new this shell scripting. I have one html page which is executing shell script. That web form is passing some query strings to the script. Now how can I read query string in shell script and parse it in variables.

I tried with below shell script but its not working.
Code:
#!/bin/sh

saveIFS=$IFS
IFS='=&'
param=($QUERY_STRING)
IFS=$saveIFS

echo "$param[2]";

# ./test.cgi name=jdp
./test.cgi: line 5: syntax error: "(" unexpected

Appreciate your help in resolving this?

Thanks,
Jdp

Last edited by jdp; 07-01-2013 at 10:33 PM.. Reason: added code
# 2  
Old 07-01-2013
What OS are you using? If Solaris, sh does not support arrays, if AIX sh is a link to ksh which may not support that array syntax - use set -A

In addition, the syntax of line 8 is wrong.

Code:
echo ${param[2]}

# 3  
Old 07-01-2013
Thanks Scott for your reply,

I am using linux running on device. I tried your suggestion but it is throwing error for declare.

Last edited by jdp; 07-01-2013 at 10:57 PM..
# 4  
Old 07-01-2013
Which Linux (i.e. Fedora, Debian, etc.), on what "device"? Try pointing the "shebang" on line one to /bin/bash, if it exists, or say what /bin/sh is pointing to.
# 5  
Old 07-02-2013
Thanks Scott,

The device is having linux OS and running a shttpd web server. I checked the /bin directory, It has sh only and I didn't find bash. I have never written shell scripts so looking for a script which can read GET/POST data using shell scripts.
# 6  
Old 07-02-2013
Clearly, the problem is with the array (param=($QUERY_STRING)). Your shell doesn't support them, or doesn't support them in that format. You'll need to find an alternative way of getting the values.

i.e.
Code:
$ cat myScript
QUERY_STRING="a=1&b=2&c=3&d=4&e=5"
IFS='=&'
set -- $QUERY_STRING

echo $3 is $4
echo $9 is ${10}

$ ./myScript
b is 2
e is 5

# or

$ cat myScript
QUERY_STRING="a=1&b=2&c=3&d=4&e=5"
IFS='&'
set -- $QUERY_STRING
echo ${2%=*} is ${2#*=}

$ ./myScript
b is 2

You didn't answer either of my questions: Which Linux are you using, and what is a "device".
This User Gave Thanks to Scott For This Post:
# 7  
Old 07-02-2013
Thank Scott for your code. I really appreciate.

I did some googling and managed to get the data using POST request from HTML using JavaScript. Posting the code in case someone newbie like me need to achieve the same,
Code:
#!/bin/sh

if [ "$REQUEST_METHOD" = "POST" ]; then
  if [ "$CONTENT_LENGTH" -gt 0 ]; then
      read -n $CONTENT_LENGTH POST_DATA <&0
  fi
fi

#echo "$POST_DATA" > data.bin
IFS='=&'
set -- $POST_DATA

#2- Value1
#4- Value2
#6- Value3
#8- Value4

echo $2 $4 $6 $8

echo "Content-type: text/html"
echo ""
echo "<html><head><title>Saved</title></head><body>"
echo "Data received: $POST_DATA"
echo "</body></html>"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script (ksh) - SQLPlus query filter using a string variable

Using ksh, I am using SQLPlus to execute a query with a filter using a string variable. REPO_DB=DEV1 FOLDER_NM='U_nmalencia' FOLDER_CHECK=$(sqlplus -s /nolog <<EOF CONNECT user/pswd_select@${REPO_DB} set echo off heading off feedback off select subj_name from subject where... (5 Replies)
Discussion started by: nkm0brm
5 Replies

2. Shell Programming and Scripting

How do I read sql query into shell script?

Hello All, I'm trying to put together a shell script that will: 1. connect to an oracle database 2. execute a query 3. save the output to a csv file I know that I can execute the sqlplus -s user/pass @dbsid and get logged in. What I would like to do is have my query in a separate text... (9 Replies)
Discussion started by: bbbngowc
9 Replies

3. Programming

Query result from shell script

Hi, I wrote the below script to get the query result from a Syabase DB. isql -s -U **** -P **** SYBASE SERVERNAME USE ***(Database name in Sybase) @command.sql But im not getting the output. Can anyone help me on this (2 Replies)
Discussion started by: rohan G
2 Replies

4. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

5. Shell Programming and Scripting

shell script query

Hi Admins, I was trying to list the failed logins as part of my daily checklist. Here is my script for i in `who -s /etc/security/failedlogins|tail -100|grep "$dt"|awk '{print $1" "$6}'` do a=`echo $i|wc -l` if then echo $i else echo "There are no failed logins" fi done but... (3 Replies)
Discussion started by: newaix
3 Replies

6. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

7. Shell Programming and Scripting

shell script query

hi all i have a shell script for connecting in sybase env what i need is i have around 10 servers , i need to connect to all servers and retrive the database inforamtion in the servers and display them any one of u have it request to share it asap ! "QUERY TO Connect to all servers... (1 Reply)
Discussion started by: mvsramarao
1 Replies

8. Shell Programming and Scripting

query sql using shell script

query sql using shell script, is it possible? my friend told me to do a file.sql and link to my shell script, but can i query sql using shell script? thanks in advance! (2 Replies)
Discussion started by: kingpeejay
2 Replies

9. UNIX for Dummies Questions & Answers

Shell script query

Hi, I am stuck assigning a value to a variable. I am trying to assign a value to a variable but getting error.... IP_ADDR=grep 'I.P. Address' /install/cfgdist/`uname -n`.cfg | cut -d : -f 2| cut -d . -f 1-3| sed s/" "//g I am using this script to grep first three octets of an IP address... (4 Replies)
Discussion started by: max29583
4 Replies

10. Shell Programming and Scripting

C Shell Script query

Hi I need a small help Cshell% more abc.txt ******** Cshell% cat abc.txt | cut -c1-3 *** Cshell%set test3=`cat abc.txt | cut -c1-3` Cshell%echo $test3 a.txt b.txt................. ..... It displays all the file in the current directory. I want *** to be displayed. Can any one of... (1 Reply)
Discussion started by: bpupdown
1 Replies
Login or Register to Ask a Question