简体   繁体   中英

Convert unix to windows shell script

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?

Two issues:

  1. The "loop variable" can only have one character. Try using just %%s for example. Note that depending on the type of the FOR loop the names have a meaning (see FOR /? for more information); for example when used with `FOR /F "tokens=..."``. In your case it shouldn't matter though.
  2. The opening brace must be on the same line as the DO

Complete example:

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)
)

Hint: you can use a caret ( ^ ) as line continuation character to avoid overly long lines. Just make sure there is really no other character (not even whitespace) after the caret.

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)
)

EDIT As commenter @dbenham points out, the line continuation inside above are not actually neccessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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