LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-09-2011, 12:20 PM   #1
tate89
LQ Newbie
 
Registered: Aug 2011
Posts: 14

Rep: Reputation: Disabled
Question Trying to Run a Bash Script


I need this script to loop through a folder of images on my desktop on windows and change the pixels and format from .sid to .tif using Gdal.It wont run can use please help. I have cygwin on my pc but this is my first time using this program.

Last edited by tate89; 08-11-2011 at 04:10 PM.
 
Old 08-09-2011, 01:00 PM   #2
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 390

Rep: Reputation: 150Reputation: 150
Hello there and welcome.

I'm afraid I don't know about cygwin, but I just tried your script and it runs fine. Just a suggestion, you don't need ls to list files in the for loop, bash does filename expansion:

Code:
for f in *.sid; do
Oh, another thing. If your filenames contain spaces, quote the variables:

Code:
gdal_translate -of GTiff -outsize 31238 39915 "$f" "res_$f"
Quote:
Originally Posted by tate89 View Post
When I try in python idle in says invalid syntax.
I'm afraid python doesn't understand bash syntax. Use bash instead. Perhaps this thread will help you.
 
1 members found this post helpful.
Old 08-09-2011, 01:12 PM   #3
tate89
LQ Newbie
 
Registered: Aug 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks. So now I can stop wasting my time in Python. I guess the real problem is that I am new to programming. Do you have any suggestions on how I can run the script instead of using Cygwin.
 
Old 08-09-2011, 01:31 PM   #4
tate89
LQ Newbie
 
Registered: Aug 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
When i ran the script in Cygwin it gives me the following:

>#!/bin/bash
"#!" is not recognized as an internal or external command operable program or batch file.

>x=0
"x" is not recognized as an internal or external command operable program or batch file. f was unexpected at this time.

>for f in *.sid; do
 
Old 08-09-2011, 01:33 PM   #5
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 390

Rep: Reputation: 150Reputation: 150
Did you read this thread? Maybe that's all you need, give it a try.
 
Old 08-09-2011, 01:37 PM   #6
tate89
LQ Newbie
 
Registered: Aug 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks Diantre I did. I received the errors above.
 
Old 08-09-2011, 04:22 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by tate89 View Post
for f in $( ls *.sid); do x=`expr $x + 1`
Why use ls, why not just "*.sid"? Using ls just makes things more complicated and breaks support for filenames with spaces.

Also, avoid backticks, usr $(command) instead. It's easy to nest and can't be confused with single quotes.

Finally, there's no need to use expr. bash can do arithmetic:

Code:
x=$(( x + 1 ))
Quote:
Originally Posted by tate89 View Post
# echo number of file we are on + name of file echo $x $f

# perform analysis, output to new file gdal_translate -of GTiff -outsize 31238 39915 $f res_$f
Did you intentionally comment out the commands? Why did you write text in front of them?

Quote:
Originally Posted by tate89 View Post
When I try in python idle in says invalid syntax. And im totally lost with cygwin.
What makes you think you can run a bash script with Python? That makes about as much sence as trying to compile a Python program with a C compiler. Run it like this:

Code:
bash path/to/script
 
Old 08-10-2011, 01:51 PM   #8
tate89
LQ Newbie
 
Registered: Aug 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Command not found

Thanks MTK358

I found out that I didn't have the complete Cygwin package downloaded so I re-ran the script as is above.


Now it is saying
line 4: $'\r': command not found
line 9: gdal_translate:command not found
Can someone please help me correct this problem?

Last edited by tate89; 08-11-2011 at 04:11 PM.
 
Old 08-10-2011, 02:01 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by tate89 View Post
for f in $(ls *.sid); do
Again, you don't need ls. It just makes the script more complex and makes it fail when there are spaces in filenames. Just replace it with this:

Code:
for f in *.sid; do
Quote:
Originally Posted by tate89 View Post
x='expr $x+1'
You're not running the command here, you're just setting the varaible x to the string "expr $x+1". Also, you need to separate the "$x", "+", and "1" parts into separate arguments. Use this instead:

Code:
x=$(expr $x + 1)
Or even better, use bash's built-in arithmetic. It's better because it doesn't need to launch another process:

Code:
((x++))
 
Old 08-10-2011, 02:25 PM   #10
tate89
LQ Newbie
 
Registered: Aug 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
I made the changes and I am still getting the same outputs.
 
Old 08-10-2011, 03:27 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Try running this command and then running the script again:

Code:
sed -i 's/\r//g' path/to/your/script
 
1 members found this post helpful.
Old 08-10-2011, 04:32 PM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I see you marked the above post as helpful. Did it fix your problem?

Anyway, what is does is convert Windows newlines (CRLF) to Unix newlines (LF) by removing all the CR characters from the file. When you write scripts, it's probably a good idea to either use a Unix editor or a Windows editor that lets you set it to use Unix newlines.
 
Old 08-10-2011, 04:46 PM   #13
tate89
LQ Newbie
 
Registered: Aug 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Yes it did. Thanks again. Now I am trying to figure out how to get Cygwin to read the gdal command gdal_translate.
Also today I learned to use dos2unix and I downloaded Vim to convert.
 
Old 08-10-2011, 07:24 PM   #14
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 607

Rep: Reputation: 127Reputation: 127
Locate where (in which folder) you have put gdal, gdal_translate etc.

Change the script to prefix the folder name to the gdla_command:
/folder_name_of_gdal/gdal_translate -of GTiff -outsize 31238 39915 "$f" "res_$f"

OK
 
1 members found this post helpful.
  


Reply

Tags
bash scripting, cygwin


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
[SOLVED] Partial list with ls-l in bash script run in cron but full list run from command line redgshost Linux - General 29 01-16-2011 12:14 PM
My BASH script work perfect when i run it manualy, but not when it run in the crontab roqarg Linux - Newbie 30 05-06-2010 01:37 PM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
how to run one bash script from within another? babag Programming 9 04-28-2005 12:12 AM


All times are GMT -5. The time now is 11:55 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration