LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-16-2019, 11:18 PM   #1
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Rep: Reputation: 0
FOR loop


I want to create multiple folders using the for loop and I have been trying to figure it out now for a while and can't seem to get the right syntax. I want to create multiple folders in the folder i run the script in called BOINC_100 BOINC_101 BOINC_102 etc I have tried a lot of things and the only one I have gotten to run is as follows:

Code:
#!/bin/bash
for x in {100..1..101}  
do
mkdir "BOINC_$x"
done
But instead of creating multiple folders it just creates one folder called BOINC_{100..101..1}

If I try for $x in {100..1..101} I get this error Syntax error: Bad for loop variable

If I try for x in [100..1..101]

I get a folder called 'BOINC_[100..1..101]'

Cheers.

Last edited by gilesaj001; 12-16-2019 at 11:25 PM.
 
Old 12-16-2019, 11:52 PM   #2
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
OK I found the answer, I guess I jumped the gun doing the post but I could not find an answer. I ended up with

Quote:
#!/usr/bin/bash

n=100;
max=102;
while [ "$n" -le "$max" ]; do
mkdir "BOINC_$n"
n=`expr "$n" + 1`;
done
 
Old 12-17-2019, 12:43 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
{100..1..101} is not a Bash expression. Try this:
Code:
for f in {100..200}
do mkdir BOINC_$f
done
 
Old 12-17-2019, 12:58 AM   #4
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Convert Windows command to Linux

I am trying to convert a Windows commands so that it will run on Linux I have done the first two of three but the third is giving me problems. I will bold the Windows command I am trying to change to Linux.

The scripts only create two folders but I will use to create a lot more when working

This is the Windows Instructions:
Code:
1. Create a directly c:\multiboinc and inside that create a BOINC directory (C:\multiboinc\BOINC)
2. navigate to your original Boinc folder in C:\ProgramData\BOINC and copy the file: account_www.worldcommunitygrid.org to the BOINC folder created in step 1.
3.Open a command prompt and navigate to: c:\multiboinc
4. Run this string to create the 10 new boinc folders:
FOR /L %X IN (100,1,109) DO mkdir BOINC_%X\
5. Next run this string to copy the file from step 2 into the new boinc folders:
FOR /L %X IN (100,1,109) DO copy BOINC\*.* BOINC_%X\*.*
6.Use this string to start up your new instances from the command line:
FOR /L %X IN (100,1,109) DO start "BOINC_%X" /MIN "c:\Program Files\BOINC\boinc.exe" -allow_multiple_clients -dir c:\Multiboinc\BOINC_%X -gui_rpc_port 50%X
7. Use this string to gracefully stop them all (if you need to stop them): taskkill /im boinc.exe
The folder I created is /usr/bin/multiboinc and it contains a folder called BOINC2. BOINC2 has two files called account_nci.goofyxgridathome.net.xml account_wuprop.boinc-af.org.xml

I have converted #4 to
Code:
#!/usr/bin/bash

n=100;
max=102;
while [ "$n" -le "$max" ]; do
  mkdir "BOINC_$n"
  n=`expr "$n" + 1`;
done
I have converted #5 to

Code:
#!/usr/bin/bash

