Bash script pulling variable from query_string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script pulling variable from query_string
# 1  
Old 04-06-2010
Bash script pulling variable from query_string

I have a variable embedded in a long string called "commands" coming in on a query_string. I need to copy the string to a file with the variable $loop1 converted before it writes. Right now, it writes the varible itself instead of what it should be.

QUERY_STRING
Code:
test.cgi?commands=cont+local%0D%0Aconfig%0D%0Acont+local%0D%0Aport+atm+3%2F0%0D%0Adescription+%24loop1%0D%0Acommit%0D%0A

Code:
#!/bin/sh
loop1="10.1.1.1"
echo "$QUERY_STRING" | sed -n 's/^.*commands=\([^&]*\).*$/\1/p' | nawk 'gsub(/%0D%0A/,"\n")' | sed 's/+/ /g;s/%24/\$/g;s/%2F/\//g' > cmd.list

This is what prints to the file:
Code:
cont local
config
cont local
port atm 3/0
description $loop1
commit

Here is what I need it to print:
Code:
cont local
config
cont local
port atm 3/0
description 10.1.1.1
commit

# 2  
Old 04-06-2010
Code:
$
$
$ echo $QUERY_STRING
test.cgi?commands=cont+local%0D%0Aconfig%0D%0Acont+local%0D%0Aport+atm+3%2F0%0D%0Adescription+%24loop1%0D%0Acommit%0D%0A
$
$ echo $loop1
10.1.1.1
$
$ echo $QUERY_STRING | perl -plne "s/%24loop1/$loop1/; s/^.*?=//; s/%0D%0A/\n/g; s/\+/ /g; s/%2F/\//g"
cont local
config
cont local
port atm 3/0
description 10.1.1.1
commit
$
$

tyler_durden
# 3  
Old 04-06-2010
Thank you durden_tyler. This works except I forgot to mention that the variable loop1 can be a loop1, loop2, loop3, etc. Also, there is an additional string called router which I pull out separately.
Code:
loop1="10.1.1.1"
loop2="10.1.1.2"
loop3="10.1.1.3"

QUERY_STRING:
Code:
test.cgi?commands=cont+local%0D%0Aconfig%0D%0Acont+local%0D%0Aport+atm+3%2F0%0D%0Adescription+%24loop1%0D%0Acommit&router=cisco.router.net



---------- Post updated at 05:52 PM ---------- Previous update was at 12:50 PM ----------

I added to the perl statement, but was hoping there is a way to shorten it up a bit.
Code:
echo $QUERY_STRING | perl -plne "s/%24loop1/$loop1/; s/%24loop2/$loop2/; s/%24loop3/$loop3/; s/^.*?=//; s/%0D%0A/\n/g; s/\+/ /g; s/%2F/\//g; s/&router=.*//"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need bash script to use a sed command as a variable

I need to be able to use a sed command as a variable in a bash script. I have the sed command that almost works the way I want it. the command is sed -n '/inet/,/}/p' config.boot This gets me this result: inet 192.168.1.245 } I need to get the IP address into a variable so I... (9 Replies)
Discussion started by: edlentz
9 Replies

2. Shell Programming and Scripting

Bash: Pulling first and last character in string

I am writing a bash script that will find all references to the “Well_List” in the “Comp_File”. I am filtering a Well_List that contains the following: TEST_WELL_01 TEST_WELL_02 TEST_WELL_11 TEST_WELL_22 GOV_WELL_1 GOV_WELL_201 PUB_WELL_57 PUB_WELL_82 . . Comparison... (5 Replies)
Discussion started by: petfyp
5 Replies

3. Shell Programming and Scripting

Acces Variable from expect-Script in bash-Script

Hi all, I have a little problem with a expect in a bash Script. The hull of my script: #!/bin/sh ( expect -c ' set a \"eee\"; # the variable a ' ) echo $a; # using the variable out of the expect script I would like to use the variable out of the expect script(in bash),... (3 Replies)
Discussion started by: gandalfthepink
3 Replies

4. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

5. Shell Programming and Scripting

BASH- Need help pulling data from .emlx

Hello, fellow computer junkies. First time poster! My boss wrote an application (Mavericks 10.9, Mountain Lion 10.8) that checks a user's security settings. The user runs the application, then it spits out an email that is sent back to our inbox showing the results. On our end, we have a mail rule... (5 Replies)
Discussion started by: sudo
5 Replies

6. Shell Programming and Scripting

Basic bash -- pulling files from an FTP server

Hi guys. Very new to this so apologies if this is ridiculously obvious, but I am not sure why this isn't working. I want to pull a file off an FTP server. I currently do it through windows, which is no problem, but I want to move everything to a Linux box I just installed. wget won't work as the... (4 Replies)
Discussion started by: majormajormajor
4 Replies

7. Shell Programming and Scripting

Pulling remote hostname into variable

I'm trying to write a shell script using bash that connects to a remote server, runs a command that generates a file, pulls the file over, then renames it with the hostname of the remote server and a an extension. So far, I'm able to everything but pull the hostname of the remote server into a... (2 Replies)
Discussion started by: hobbes80
2 Replies

8. Shell Programming and Scripting

QUERY_STRING with multiples appearances of the same string prefix

I have a shell script and it works pretty well to pull out the query_string contents of a single instance of router= But if I have multiple router= strings, it only pulls out the last one. Is there a way to pull out every router= and if there is a multple string, to egrep them together... (1 Reply)
Discussion started by: numele
1 Replies

9. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

10. Shell Programming and Scripting

How do you parse a variable in a bash script?

I have a script I use on my web server (Apache2). I am changing to Lighttpd and need to make a few changes. This is what I use on my apache server #!/bin/bash # accepts 3 parameters: <domain name> <user name> <XXXXXXXX> # domain name is without www (just domain.com) # username would be... (3 Replies)
Discussion started by: vertical98
3 Replies
Login or Register to Ask a Question