如何将Unix shell脚本修改为Windows批处理文件?
问题描述:
我需要将以下Unix脚本转换为在Windows上运行。我假设cd
命令将更改为拨打dir
,echo
命令更改为拨打print
。如何将Unix shell脚本修改为Windows批处理文件?
脚本检查给定文件的目录,如果该文件不存在,则脚本循环/休眠,然后再次检查。
我主要是努力如何最好地转换变量和循环命令,任何提示/提示将不胜感激?
cd $DIRECTORY_NAME
echo "You are currently in $(pwd) directory"
numfiles=0
while [ $numfiles -lt "1" ]
do
export filename=`ls SWIX_BAU_success`
export numfiles=$(echo $filename | wc -w)
echo "$numfiles file found"
if [ $numfiles -ge "1" ]
then
echo "$filename file found"
exit 0
else
echo "Incorrect number of files found, 1 file expected"
numfiles=0
sleep 300
fi
done
答
我只是在撤销这个问题时发布这个答案。
假设SWIX_BAU_success
是只有一个文件的(子)目录。
@Echo off
cd /D "%DIRECTORY_NAME%"
echo You are currently in directory: %CD%
Set numfiles=0
For /f "tokens=1,* delims=:" %%A in (
'Dir /B "SWIX_BAU_success" ^| findstr /n^'
) DO Set numfiles=%%a&Set filename=%%B
If %numfiles% equ 1 (
echo %filename% found
exit /B 0
) else (
echo "Incorrect number of files found, 1 file expected"
set numfiles=0
Timeout /T 300
)
SS64有两个像样的参考文献[Windows批处理('cmd')(https://ss64.com/nt)和['bash'(https://ss64.com/bash/) 。在进行转换之前,您可能需要检查它们;您可能还想考虑使用PowerShell而不是Windows批处理。 –
请阅读[为什么“有人可以帮我吗?”不是一个真正的问题?](https://meta.*.com/q/284236/354577) – Chris