Solved

how to return a value from DOS shell function?

Posted on 2009-07-11
9
585 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 69

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 69

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
7-Zip Command Line Issues 12 91
xxcopy batch file to move specific client folders using an input text file. 21 197
robocopy vs xcopy vs copy 8 110
file size 9 51
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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