Parentheses block causing error


D

David Trimboli

I was writing a simple pinging interactive prompt to make pinging a lot
of machines more convenient. I wanted to include a shell escape with the
usual "!" command. One thing doesn't work: if I just type "!", I get an
error, ") was unexpected at this time." Why is this happening, and how
do I fix it? Do I need to escape something? (Windows XP SP3)

::::::::::Sample Output::::::::::

H:\>pinger
Pinger
Enter a host name to ping. "Exit" to exit.
stormcrow Reply from 143.48.3.46
!cd
H:\
Press any key to continue . . .
) was unexpected at this time.

H:\>

::::::::::The Script:::::::::

@echo off
setlocal enableextensions enabledelayedexpansion
echo Pinger!
echo Enter a host name to ping. "Exit" to exit.
:prompt
set /p "host=> "
if /i "%host%" equ "exit" goto :eof
if "%host:~0,1%" equ "!" goto shell
for /f "tokens=1-3* delims=: " %%i in ('ping -n 1 %host%') do (
if "%%i" equ "Pinging" set IP=%%k
if "!IP!" equ "with" set IP=%%j
if "%%i" equ "Reply" echo Reply from %%k
if "%%i" equ "Request" echo Request timed out. !IP!
if "%%j" equ "request" echo Host !host! not found.
)
goto prompt
:shell
if "%host:~0,1%" equ "%host%" (
cmd
) else (
%host:~1%
)
pause
goto :prompt
 
Ad

Advertisements

P

Pegasus

David Trimboli said:
I was writing a simple pinging interactive prompt to make pinging a lot of
machines more convenient. I wanted to include a shell escape with the usual
"!" command. One thing doesn't work: if I just type "!", I get an error, ")
was unexpected at this time." Why is this happening, and how do I fix it?
Do I need to escape something? (Windows XP SP3)

::::::::::Sample Output::::::::::

H:\>pinger
Pinger
Enter a host name to ping. "Exit" to exit.
H:\
Press any key to continue . . .
) was unexpected at this time.

H:\>

::::::::::The Script:::::::::

@echo off
setlocal enableextensions enabledelayedexpansion
echo Pinger!
echo Enter a host name to ping. "Exit" to exit.
:prompt
set /p "host=> "
if /i "%host%" equ "exit" goto :eof
if "%host:~0,1%" equ "!" goto shell
for /f "tokens=1-3* delims=: " %%i in ('ping -n 1 %host%') do (
if "%%i" equ "Pinging" set IP=%%k
if "!IP!" equ "with" set IP=%%j
if "%%i" equ "Reply" echo Reply from %%k
if "%%i" equ "Request" echo Request timed out. !IP!
if "%%j" equ "request" echo Host !host! not found.
)
goto prompt
:shell
if "%host:~0,1%" equ "%host%" (
cmd
) else (
%host:~1%
)
pause
goto :prompt
Use a subroutine in order to avoid the delayed expansion mechanism. Your
exclamation marks are then treated as ordinary characters. Here is a
modified but untested version of your batch file:
@echo off
setlocal enableextensions
echo Pinger!
echo Enter a host name to ping. "Exit" to exit.
:prompt
set /p "host=> "
if /i "%host%" equ "exit" goto :eof
if "%host:~0,1%" equ "!" goto shell
for /f "tokens=1-3* delims=: " %%i in ('ping -n 1 %host%') do call :Sub %%i
%%j %%k
goto prompt

:shell
if "%host:~0,1%" equ "%host%" (cmd) else (%host:~1%)
pause
goto :prompt

:Sub
if "%1" equ "Pinging" set IP=%3
if "%IP%" equ "with" set IP=%2
if "%1" equ "Reply" echo Reply from %3
if "%1" equ "Request" echo Request timed out. %IP%
if "%2" equ "request" echo Host %host% not found.
 
T

Timo Salmi

D

David Trimboli

Use a subroutine in order to avoid the delayed expansion mechanism.
Your exclamation marks are then treated as ordinary characters.
Oh! You mean the "!" that gets typed in gets treated as a delayed
expansion marker? Yow!

Right, I'll try out the subroutine and see how that goes.
 
D

