0

I'm trying to convert the following unix shell script to windows:

for strWorkerDirectory in $BASEDIR/worker01/applogs $BASEDIR/worker01/filtered   $BASEDIR/worker01/filtered/error-logs $BASEDIR/worker01/filtered/logs $BASEDIR/worker01/filtered/session-logs
do
  if [ ! -d $strWorkerDirectory ]; then
    mkdir -p $strWorkerDirectory
  fi
done

So far, I came up with this:

FOR %%strWorkerDirectory IN (%BASEDIR%worker01\applogs %BASEDIR%worker01\filtered %BASEDIR%worker01\filtered\error-logs %BASEDIR%worker01\filtered\logs %BASEDIR%worker01\filtered\session-logs) DO 
( 
IF exist %%strWorkerDirectory ( echo %%strWorkerDirectory exists ) ELSE ( mkdir %%strWorkerDirectory && echo %%strWorkerDirectory created)
)

but i'm getting an error message which just says something is wrong here

"%strWorkerDirectory" kann syntaktisch an dieser Stelle nicht verarbeitet werden.

What would be the correct conversion here?

4

1 回答 1

3

两个问题:

  1. “循环变量”只能有一个字符。尝试仅%%s使用示例。请注意,根据FOR循环的类型,名称具有含义(FOR /?有关更多信息,请参阅);例如,当与`FOR /F "tokens=..."` 一起使用时。在你的情况下,它应该没关系。
  2. 左大括号必须与DO

完整示例:

FOR %%s IN (%BASEDIR%worker01\applogs %BASEDIR%worker01\filtered %BASEDIR%worker01\filtered\error-logs %BASEDIR%worker01\filtered\logs %BASEDIR%worker01\filtered\session-logs) DO (
IF exist %%s ( echo %%s exists ) ELSE ( echo mkdir %%s && echo %%s created)
)

提示:您可以使用插入符号 ( ^) 作为行继续符以避免过长的行。只需确保插入符号后确实没有其他字符(甚至没有空格)。

FOR %%s IN (%BASEDIR%worker01\applogs  ^
  %BASEDIR%worker01\filtered ^
  %BASEDIR%worker01\filtered\error-logs ^
  %BASEDIR%worker01\filtered\logs ^
  %BASEDIR%worker01\filtered\session-logs) DO (
    IF exist %%s ( echo %%s exists ) ELSE ( echo mkdir %%s && echo %%s created)
)

编辑正如评论者@dbenham 指出的那样,上面的行延续实际上并不是必需的。

于 2012-06-18T10:28:34.540 回答