如何在cmd中打开一个新shell,然后在新shell中运行脚本?

时间:2014-03-28 14:47:31

标签: batch-file powershell

我的电脑操作系统是win7

我想使用.bat文件打开一个新shell,然后在新shell中运行脚本

这个shell是OSGeo4W Shell

它位于“C:\ Program Files \ QGIS Dufour \ OSGeo4W.bat”

所以我用了

cd "C:\Program Files\QGIS Dufour\"
OSGeo4W.bat
cd "C:\Users\tony\Downloads\11\computingArea" 
ogr2ogr -f CSV my_csv Grid.dbf
ogr2ogr -f CSV csv Grid.dbf
addcenter.exe
ogr2ogr -f "ESRI Shapefile" my_dir ./csv/Grid.csv
copy Grid.shp my_dir
copy Grid.shx my_dir
rd my_csv /s /q
rd csv /s /q
ogr2ogr -clipsrc t.shp test.shp ./my_dir/Grid.shp
rd my_dir /s /q
ogr2ogr -f CSV wellcsv welllocation.dbf
ogr2ogr -f CSV csv test.dbf
computingArea.exe
ogr2ogr -f "ESRI Shapefile" my_dir ./csv/test.csv
rd csv /s /q
rd wellcsv /s /q
move test.shp my_dir
move test.shx my_dir
del test.dbf /q

但是,这个脚本只打开OSGeo4W Shell

请告诉我如何解决这个问题。

4 个答案:

答案 0 :(得分:3)

我有类似的问题,我设法解决这样的问题:我有一个名为script.bat的脚本文件,其中包含各种python和ogr函数。在我输入的单独的bat文件中:

call "C:\Program Files\QGIS 2.14\OSGeo4w.bat" start cmd.exe /k  script  
pause

这个bat文件在OSGeo4W Shell中调用inital script.bat。

* note1:两个bat文件必须位于同一目录中。 ** note2:在您的情况下,script.bat将包含内容:

ogr2ogr -f CSV my_csv Grid.dbf
ogr2ogr -f CSV csv Grid.dbf
addcenter.exe
ogr2ogr -f "ESRI Shapefile" my_dir ./csv/Grid.csv
copy Grid.shp my_dir
copy Grid.shx my_dir
rd my_csv /s /q
rd csv /s /q
ogr2ogr -clipsrc t.shp test.shp ./my_dir/Grid.shp
rd my_dir /s /q
ogr2ogr -f CSV wellcsv welllocation.dbf
ogr2ogr -f CSV csv test.dbf
computingArea.exe
ogr2ogr -f "ESRI Shapefile" my_dir ./csv/test.csv
rd csv /s /q
rd wellcsv /s /q
move test.shp my_dir
move test.shx my_dir
del test.dbf /q

并且单独的bat文件将是:

call "C:\Program Files\QGIS Dufour\OSGeo4w.bat" start cmd.exe /k  script 

并且两个bat文件必须位于数据目录中(C:\ Users \ tony \ Downloads \ 11 \ computingArea)。我希望这会奏效,对我有用!

答案 1 :(得分:0)

你可以试试这个:

start cmd /K echo Hello world!

它肯定适用于Windows XP,/ K运行指定的命令但保留新的cmd提示符!希望它有用!

如果您不希望保留cmd提示符,可以尝试此操作:

start cmd /C echo Hello world!

答案 2 :(得分:0)

我担心我还没有使用OSGeo4W我只是假设它使用的是Windows。无论我是否列出了一些适用于Linux的选项,具体取决于shell,然后是使用Cygwin的示例。也许它会有所帮助..

- >你可以使用gnome-terminal Gnome-terminal是终端仿真应用程序,-x指定您要执行后续命令:

gnome-terminal -x ./script.sh

- >后台当前终端的进程 您可以通过在命令行中添加&符号(&)或在运行时按Ctrl + Z来对第一个进程进行后台处理。然后输入" bg"让这个过程继续下去。

- >您可以使用xterm和后台进程:

xterm -e ./myProcess3 &

- >使用cygwin的例子

cygstart /bin/bash -li

希望我做对了,我实际上无法立即测试(新窗口构建PC)。

答案 3 :(得分:-2)

这是一个很好的问题。我也试图这样做。您可以尝试将其余脚本放在另一个.bat文件或.sh文件中,并将此文件作为参数传递给OSGeo4W.bat。脚本将是这样的:

cd "C:\Program Files\QGIS Dufour\"  
OSGeo4W.bat script_inside_the_shell.bat

cd "C:\Program Files\QGIS Dufour\"  
OSGeo4W.bat sh script_inside_the_shell.sh