n=100;
max=102;
while [ "$n" -le "$max" ]; do
  cp BOINC2/* "BOINC_$n"
  n=`expr "$n" + 1`;
done

I cannot seem to get #6 to work as I get an error
chdir: No such file or directory
chdir: No such file or directory
chdir: No such file or directory

I am using the commands from https://boinc.berkeley.edu/wiki/Clie...d-line_options
Code:
#!/usr/bin/bash
n=100;
max=102;
while [ "$n" -le "$max" ]; do
  /usr/bin/boinc --allow_multiple_clients --dir "/usr/bin/multiboinc/BOINC_%n" --gui_rpc_port 50%n
  n=`expr "$n" + 1`;
done
Any help would be appreciated.

Last edited by gilesaj001; 12-17-2019 at 01:02 AM.
 
Old 12-17-2019, 01:10 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Code:
n=`expr "$n" + 1`
is found in old books.
The old Bourne shell did not have $(( )) arithmetics and needed the external expr command.
All Posix compatible shells take the much faster
Code:
n=$((n + 1))
And bash since v3 even takes
Code:
((n++))
The bash for loop using brace expansion is
Code:
for x in {100..102}
with an automatic default step 1 or -1 that you can set explicitly (since bash v4)
Code:
for x in {100..102..1}
The disadvantage with brace expansion is that the numbers are fixed: a $var is not evaluated.

Last edited by MadeInGermany; 12-17-2019 at 11:53 AM.
 
1 members found this post helpful.
Old 12-17-2019, 01:17 AM   #6
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Convert Windows command to Linux

I am trying to convert a Windows commands so that it will run on Linux I have done the first two of three but the third is giving me problems. I will bold the Windows command I am trying to change to Linux.

The scripts only create two folders but I will use to create a lot more when working

This is the Windows Instructions:
Code:
1. Create a directly c:\multiboinc and inside that create a BOINC directory (C:\multiboinc\BOINC)
2. navigate to your original Boinc folder in C:\ProgramData\BOINC and copy the file: account_www.worldcommunitygrid.org to the BOINC folder created in step 1.
3.Open a command prompt and navigate to: c:\multiboinc
4. Run this string to create the 10 new boinc folders:
FOR /L %X IN (100,1,109) DO mkdir BOINC_%X\
5. Next run this string to copy the file from step 2 into the new boinc folders:
FOR /L %X IN (100,1,109) DO copy BOINC\*.* BOINC_%X\*.*
6.Use this string to start up your new instances from the command line:
FOR /L %X IN (100,1,109) DO start "BOINC_%X" /MIN "c:\Program Files\BOINC\boinc.exe" -allow_multiple_clients -dir c:\Multiboinc\BOINC_%X -gui_rpc_port 50%X
7. Use this string to gracefully stop them all (if you need to stop them): taskkill /im boinc.exe
The folder I created is /usr/bin/multiboinc and it contains a folder called BOINC2. BOINC2 has two files called account_nci.goofyxgridathome.net.xml account_wuprop.boinc-af.org.xml

I have converted #4 to
Code:
#!/usr/bin/bash

n=100;
max=102;
while [ "$n" -le "$max" ]; do
  mkdir "BOINC_$n"
  n=`expr "$n" + 1`;
done
I have converted #5 to

Code:
#!/usr/bin/bash

n=100;
max=102;
while [ "$n" -le "$max" ]; do
  cp BOINC2/* "BOINC_$n"
  n=`expr "$n" + 1`;
done

I cannot seem to get #6 to work as I get an error
chdir: No such file or directory
chdir: No such file or directory
chdir: No such file or directory

I am using the commands from https://boinc.berkeley.edu/wiki/Clie...d-line_options
Code:
#!/usr/bin/bash
n=100;
max=102;
while [ "$n" -le "$max" ]; do
  /usr/bin/boinc --allow_multiple_clients --dir "/usr/bin/multiboinc/BOINC_%n" --gui_rpc_port 50%n
  n=`expr "$n" + 1`;
done
Any help would be appreciated.


EDIT: I changed the % in #6 to a $ and it ran but only started one instance 100

/usr/bin/boinc --allow_multiple_clients --dir "/usr/bin/multiboinc/BOINC_$n" --gui_rpc_port 50$n
 
Old 12-17-2019, 01:50 AM   #7
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
I changed the he command to n=$((n + 1)) but still only creates the one instance
 
Old 12-17-2019, 02:41 AM   #8
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Can you share the current version of #6?

I would simplify it somewhat, with a for loop instead of a while, like this:
Code:
#!/usr/bin/bash
for n in {100..102}
do
  /usr/bin/boinc --allow_multiple_clients --dir "/usr/bin/multiboinc/BOINC_$n" --gui_rpc_port 50$n
done
Are you still getting the error message "no such directory"? If so, my guess is that the /usr/bin/multiboinc/BOINC... directories don't exist.
 
1 members found this post helpful.
Old 12-17-2019, 02:52 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I would do something like:
Code:
#!/usr/bin/env bash

base=/usr/bin
mb_dir="$base/multiboinc/BOINC_"

mkdir -p "$mb_dir"{100..102}

for dir in "$mb_dir"*
do
  /usr/bin/boinc --allow_multiple_clients --dir "$dir" --gui_rpc_port 50"${dir##*_}"
done
If you place set -vx at the top of the script it might show you why only one instance is being fired. My guess would be that you need to start each one in the background so perhaps append '&' after command

Last edited by grail; 12-17-2019 at 02:54 AM. Reason: Didn't realise n at end was a variable
 
1 members found this post helpful.
Old 12-17-2019, 03:33 AM   #10
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
I would do something like:
Code:
#!/usr/bin/env bash

base=/usr/bin
mb_dir="$base/multiboinc/BOINC_"

mkdir -p "$mb_dir"{100..102}

for dir in "$mb_dir"*
do
  /usr/bin/boinc --allow_multiple_clients --dir "$dir" --gui_rpc_port 50"${dir##*_}"
done
If you place set -vx at the top of the script it might show you why only one instance is being fired. My guess would be that you need to start each one in the background so perhaps append '&' after command

Thanks for the reply.

The mkdir only creates a directory called BOINC_{100..102} and I don't want to combine the make directory and the start because I need to start different instances so I need to be able to start X number of instances that are already created.

I added the & at the end and your script after I took out the mkdir line and it processed but the port part did not work properly. Because I had 250 directories it tried to start them all and there were just too many errors so I need to have the option to just start a couple till it works.

Some of the errors were

Code:
17-Dec-2019 20:27:04 [---] GUI RPC bind to port 50 failed: 98
17-Dec-2019 20:27:04 [---] GUI RPC bind to port 50 failed: 98
17-Dec-2019 20:27:04 [---] GUI RPC bind to port 50 failed: 98
17-Dec-2019 20:27:04 [---] GUI RPC bind to port 50 failed: 98
17-Dec-2019 20:27:04 [---] GUI RPC bind to port 50 failed: 98
17-Dec-2019 20:27:04 [---] GUI RPC bind to port 50 failed: 98
17-Dec-2019 20:27:04 [---] GUI RPC bind to port 50 failed: 98
17-Dec-2019 20:27:04 [---] GUI RPC bind to port 50 failed: 98
 
Old 12-17-2019, 03:43 AM   #11
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by berndbausch View Post
Can you share the current version of #6?

I would simplify it somewhat, with a for loop instead of a while, like this:
Code:
#!/usr/bin/bash
for n in {100..102}
do
  /usr/bin/boinc --allow_multiple_clients --dir "/usr/bin/multiboinc/BOINC_$n" --gui_rpc_port 50$n
done
Are you still getting the error message "no such directory"? If so, my guess is that the /usr/bin/multiboinc/BOINC... directories don't exist.
The original Windows was
FOR /L %X IN (100,1,109) DO start "BOINC_%X" /MIN "c:\Program Files\BOINC\boinc.exe" -allow_multiple_clients -dir c:\Multiboinc\BOINC_%X -gui_rpc_port 50%X

What I tried first was
#!/usr/bin/bash
n=100;
max=102;
while [ "$n" -le "$max" ]; do
/usr/bin/boinc --allow_multiple_clients --dir "/usr/bin/multiboinc/BOINC_%n" --gui_rpc_port 50%n
n=`expr "$n" + 1`;
done

When this is run I get this error:

sh start_some.sh
chdir: No such file or directory
chdir: No such file or directory
chdir: No such file or directory


If I change the %n to $n then this runs but only opens one instance.
 
Old 12-17-2019, 03:48 AM   #12
gilesaj001
Member
 
Registered: Apr 2017
Location: Australia
Distribution: Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 0
Fixed

I went back to the original script and added a & to the end of the command line and it runs fine now, I think. It started the three instances that it was supposed to.

I ended up with:

Code:
#!/usr/bin/bash
n=100;
max=102;
while [ "$n" -le "$max" ]; do
  /usr/bin/boinc --allow_multiple_clients --dir "/usr/bin/multiboinc/BOINC_$n" --gui_rpc_port 50$n &
  n=`expr "$n" + 1`;
done

Very close to what I started with. I will close this thread and check it again tomorrow. I have been on this computer for hours trying to get this to work.

Thanks for all your help.
 
Old 12-17-2019, 04:12 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
The mkdir only creates a directory called BOINC_{100..102}
Unless you changed what was in the script, as I tested each line, the output will be:
Code:
$ mkdir -pv BOINC_{100..102}
mkdir: created directory 'BOINC_100'
mkdir: created directory 'BOINC_101'
mkdir: created directory 'BOINC_102'
Added -v so you can see what happened. If you placed the curly brackets in quotes though then you would end up with what you showed.
 
Old 12-17-2019, 06:43 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'm surprised no-one suggested 'seq'
Code:
for num in in $( seq 1 3 )
do
	mkdir BOINC_${num}
done
 
Old 12-19-2019, 11:40 AM   #15
Skillz
Member
 
Registered: Sep 2007
Posts: 252

Rep: Reputation: 32
Try adding --daemon to the boinc start command to start each instance. This will tell Linux to start the program in the background and may allow you to run multiple.
 
  


Reply

Tags
for loop, loop



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
while loop or for loop? mijohnst Programming 18 11-21-2005 04:48 PM

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

All times are GMT -5. The time now is 09:00 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