איך לקבל command line parameter

frolik

New member
איך לקבל command line parameter

שלום לכולם יש לי סקריפט בשפת perl (בסביבת לינוקס) אני רוצה לקבל פרמטרים ממערכת ההפעלה: SCRIPT-NAME -F JKJJ -B JKL אני רוצה לקבל את ה- JKJJ וה- JKL לתוך שתי משתנים רעיונות ? תודה
 

annefan

New member
פשוט ומורכב

הארגומנטים נגישים בתוך ARGV@, כש-0 הוא הפרמטר הראשון. בסקריפט פשוט זה עשוי להספיק לך. אם הטיפול בפרמטרים מתחיל להיות מורכב, תשתמש במודול Getopt::Long או Getopt::Std.
 

alexrait1

New member
אם אני זוכר נכון

אז זה המשתנה
@_​
או המשתנה:
_$​
תלוי איך אתה רוצה לקבל אותם, במקרה שלך זה הכרוכית.
 

alexrait1

New member
אה טעות שלי זה @ARGV באמת

מה שאמרתי היה בנוגע לפונקציות.
 

voguemaster

New member
אני אוהב את השיטה הזו

usage() unless (@ARGV == 2); my $a= shift(@ARGV); my $b= shift(@ARGV);​
כאשר usage זו פונק' שמדפיסה איך להשתמש בתוכנית.
 

frolik

New member
יש דרך פשוטה

להפריד את המקדם (-f) לערך ? לדוגמא אני מריץ את האפליקציה עם: app -f "bla bla" אני רוצה שבמקרה של -f לתוך משתנה מסויים יכנס הערך (bla bla) תודה על העזרה
 

frolik

New member
איפו הטעות שלי?

foreach (@ARGV) { my $a= shift(@ARGV); my $b= shift(@ARGV); if ($a == "\-F") { $mailfrom = $b } if ($a == "\-f") { $from = $b } if ($a == "\-s") { $smtpserver = $b } if ($a == "\-S") { $subject = $b } if ($a == "\-m") { $msg = $b } if ($a == "\-t") { $mailto = $b } }
 

frolik

New member
תודה הסתדרתי

אני משתמש ב- use Getopt::Std; my %options; getopts("F:f:S:s:m:t:", \%options); וזה עובד מעולה :) תודה
 

frolik

New member
בעיה חדשה שקשורה

התהליך מתחיל ב- bash script, ומהסקריפט אני קורא לפרל, אני מעוניין להעביר לפרל string עם רווחים, ניסיתי להעביר בתוך גרש ואו גרשיים וכלום הוא מזהה את הרווחים כמפריד בין פרמטרים. איך אני יכול להעביר מחרוזת עם רווחים לסקריפט פרל ? תודה
 

annefan

New member
בלינוקס?

אני מנסה ב-CYGWIN, וזה בסדר: סקריפט בפרל:
#!/usr/bin/perl foreach (@ARGV) { print "$_\n"; }​
סקריפט ב-BASH (שלוש דרכים):
#!/bin/bash ./a.pl a d f ./a.pl "a d f" ./a.pl 'a d f'​
 

frolik

New member
הצורה שאני מטפל במשתנים בפרל :

use Getopt::Std; my %options; getopts("F:f:S:s:m:t:", \%options); $mailfrom = $options{F}; # real Email address of the sender $from =$options{f}; # the display name of the sender $smtpserver = $options{s}; # IP of the smtp server $subject = $options{S}; # subject of the Email $msg =$options{m}; # the message of the Email $mailto = $options{t}; # real email address of the recipient a.pl -f "bla bla" -m "message " .... הצרה החדשה שהוא מזהה את הרווחים כפרמטר חדש למה ואיך אפשר להתגבר על זה ?
 

annefan

New member
אתה בטוח

הוספתי פקודת
print "$from\n";​
והוא מדפיס "bla bla", כלומר לא קוטע את המשתנה לפי רווח. (הערה, תשתדל להשתמש ב"תחילת קוד" ו"סוף קוד" בהודעות שלך בתפוז, כדי להגדיל קריאות)
 

frolik

New member
כן

תעשה bash script שקורא לסקריפט פרל ותנסה להעביר משתים עם רווחים
 

annefan

New member
ב-CYGWIN

הוא מדפיס את הפרמטר כמו שצריך. תעלה קוד לדוגמא מדויק.
 

frolik

New member
הקוד

use Getopt::Std; my %options; getopts("F:f:S:s:m:t:", \%options); $mailfrom = $options{F}; # real Email address of the sender $from =$options{f}; # the display name of the sender $smtpserver = $options{s}; # IP of the smtp server $subject = $options{S}; # subject of the Email $msg =$options{m}; # the message of the Email $mailto = $options{t}; # real email address of the recipient print "\nmailfrom = $mailfrom\n"; print "from= $mailfrom\n"; print "to= $mailto\n"; print "whoisme= $from\n"; print "subject= $subject\n"; print "msg= $msg\n"; print "smtpsrver= $smtpserver \n"; print "Content-type: text/plain", "\n\n";
 

frolik

New member
מחדש

use Getopt::Std; my %options; getopts("F:f:S:s:m:t:", \%options); $mailfrom = $options{F}; # real Email address of the sender $from =$options{f}; # the display name of the sender $smtpserver = $options{s}; # IP of the smtp server $subject = $options{S}; # subject of the Email $msg =$options{m}; # the message of the Email $mailto = $options{t}; # real email address of the recipient print "\nmailfrom = $mailfrom\n"; print "from= $mailfrom\n"; print "to= $mailto\n"; print "whoisme= $from\n"; print "subject= $subject\n"; print "msg= $msg\n"; print "smtpsrver= $smtpserver \n"; print "Content-type: text/plain", "\n\n";​
וה- bash script :
./sendmail.pl -F "[email protected]" -f "My name<[email protected]>" -s localhost -t "[email protected]" -m "message data how can i send new line ? " -S "test one"​
הבעיה שהפרל מזהה את הרווחים כמשתנים שונים ולא כתוכן של משתנה אחד. תודה
 

annefan

New member
זה הפלט אצלי (cygwin ולינוקס)

$ ./a.sh mailfrom = [email protected] from= [email protected] to= [email protected] whoisme= My name<[email protected]> subject= test one msg= message data how can i send new line ? smtpsrver= localhost Content-type: text/plain​
ובלינוקס: נראה מצוין.
>./a.sh mailfrom = [email protected] from= [email protected] to= [email protected] whoisme= My name<[email protected]> subject= test one msg= message data how can i send new line ? smtpsrver= localhost Content-type: text/plain​
נראה מצוין
 
למעלה