繁体   English   中英

将Unix转换为Windows Shell脚本

[英]Convert unix to windows shell script

我正在尝试将以下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.

什么是正确的转换?

两个问题:

  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指出的那样,上面的行连续实际上不是必需的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM