Solved

how to return a value from DOS shell function?

Posted on 2009-07-11
9
569 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
  • 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
 
LVL 68

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 68

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now