David Trimboli

David said:
Oh! You mean the "!" that gets typed in gets treated as a delayed
expansion marker? Yow!

Right, I'll try out the subroutine and see how that goes.
No, the same thing happens. It fails at the line

if "%host:~0,1%" equ "%host%" (cmd) else (%host:~1%)

with

) was unexpected at this time.

It only fails when the only input is "!".

In fact, now that I think about it, it never gets to the FOR statement.
The IF statement right before it recognizes that the first character of
the input string is "!" and goes to :shell. I know this works, because
input of, say, "!cd" works correctly.
 
D

David Trimboli

David said:
No, the same thing happens. It fails at the line

if "%host:~0,1%" equ "%host%" (cmd) else (%host:~1%)

with

) was unexpected at this time.

It only fails when the only input is "!".

In fact, now that I think about it, it never gets to the FOR statement.
The IF statement right before it recognizes that the first character of
the input string is "!" and goes to :shell. I know this works, because
input of, say, "!cd" works correctly.
As a further test, I just changed the script to let "x" be the shell
command. The same error occurs. It's not because of the "!".
 
Ad

Advertisements

D

David Trimboli

David said:
I was writing a simple pinging interactive prompt to make pinging a lot
of machines more convenient. I wanted to include a shell escape with the
usual "!" command. One thing doesn't work: if I just type "!", I get an
error, ") was unexpected at this time." Why is this happening, and how
do I fix it? Do I need to escape something? (Windows XP SP3)

::::::::::Sample Output::::::::::

H:\>pinger
Pinger
Enter a host name to ping. "Exit" to exit.
H:\
Press any key to continue . . .
) was unexpected at this time.

H:\>

::::::::::The Script:::::::::

@echo off
setlocal enableextensions enabledelayedexpansion
echo Pinger!
echo Enter a host name to ping. "Exit" to exit.
:prompt
set /p "host=> "
if /i "%host%" equ "exit" goto :eof
if "%host:~0,1%" equ "!" goto shell
for /f "tokens=1-3* delims=: " %%i in ('ping -n 1 %host%') do (
if "%%i" equ "Pinging" set IP=%%k
if "!IP!" equ "with" set IP=%%j
if "%%i" equ "Reply" echo Reply from %%k
if "%%i" equ "Request" echo Request timed out. !IP!
if "%%j" equ "request" echo Host !host! not found.
)
goto prompt
:shell
if "%host:~0,1%" equ "%host%" (
cmd
) else (
%host:~1%
)
pause
goto :prompt
Aha! I've figured it out. If the input is "!", then

if "%host:~0,1%" equ "%host%" (cmd) else (%host:~1%)

evaluates as

if "!" equ "!" (cmd) else ()

It's the empty parentheses in the else clause that are causing the
error. I'll just rewrite this bit so that there is never an empty else
clause.
 
B

billious

David Trimboli said:
No, the same thing happens. It fails at the line

if "%host:~0,1%" equ "%host%" (cmd) else (%host:~1%)

with

) was unexpected at this time.

It only fails when the only input is "!".

In fact, now that I think about it, it never gets to the FOR statement.
The IF statement right before it recognizes that the first character of
the input string is "!" and goes to :shell. I know this works, because
input of, say, "!cd" works correctly.
If your input is simply a single exclamation mark, then
if "%host:~0,1%" equ "%host%" (cmd) else (%host:~1%)

resolves to

if "!" equ "!" (cmd) else ()

This is the source of the error - the empty ELSE

How about

if "%host:~0,1%" equ "%host%" cmd&goto prompt
%host:~1%
goto prompt
 
Ad

Advertisements

D

David Trimboli

billious said:
If your input is simply a single exclamation mark, then
if "%host:~0,1%" equ "%host%" (cmd) else (%host:~1%)

resolves to

if "!" equ "!" (cmd) else ()

This is the source of the error - the empty ELSE

How about

if "%host:~0,1%" equ "%host%" cmd&goto prompt
%host:~1%
goto prompt
My final solution was

if "%host:~0,1%" equ "%host%" (cmd) else (cmd /c %host:~1%)

Cmd doesn't mind if it's run with the /c parameter and no script argument.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top