How can I print out a variable in batch file

Microsoft Windows xp professional editio...
October 6, 2009 at 10:35:57
Specs: Windows XP
Hi,

Please show me how to print out the below line in batch programming:

perl -e "sleep (rand 30)"

I tried:
set random_sec=perl -e "sleep (rand 30)"
echo %random_sec"

and it does not work.

Thanks for helping.
Codelearner


See More: How can I print out a variable in batch file

Report •


#1
October 6, 2009 at 12:25:03
Codelearner,

You need to enclose the random_sec variable with "%".

Like this:

echo %random_sec%

-- kptech


Report •

#2
October 6, 2009 at 14:25:44
I tried
echo random_sec = %random_sec%

and its output is nothing as shown below:

random_sec=


Report •

#3
October 6, 2009 at 18:40:42
No problems setting the var and echoing here, perhaps you should check the syntax you are using in the Set command..

You have a space before the = in this
echo random_sec = %random_sec%

but it's not shown on the output
random_sec=


Report •

Related Solutions

#4
October 7, 2009 at 14:45:42
Here is my code:
set number_of_loop=%1
set /a counter=0

rem Number of loop must be enter:
if "%number_of_loop%"=="" (
goto error
)

:loop
set /a counter+=1
set random_sec=perl -e "sleep (rand 30)"
echo random_sec=%random_sec%
echo Number of loops=%counter%
if "%counter%"=="%number_of_loop%" (
goto end
) else (
goto loop
)


:error
echo Need to enter number of loop
echo Usage: Script_Name number_of_loop


:end

Result:
C:\WorkingFolderTemp>set number_of_loop=1

C:\WorkingFolderTemp>set /a counter=0

C:\WorkingFolderTemp>rem Number of loop must be enter:

C:\WorkingFolderTemp>if "1" == "" (goto error )

C:\WorkingFolderTemp>set /a counter+=1

C:\WorkingFolderTemp>set random_sec=perl -e "sleep (rand 30)"

C:\WorkingFolderTemp>echo random_sec=perl -e "sleep (rand 30)"
random_sec=perl -e "sleep (rand 30)"

C:\WorkingFolderTemp>echo Number of loops=1
Number of loops=1

Problem: cannot print out the random second


Report •


Ask Question