Handling Parameters in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling Parameters in Perl
# 1  
Old 03-03-2010
Handling Parameters in Perl

Hi All,

I'm pretty new to the forum and also to UNIX. I have a requirement for which I need some help. I have a script (example.script) where I get user inputs using the read command. I would need to pass the read-fetched input to a perl command (explained below) in my script. The part which confuses me is that the input has characters like / and . which has to manipulated. Please help me on the same. Below are the files.

test.file: (Current)
--------
Code:
 
-
-
.LOGON SERVER/userid,password;
-
-

test.file: (Expected)
--------
Code:
 
-
-
.RUN FILE /home/user/my.test.logon;
-
-

example.script:
--------------
Code:
 
echo " Please enter the path to the LOGON file: \c"
read logon
 
perl -p -i -e "s/^\.LOGON.*$/.RUN FILE $logon;/g" test.file

I execute the example script as:
Code:
 
$ ksh example.script
 Please enter the path to the LOGON file: /home/user/my.test.logon

While running the above code, I am getting the below error message:

Quote:
Bareword found where operator expected at -e line 1, near "s/^\.LOGON.*$/.RUN FILE /home"
syntax error at -e line 1, near "s/^\.LOGON.*$/.RUN FILE /home"
Search pattern not terminated at -e line 1.
I hope this is because of using characters / and . in the input string without escape sequences (\). But basically I want input to be entered without escape characters, like in /home/user/my.test.logon, as it will be entered by all users. Is there a way in UNIX where I can have the input entered as-is and still I will be able to format the input before parsing it?

Any help on this is greatly appreciated.

Thanks in Advance. Let me know for any questions.
# 2  
Old 03-03-2010
Dear Friend,

The test.file contain the following content
.LOGON SERVER/userid,password;

In the script file you should use the following perl code instead of your code

Code:
 
echo " Please enter the path to the LOGON file: \c"
read logon

perl -p -i -e "s!^\.LOGON.*!.RUN FILE $logon;!g" test.file

After run the command it will display the following
Please enter the path to the LOGON file: \c

Then give the input as like
/home/user/my.test.logon

Then see the test.file content. .It is contain the following content
.RUN FILE /home/user/my.test.logon;
# 3  
Old 03-03-2010
Thanks a lot! Works like a Charm! So if we use (!) instead of (/) we do not want to escape characters like (/) and (.) ?

Please explain!
# 4  
Old 03-09-2010
Refer the following link
perlre - Perl regular expressions
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : How is file handling working here??

Hi, I have a perl script which is just reading from the file but what I would like to understand is how the counter is moving without using the loop in the script. Below are the details: $ more /tmp/abc.txt This is 0 This is 1 This is 2 This is 3 This is 4 This is 5 This is 6 This... (2 Replies)
Discussion started by: siddharthjindal
2 Replies

2. Shell Programming and Scripting

PERL error handling

I have a PERL command line embedded in a UNIX script. The script doesn't handle errors coming out of this command. I'm processing large files and occassionally I run out of disk space and end up with half a file. perl -p -e 's/\n/\r\n/g' < TR_TMP_$4 > $4 How do I handle errors coming out... (1 Reply)
Discussion started by: OTChancy
1 Replies

3. Programming

Perl help for file handling

$# some text $$ some text $@ some text $$. some text Mg1 some text Mg2 some text . . . Mg10 some text The above 10 lines are to be extracted except the lines starting from $#,$$.,... (4 Replies)
Discussion started by: baig.abdul
4 Replies

4. Infrastructure Monitoring

Perl Error Handling Problem

I can get this working, but if something is down I get an error and the script does not move on. I can not get the "else" function working. What might I be doing wrong? use SNMP::Simple my %ios = (); $list="list.list"; open(DAT, $list) || die("Can't Open List"); @raw_data=<DAT>;... (4 Replies)
Discussion started by: mrlayance
4 Replies

5. Shell Programming and Scripting

Handling parameters in Shell Functions

Hi, Please help me with the below situation where I have to handle the parameters passed to a function in a unique way. Below is the code, which I am trying to execute. I basically want to pass the parameter to a function, where I am trying to get user input into array(s). I want to name... (7 Replies)
Discussion started by: bharath.gct
7 Replies

6. Shell Programming and Scripting

special characters handling in perl

Hi, Here is my piece of code-- sub per_user_qna_detail { for($index=0;$index<@records;$index++) { if($records =~ m/^(.*)\s*Morocco.*Entering\s*Module::authenticate/) { printf "INSIDE per_user_qna_detail on LINE NO $index\n"; $Time_Stamp = $1;... (0 Replies)
Discussion started by: namishtiwari
0 Replies

7. Programming

XML Handling in Perl

Hi there, I'm newby in perl and XML. I can read and parse Xml with XML-Node upper XML::Parser, but how can I create XML tags and pack my individual data in it then send through socket. PLZ lead me :) Thanks in Advance. (1 Reply)
Discussion started by: Zaxon
1 Replies

8. Shell Programming and Scripting

Perl revers File handling

Hi Experts, I have a big text file, so I want read it at eof to upper bound !. after I use a fseek to go SEEK_END, is it possible to step up upperbound? Best Regards. Note that I'm used perl script. (2 Replies)
Discussion started by: Zaxon
2 Replies

9. Shell Programming and Scripting

Signal handling in Perl

Guys, I'm doing signal handling in Perl. I'm trying to catch ^C signal inside the script. There two scripts : one shell script and one perl script. The shell script calls the perl script. For e.g. shell script a.sh and perl scipt sig.pl. Shell script a.sh looks something like this :... (6 Replies)
Discussion started by: obelix
6 Replies

10. UNIX for Dummies Questions & Answers

file handling problem in perl......

Hi, I am opening a file......then i am wrting some data into it......and i am reopening the file again but ......i get a error cannot open file....... $::file= "\adder\testfile.txt" open(TEST1,$::file); some write operation close(TEST1) open(TEST1,$::file) 'I GET A ERROR CAN OPEN... (2 Replies)
Discussion started by: vivekshankar
2 Replies
Login or Register to Ask a Question