我正在尝试将以下unix shell脚本转换为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
到目前为止,我提出了这个问题:
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)
)
但是我收到的错误信息只是说这里有问题
"%strWorkerDirectory" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
这里的正确转换是什么?
答案 0 :(得分:3)
两个问题:
%%s
。
请注意,根据FOR
循环的类型,名称具有含义(有关详细信息,请参阅FOR /?
);例如当与'FOR / F“使用时令牌= ...”``。在你的情况下,它应该无关紧要。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所指出的那样,上面的线路延续实际上并不是必要的。