I'm using dash and I need to create CGI script that will parse the query string and save each value to a variable
OLDIFS=$IFS
// use & as delimeter
IFS='&'
//this will loop the queryString variable
for line in $queryString;
do
//check if the key = action, if the condition was satisfied it will save the line to a variable..
//if the key is equal to type etc. etc.
echo "{$line,1}"
if[{$line,0:7}="action="]
then
cgi_action={$line:8}
elif[{$line:0:5}="type="]
then
cgi_type={$line:6}
fi
done
IFS=$OLDIFS
Im sure that I have errors to get the substring of the line (or string) but please let's focus on the error I get when I put an if statement inside a for loop. What's the correct way to write an if condition inside for loop in dash shell scripting.
Additional info, im using ubuntu 14.04,
Firstly, comments in shell scripts are #
, not //
, which means that your script is confusing dash when it tries to parse it.
Secondly, you have to put spaces around all the tokens in the if condition - It's actually a syntax error the way you've written it e.g. change the action test to:
if [ {$line,0:7} = "action=" ]
thirdly, dash doesn't support substring extraction, and even if it did, the correct format is:
${variable:start}
${variable:start:nchars}
If you want to use substring extraction then you should use bash
rather than dash
.
Thirdly, you've got an off-by one error in the index into the value extraction - you're dropping the first character in the string. e.g. you check from offset 0
for a length of 5
for the type=
value, then you take everything from index 6
, which is one greater than you should be using.
your code would better read something like:
OLDIFS=$IFS
# use & as delimeter
IFS='&'
#this will loop the queryString variable
for line in $queryString; do
#check if the key = action, if the condition was satisfied it will save the line to a variable..
#if the key is equal to type etc. etc.
echo "${line:1}"
if [ ${line:0:7} = "action=" ]; then
cgi_action=${line:7}
elif [ ${line:0:5} = "type=" ]; then
cgi_type=${line:5}
fi
done
IFS=$OLDIFS
Not that I would ever recommend using shell for CGI scripting