script to parse variables to curl script and execute
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
script to parse variables to curl script and execute
Hi Guys,
I currently have a situation which up until now has been a complete manual copy, paste & URL editing procedure.
I have a text file which contains information in basic form:
111111,21-11-2007,Some Text Seperate By Spaces,No
222222,21-11-2007,More Text Seperate By Spaces,Yes
above is the example of what is in the text file.
What i am trying to do is make a script which reads the text file and puts the above into variables such as 22222 = $NUM, 21-11-2007 = $DATE, Text Spaces = $REASON, Yes = $ENABLED
For the ",Some Text Seperate By Spaces," i need all the spaces to get turned into "+" characters before being put as a variable
# read a line (assuming file is opened using open())
$read = <file>;
# get relevent data
$read =~ /(.*?),(.*?),(.*?),(.*)/;
$num = $1;
$data = $2;
$reason = $3;
$reason =~ s/ /+/g;
$enable =$4
open TOCURL, "| curl";
# its case sensitive so the below and above need to match case
print TOCURL "<URL>/index.php?number=$NUM&date=$DATE&reason=$REASON&enabled=$ENABLED
";
close TOCURL;
Yah. What chrism01 said. Emphasis on exactly. With error messages and other relevant output pasted.
Did you replace the URL and curl options appropriately, as I mentioned?
--- rod.
This assumes that FILE & URL have been set to your desired values. FILE seems to have to be exported, because (AFAIK) the awksystem call executes in a sub-shell.
The final double double quote is an artifact of the piece-wise construction of the code & may be unnecessary.
As for your 2nd Q, I am not clear if angrybanana's
code represents the logic you want, or if you want $3/$reason changed regardless of its value. I.E., if $4/$enabled does = "yes" but $3/$reason does NOT = "Yes+this+is+enabled" do you want it to be changed to "This+is+disabled" anyway?
@Four: so i assume this reads line by line from the text file and does the same curl command per line?
Just i am at work atm and unable to test it.
EDIT:
Just tested it, it just errors out
$ perl test.pl
syntax error at test.pl line 16, near "$4
open "
Execution of test.pl aborted due to compilation errors.
Oops I forgot a semicolon
Heres a more complete version simply run by
perl perlcode.pl file.txt_or_some_file
Code:
# this code is not tested I'm at school right now using windows machine with no perl
# read a line (assuming file is opened using open())
$file = $ARGV[0];
if(!$file){
print "Please enter a file name\n";
$file = <>;
}
if(!(-e $file)){
print "file doesn't exit\n";
}
open (file, "$file") or die ("couldn't open $file\n");
while(<file>){
$read = $_;
# get relevent data
$read =~ /(.*?),(.*?),(.*?),(.*)/;
$num = $1;
$data = $2;
$reason = $3;
$reason =~ s/ /+/g;
$enable = $4;
if($enable eq "yes"){
# put code to do something here if you want
}
open (TOCURL, "| curl") or die("Couldn't open pipe to curl\n");
# its case sensitive so the below and above need to match case
print TOCURL "<URL>/index.php?number=$num&date=$date&reason=$reason&enabled=$enable
";
close TOCURL;
}
close file;
You need to chomp() the input filename to remove the trailing '\n'.
Also, for cmds like 'open'/'close', perl stores the error code/msg in '$!'. You should add this to the exit msg so you know WHY it failed.
when i enter the CURL URL and run the script it gives me back the following errors.
$ perl script.pl script_txt.txt
Bareword found where operator expected at script.pl line 14, near ""curl -s -S -b cookie.txt -d "number"
(Missing operator before number?)
Operator or semicolon missing before &radius at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
Operator or semicolon missing before &email at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
Operator or semicolon missing before &ftp at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
Operator or semicolon missing before &cancel_reason at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
Operator or semicolon missing before &do_cancel_service at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
String found where operator expected at script.pl line 14, near "1" http://url/subdir/page.php""
(Missing operator before " http://url/subdir/page.php"?)
Global symbol "$reason" requires explicit package name at script.pl line 8.
syntax error at script.pl line 14, near ""curl -s -S -b cookie.txt -d "number"
Execution of script.pl aborted due to compilation errors.
------
Also with $enabled checking if = yes or no, all i want it to do is. if $enabled = Yes then change $enabled to "ETC Applies" if $enabled = No then change $enabled to "No ETC"
when i enter the CURL URL and run the script it gives me back the following errors.
$ perl script.pl script_txt.txt
Bareword found where operator expected at script.pl line 14, near ""curl -s -S -b cookie.txt -d "number"
Please post the line in question with your edits. I see nothing that prevents it from compiling, and it compiled fine in the form that I posted.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.