LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Converting a .bat file to a .sh file (https://www.linuxquestions.org/questions/linux-newbie-8/converting-a-bat-file-to-a-sh-file-4175519433/)

aehanes 09-19-2014 01:06 PM

Converting a .bat file to a .sh file
 
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" \


suicidaleggroll 09-19-2014 01:33 PM

I don't understand what this
Code:

PATH=$ogr2ogrPATH:"/Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py"$ogr2ogrPath \
is supposed to do.

Don't you just want
Code:

ogr2ogrPath=/Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py
?

And what's with all of the delimeters ("\") all over the place?

aehanes 09-19-2014 01:36 PM

Thanks, Ok, I will try it without the "\"

When I run it like you have typed above, I receive this error:

clip.sh: line 4: ogr2ogrPath: command not found
clip.sh: line 8: syntax error: unexpected end of file

Sorry, I am very new to this!

aehanes 09-19-2014 01:40 PM

When I tried it like the code you gave me, I received this error.

clip.sh: line 4: ogr2ogrPath: command not found
clip.sh: line 8: syntax error: unexpected end of file

why am I getting an unexpected end of file on line 8? I don't have anything on line 8. I am assuming my whole code is wrong?

suicidaleggroll 09-19-2014 01:44 PM

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.

aehanes 09-19-2014 01:52 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


suicidaleggroll 09-19-2014 01:56 PM

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.

Maybe this will help:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

aehanes 09-19-2014 02:01 PM

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


suicidaleggroll 09-19-2014 02:03 PM

Line 7: You misspelled ogr2ogrPath

Line 9: ogr2ogr.py doesn't have execute permissions. You can use
Code:

chmod +x /Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py
to fix it. That command only needs to be run once from outside of your script.

aehanes 09-19-2014 02:10 PM

Ok, this is how I typed this line of code (I didn't change anything else).

Code:

ogr2ogrPath="chmod +x/Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/gdal/ogr2ogr.py"
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.

suicidaleggroll 09-19-2014 02:15 PM

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.

aehanes 09-19-2014 02:23 PM

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?

suicidaleggroll 09-19-2014 02:37 PM

Glad you got it working!

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.

Otp 11-27-2018 12:09 PM

Try online batch file to shell script converter
 
Hello Guys,

Try this [link removed]

For converting you windows batch file to linux bash (shell) script. This is quite cool for simple batch files.

TB0ne 11-27-2018 12:17 PM

Quote:

Originally Posted by Otp (Post 5930727)
Hello Guys,

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.


All times are GMT -5. The time now is 06:29 PM.