Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I am taking a GIS Programming class. I use a MAC but the class is written for windows. I am having a bit of trouble converting the .bat file to a .sh file to run in terminal. Any help is greatly appreciated!!
The .bat file that was provided to me.
Code:
cd /d c:\data\PhiladelphiaBaseLayers
set ogr2ogrPath="c:\program files\QGIS Dufour\bin\ogr2ogr.exe"
for %%X in (*.shp) do %ogr2ogrPath% -skipfailures -clipsrc c:\data\PhiladelphiaBaseLayers\clipFeature\city_limits.shp c:\data\PhiladelphiaBaseLayers\clipped\%%X c:\data\PhiladelphiaBaseLayers\%%X
for %%X in (*.shp) do %ogr2ogrPath% -skipfailures -s_srs EPSG:4326 -t_srs EPSG:3857 c:\data\PhiladelphiaBaseLayers\clippedAndProjected\%%X c:\data\PhiladelphiaBaseLayers\clipped\%%X
My .sh file I have been working on.
Code:
#!/bin/bash -x
cd ~/Desktop/PhiladelphiaBaseLayers
PATH=$ogr2ogrPATH:"/Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py"$ogr2ogrPath \
for shpfile in *.shp; do $org2ogrPath -skipfailures -clipsrc \ ~/Desktop/PhiladelphiaBaseLayers/clipFeature/city_limits.shp \ ~/Desktop/PhiladelphiaBaseLayers/clipped/"$shpfile" \ ~/Desktop/PhiladelphiaBaseLayers/"$shpfile" \
for shpfile in *.shp; do $ogr2ogrPath -skipfailures -s_srs EPSG:4326 -t_srs EPSG:3857 \ ~/Desktop/PhiladelphiaBaseLayers/clipped/"$shpfile" \
For the first error, I'm guessing you have a space between ogr2ogrPath and the "="? There can't be any spaces around the "=" in bash variable assignment.
For the second error, you're missing a "; done" at the end of each for loop.
If this is going in a script, there's no need or reason to put the entire for loop on one line, you should split it up for readability.
Code:
for shpfile in *.shp; do
$org2ogrPath -skipfailures -clipsrc ~/Desktop/PhiladelphiaBaseLayers/clipFeature/city_limits.shp ~/Desktop/PhiladelphiaBaseLayers/clipped/"$shpfile" ~/Desktop/PhiladelphiaBaseLayers/"$shpfile"
done
I'm also confused why you're changing into ~/Desktop/PhiladelphiaBaseLayers at the top, and then referencing everything by absolute path in the script, despite that absolute path pointing to your current directory
Code:
for shpfile in *.shp; do
$org2ogrPath -skipfailures -clipsrc clipFeature/city_limits.shp clipped/"$shpfile" "$shpfile"
done
is much easier to read, since you know you're already in ~/Desktop/PhiladelphiaBaseLayers.
As far as I can tell there's no reason to have two separate for loops either. Just loop over *.shp once and run both commands inside.
Last edited by suicidaleggroll; 09-19-2014 at 01:47 PM.
My new code...I think I will leave two for loops for now. One is creating the clipped files, the other is projecting the clipped files and I'm not sure I know how to type it with both in one. Mind looking over this for me? Also, does the ";" go after (*.shp or somewhere else? And do I need ";" anywhere else. Thank you again for helping me! I ran this code and I was still getting an error of unexpected end of line. :/ I am wondering if maybe my Path is incorrect...this is so frustrating.
Code:
#!/bin/bash -x
cd ~/Desktop/PhiladelphiaBaseLayers
ogr2ogrPath=/Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py
for shpfile in *.shp; do $org2ogrPath -skipfailures -clipsrc
/clipFeature/city_limits.shp
/clipped/"$shpfile"
/"$shpfile"
done
for shpfile in *.shp; do $ogr2ogrPath -skipfailures -s_srs EPSG:4326 -t_srs EPSG:3857
/clipped/"$shpfile"
done
Why are you putting half of the command on the for loop declaration line and then splitting up the arguments onto separate lines with leading "/"s? Please look again at how I wrote the for loop.
New and improved code?? Ok, this time I ran it but I received another error message.
clip.sh: line 7: -skipfailures: command not found
clip.sh: line 9: /Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py: Permission denied
do you think my code is incorrect? or my path is incorrect? I'm really confused. The .bat file seems so simple, I didn't think I would have too much trouble converting it. Thanks for the link too!
Code:
#!/bin/bash -x
cd ~/Desktop/PhiladelphiaBaseLayers
ogr2ogrPath=/Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py
for shpfile in *.shp; do
$org2ogrPath -skipfailures -clipsrc clipFeature/city_limits.shp clipped/"$shpfile" "$shpfile"
done
for shpfile in *.shp; do $ogr2ogrPath -skipfailures -s_srs EPSG:4326 -t_srs EPSG:3857 clipped/"$shpfile"
done
I received this error: chmod: Invalid file mode: +x/Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py
so I have typed my .sh file in a text editor and I am running it in terminal (on a Mac) does that make any difference? Like I said, I am very new to shell scripting.
I worked on this for several days. I really appreciate you helping me. This is probably the most progress I have made.
Do not edit the script, go back to what it was. ogr2ogrPath was perfect as-is.
As I said in my previous post, "That command only needs to be run once from outside of your script."
So, open a terminal, and run the exact command I posted, don't add "ogr2ogrPath=" or anything else, just run it exactly as written, then try your script again. All you're doing is adding execute permissions to the ogr2ogr.py file so that the shell will run it as a program. This only needs to be done once, ever. Once the file has execute permissions, you can execute it from within your script or on the command line or however you want.
If you run
Code:
ls -l /Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py
before and after you run the "chmod +x" command, you'll see the change it makes. The file permissions should go from something like this
Code:
-rw-rw-r--
to something like this
Code:
-rwxrwxr-x
The three groups of letters are file permissions for the 1) owner, 2) group, and 3) everyone else. Each group has r (read), w (write) and x (execute) permissions that can be set. If the file doesn't have execute permissions, then it can't be executed. "chmod" modifies file permissions, "+x" tells it to add execute permission.
THANK YOU SO MUCH!!!! It worked! I also learned several new commands! I really appreciate it! Is there anything I need to do on this forum to let everyone know how awesome you are?
You can mark the thread as solved up top in the "Thread Tools" menu. Beyond that, just clicking "Yes" next to "Did you find this post helpful" for any posts you found helpful will work.
Try this For converting you windows batch file to linux bash (shell) script. This is quite cool for simple batch files.
Post reported: spam/advertising.
Your name is OTP, and you posted to a thread that had been closed for FOUR YEARS, to post a link to a site that (amazingly) has the initials OTP. Read the LQ Rules.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.