Solved

how to return a value from DOS shell function?

Posted on 2009-07-11
9
614 Views
Last Modified: 2012-05-07
The following just lists what intends to be achieved (not working code):
set val1=1
set str1=test
call :getVal  %val1%  %str1%
@echo value=%val2% for %str2%
goto :eof

:getVal
set /a val2 = 2+%1
set str2="Easy %2"
goto :eof

The output is expected to be
3 for "Easy test"


0
Comment
Question by:jl66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 200 total points
ID: 24832509
It works fine when I test it.

Are you using SETLOCAL somewhere in your batch file?

ALso, val2 and str2 are defined in getval. It appears you want to use val2 and str2 as global variables. The proper way to do what you are doing in your example batch file is as follows:

   @echo off
   set val1=1
   set str1=test
   call :getVal  %val1%  %str1%
   exit /b

   :getVal
   set /a val2 = %1 + 2
   set str2="Easy %2"
   echo value=%val2% for %str2%
   exit /b
0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 200 total points
ID: 24832612
I guess what you're really attempting to do is pass values back from GetVal.

Defining GetVal as a sub-procedure can be likened to having two separate batch files. The problem then becomes more clearer. That is, how do we pass out values back to the perent batch file?

The way to do this is to pass them via the Environment Variable Table as in the following two batch files:.


BATCH.BAT

   @ECHO OFF
   SET Val1=1
   SET Str1=test

   SET Val2=
   SET Str2=

   CALL GetVal  %Val1%  %Str1%
   ECHO Value=%Val2% for %Str2%
   EXIT /B


GETVAL.BAT

   SET /A Val2 = %1 + 2
   SET Str2="Easy %2"
   EXIT /B


I've included 'SET Val2=' and 'SET Str2=' to reset the variables. This ensures we're not picking up spurious data.

There are other ways of passing data back to a parent batch file but this is the simplest way.
0
 

Author Comment

by:jl66
ID: 24833700
Thanks for inputs. Is there anyway not to write two batch files to achieve the same thing? If I did not setlocal, all the variables are global, right? But try to return the values from a function is difficult.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 70

Expert Comment

by:Qlemo
ID: 24833734
Your code posted above works.

t0t0,
your examples do not make any difference, as the scope of environment variables is the whole cmd.exe process, as long as you do not use setlocal.
0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 200 total points
ID: 24833842
Sure, writing two batch files was just a way of demonstrating that values can be returned via the environment table.

If you look at the following code (taken from my first comment) this should work perfectly well as a single batch file.

   @echo off
   set Val1=1
   set Str1=test
   call :GetVal  %Val1%  %Str1%
   exit /b

   :GetVal
   set /a Val2 = %1 + 2
   set Str2="Easy %2"
   echo Value=%Val2% for %Str2%
   exit /b

In some circumstances you can use setlocal however, variables are global by default and the above code should work for you if you copy and paste it straight into Notepad and save it as a batch file.

Returning a value from a function should be no more difficult than returning a value from a child batch file.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24833852
Qlemo

Which is the very point I'm trying to get across to the asker. I was expecting the asker to copy and paste the code (from both examples) and report back that they didn't work - or that only the batch file making a call to a function didn't work because there's no reason why his own code shouldn't return the correct results. This would have then warranted deeper investigation.
0
 

Author Comment

by:jl66
ID: 24838986
Sorry I forgot the first line is
setlocal enabledelayedexpansion

Is this the reason I could not get the values of its functions (like GetVal) in main program? I have to put this line in. Is there anyway to write one batch script and get the value in its function?
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 300 total points
ID: 24839053
The setlocal does not influence the behaviour in your code as long as you are not setting it in the GetVal. setlocal EnableDelayedExpansion sets the Delayed Expansion option, and makes all env var changes private (or local).
If you set it as first code line, it's scope is the current batch, and this works. But you can't export the var to your surrounding environment (that easy). t0t0 once showed a trick how to do it though. The code below will export val2 and str2 to be used outside the batch file.



@echo off
setlocal EnableDelayedExpansion
set val1=1
set str1=test
call :getVal  %val1%  %str1%
@echo value=%val2% for %str2%
endlocal & set val2=%val2%& set str2=%str2%
exit /b
 
:getVal
set /a val2 = 2+%1
set str2="Easy %2"
exit /b

Open in new window

0
 

Author Comment

by:jl66
ID: 24856797
Thank all of you very much. I will verify the solutions later.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question