LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 10-01-2012, 09:35 AM   #1
pacorabanix
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Rep: Reputation: Disabled
BASH newb - how to loop and do a repetitive list of commands?


hello,

in some work I do with java, I have to launch a program several times, with slightly different related parameters :
Code:
#!/bin/bash
java -jar testcsv.jar test-00001 0 10000000 1 > test01_0_10000000_1.csv
java -jar testcsv.jar test-00002 0 10000000 1 > test02_0_10000000_1.csv
java -jar testcsv.jar test-00003 0 10000000 1 > test03_0_10000000_1.csv
java -jar testcsv.jar test-00004 0 10000000 1 > test04_0_10000000_1.csv
...
java -jar testcsv.jar test-00010 0 10000000 1 > test10_0_10000000_1.csv
java -jar testcsv.jar test-00011 0 10000000 1 > test11_0_10000000_1.csv
java -jar testcsv.jar test-00012 0 10000000 1 > test12_0_10000000_1.csv
and I have to type again similar new commands for time to time.

I'm a complete bash newbie, but I'm sure there is some way to make some kind of loop with only the changing parameters (the little number 01, 02, 03 used two times, and also the "0", "10,000,000" and the "1" (which stand for start, end, step)

but I don't really understand the correct use of variables. In particular, I don't know how to automaticaly create a string "01" "02" "03", numbers with a padding zero on the left if necessary.

Can someone help me, like giving an example of how to do it or giving me some clue ?
 
Old 10-01-2012, 10:17 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Not too difficult:

Code:
for i in $(seq 1 12); do
    java -jar testcsv.jar test-$(printf "%05d" $i) 0 10000000 1 > test$i_0_10000000_1.csv
done
It's just a combination of a 'for' loop and a 'printf' call. The '1' and '12' in the first line are the start and end numbers respectively.

Hope this helps,
 
1 members found this post helpful.
Old 10-01-2012, 10:49 AM   #3
pacorabanix
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
thank you very much, it was exactly what I needed to know.
 
Old 10-01-2012, 01:52 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
BTW, there's no need to use the external seq command. bash can generate the numbers internally with brace expansion. Recent versions (v4+) can even do zero padding.

Code:
for i in {01..12}; do
    java -jar testcsv.jar test-000$i 0 10000000 1 > test$i_0_10000000_1.csv
done
There are several other possibilities too, such as using a c-style for loop, or a even a while loop with a counter. These can't directly do zero padding though, since numbers starting with 0 are treated as octal values in arithmetic expressions. But it's easy enough to pad them separately with printf.

Code:
for (( i=1; i<=12; i++ )); do
	printf -v j '%05d' "$i"	#pads i to 5 digits and sets it to variable j.
	<commands using $i and/or $j>
done
Here are a few useful bash scripting links. The first one in particular goes to a great tutorial, while the second is a particularly good reference site.

http://mywiki.wooledge.org/BashGuide
http://wiki.bash-hackers.org/start
http://www.linuxcommand.org/index.php
http://wiki.bash-hackers.org/scripting/newbie_traps
http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/BashFAQ
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://ss64.com/bash/
 
1 members found this post helpful.
Old 10-02-2012, 01:17 PM   #5
pacorabanix
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
thank you for your precisions, I'll definitely have a look at your links.

and thanks for bracket hint and the printf -v too ! I never noticed that you could printf to a variable
 
Old 10-04-2012, 03:51 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Glad to help out.

-v is a bash extension (a very handy one), so it's not portable across shells. It's possible that it's available in ksh/zsh too, but I haven't checked that.
 
  


Reply


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] Loop through list of URLs in txt file, parse out parameters, pass to wget in bash. dchol Linux - Newbie 16 07-27-2011 03:19 PM
BADLY NEEDED! comprehensive list of (suse) bash commands witecat Linux - Newbie 6 01-07-2006 10:07 AM
List of Bash Commands Penguin of Wonder Linux - Newbie 13 01-06-2006 11:09 AM
Bash Commands List by Function? TBennettcc Linux - Newbie 6 01-06-2006 03:08 AM
Quick script for running repetitive commands david_ross LinuxQuestions.org Member Success Stories 4 01-25-2004 10:12 AM


All times are GMT -5. The time now is 01:08 AM.

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