LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-19-2014, 01:06 PM   #1
aehanes
LQ Newbie
 
Registered: Sep 2014
Posts: 7

Rep: Reputation: Disabled
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" \
 
Old 09-19-2014, 01:33 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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?
 
1 members found this post helpful.
Old 09-19-2014, 01:36 PM   #3
aehanes
LQ Newbie
 
Registered: Sep 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
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!

Last edited by aehanes; 09-19-2014 at 01:39 PM.
 
Old 09-19-2014, 01:40 PM   #4
aehanes
LQ Newbie
 
Registered: Sep 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-19-2014, 01:44 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
1 members found this post helpful.
Old 09-19-2014, 01:52 PM   #6
aehanes
LQ Newbie
 
Registered: Sep 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-19-2014, 01:56 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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
 
1 members found this post helpful.
Old 09-19-2014, 02:01 PM   #8
aehanes
LQ Newbie
 
Registered: Sep 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-19-2014, 02:03 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.

Last edited by suicidaleggroll; 09-19-2014 at 02:05 PM.
 
1 members found this post helpful.
Old 09-19-2014, 02:10 PM   #10
aehanes
LQ Newbie
 
Registered: Sep 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-19-2014, 02:15 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
1 members found this post helpful.
Old 09-19-2014, 02:23 PM   #12
aehanes
LQ Newbie
 
Registered: Sep 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-19-2014, 02:37 PM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
Old 11-27-2018, 12:09 PM   #14
Otp
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Rep: Reputation: 0
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.

Last edited by rtmistler; 11-27-2018 at 12:23 PM.
 
Old 11-27-2018, 12:17 PM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Otp View Post
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to write a batch file in linux?and what are batch(.bat) file benefits? karthilin Linux - Newbie 8 10-15-2012 02:10 PM
.bat file conversion to .sh file (debian) AlanFletcher Programming 7 04-19-2012 06:47 PM
[SOLVED] Converting a HTML file to a PNG file through Python script Aquarius_Girl Programming 12 02-02-2010 10:10 AM
How to convert a batch file(.bat) to script file(.sh) manas_sem Programming 4 06-28-2007 12:10 PM
renaming or converting a text file to a dat file... tangaz Linux - Software 1 10-24-2003 06:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration