We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

how to return a value from DOS shell function?

jl66
jl66 asked
on
Medium Priority
693 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"


Comment
Watch Question

Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
jl66Consultant

Author

Commented:
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.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:
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.
jl66Consultant

Author

Commented:
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?
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
jl66Consultant

Author

Commented:
Thank all of you very much. I will verify the solutions later.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.