473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need return code from perl -e command

Hi folks,
I have a perl one-liner embedded in a ksh script.

perl -pi.bak -e "s/val/otherval/" inputfile

I'd like to check the return code to know
if the substitution was successful.

If I type:
# perl -pi.bak -e "s/val/otherval/" inputfile
Can't open inputfile: No such file or directory
# echo $?
0

If the file is found i still get a return code of 0
# perl -pi.bak -e "s/val/otherval/" realfile
# echo $?
0

Any ideas ?

Many thanks
Paul
Jul 19 '05 #1
2 7461
Hi Alex,
thanks a lot for the reply.
That command doesn't seem to work for me.
It's partially OK if the string is found
i.e.
# perl -pi.bak -e '$c+=s/test/newval/; END {exit !$c;}' /tmp/testfile
# more /tmp/testfile
0newval
N.B. The return code 0 has been pasted into the file.
# echo $?
0

If there are no substitutions:
# perl -ne '$c+=s/echo/newval/; END {exit !$c;}' /tmp/testfile
Callback called exit, <> chunk 1.
END failed--cleanup aborted, <> chunk 1.
# echo $?
255

Any ideas how to avoid putting the return code in the file ?

Thanks again
Paul

Chillies <bo****@fake.do main> wrote in message news:<bu******* ***@newsg4.svr. pol.co.uk>...
Paul Porcelli wrote:
Hi folks,
I have a perl one-liner embedded in a ksh script.

perl -pi.bak -e "s/val/otherval/" inputfile

I'd like to check the return code to know
if the substitution was successful.


The s// command returns the number of characters
substituted, and you can use exit fro the retval.

perl -ne '$c+=s/val/newval/; END {exit !$c;}'

returns 0 if successful, 1 if not.
Paul


Alex
Sorry, my newsfeed doesn't have comp.lang.perl %^|

Jul 19 '05 #2
Paul Porcelli wrote:
Hi folks,
I have a perl one-liner embedded in a ksh script.

perl -pi.bak -e "s/val/otherval/" inputfile

I'd like to check the return code to know
if the substitution was successful.


Hi, I was replying to this the other day, when my laptop crashed! Let's
try take two now...

Perl returns the result of the last evaluated expression. perl -p is
equivalent to include all code between a while (<>) {}, so if the file
exists it enters the while loop, does it's work, and returns when <>
returns undef. Therefor here the last evaluated expression is <> that
have returned undef (or something) -- perl exits 0. When the file does
not exist, the <> implicitly returns undef again (open on a non existent
file).

To actually have a different return code based on taking or not some
action, one must consider that case in the script. Two examples:

#1 exit via the $! perl var
perl -i -p -e '$!=0; s/foo/bar/ END { return !!$! }' files

#2 take some var to control the number of processed lines
perl -i -p -e '$c++; s/foo/bar/ END { return !$c }' files

Both has some problems, for instance, the first will return false if the
lsat file does not exists (independently of the first and second, etc);
the later will return false if no file has lines.

--
carlos ** http://cgd.sdf-eu.org
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8135
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: ------------------------------------- test3.pl ------------------------------------- print "PERL Hello from Perl! (plain print)<br>\n"; print STDERR "PERL This is text sent to STDERR<br>\n"; $output="PERL Some output:<br>\n"; for ($i=0; $i<5; $i++) {
2
9920
by: brad | last post by:
I am new to perl and would like to know how to check the return values of a perl program in a batch file. how can I save the return value of the perl program and use it to make decisions as to what I should do next in the batch file?
3
15281
by: FLOTServer | last post by:
Here's my problem: I run a gameserver that runs the game "Medal of Honor". On the game server is log file which contains all of the data from the players for that day (kills, deaths, etc...). I have a perl script that runs on my webserver, which is supposed to login to the gameserver and download the log. The problem is that when it gets to the part where it needs to download the file, it aborts. The gameserver FTP requires I use port 24...
3
4985
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be to use the 'Shell()'-command . This didn't work: Either I get an "File not found" error, or I get no error at all, but the PERL script isn't executed anyway. The strange thing is that I managed to execute a .bat file for example. Why the hell...
2
2937
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server where there is no scripts allowed running (the software is from Opentext called Livelink)- therefore I need javascript to do the tasks listed below: 1. validate the form - this has been completed 2. pop up another window that will go ahead and...
3
4135
by: sir.linying | last post by:
My php script is to call perl scipt which makes use of Spreadsheet::ParseExcel module to parse Excel file. I am able to launch php script from command line so that perl script can run and properly parse Excel file. However, when I put php script on server side and then launch php through client request, perl program can't run. Anyone has any suggestion re how to fix this problem? Thanks a lot, -Ying
20
4283
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
3
2343
by: uzzi | last post by:
I don't know how to make a php script to work via cron...I want to make it run daily...My server API is CGI/Fast CGI.I have hosting from godaddy and in the help section i found that i have to specify the interpreter manually...look what it says: i've tried with /web/cgi-bin/php "$HOME/html/path/to/my/script.php as command and it doesn't work. if i run the script in browser it works...what should i introduce as a command..and do i have...
2
1715
by: jonathan184 | last post by:
Hi basically what i want this script to do is In a particular dir there are alot of files from today back to 1999 or earlier. So I am trying to have the script search this dir and sort the files by create date of the file and put in dirs by year and the only data iw ant left in the source dir is this months only and when moving to next month i want only that month and so forth. . for e.g is the create date has a year of 2004 I want it to...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.