Repeating things in the terminal.

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Loops are your friend. This following example will print Hello x where x is a number:
Code:
for x in `seq 1 10`
do
        echo Hello $x
done
The output of that is as follows:
Code:
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
Change the sequence parameters to 1 30 and that would happen 30 times. Once you change that, simply replace the echo line with the command you want to execute thirty times. So, for instance:
Code:
for x in `seq 1 30`
do
        <your command>
done
Replace <your command> with the command you want executed and you're good to go.

Save that as a shell script and simply execute it from the command line:
Code:
sh <path to script>
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
how bout...

Code:
#!/bin/bash
x=1
while [ $x -le 30 ]
do
  echo "Welcome $x times"
  x=$(( $x + 1 ))
done

There's also estabishing as in for i in 1 2 3 4 5 6 7 etc.. and COUNT, COUNTER.. ohhh so many ways ;)
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
As the Perl motto goes, "There's more than one way to do it."

For the OP: you can take my script (and Dysfunction's) and make it into a command instead of a script. Mine would look like so:
Code:
for x in `seq 1 30` ; do echo $x ; done
That's helpful if you don't want a script.
 
OP
K
Joined
Jul 13, 2011
Messages
7
Reaction score
1
Points
3
Would
Code:
echo 'How many times?'
read numtimes
for x in numtimes
do
touch file $filenum.txt
done
work?
 
OP
K
Joined
Jul 13, 2011
Messages
7
Reaction score
1
Points
3
ok, it does.(i tested it)
However, the file incrementing did not. How would i do this? I want to make as many files as possible.
 
OP
K
Joined
Jul 13, 2011
Messages
7
Reaction score
1
Points
3
Ahhh, got it. That was confusing.
Code:
#! /bin/bash

echo "Ready to crash your computer?"
read yes
echo "You entered yes. Why?"
mkdir ~/Desktop/Files
cd ~/Desktop/Files
echo 'How many times?'
read x
y=99
while [ $y -le $x ]
do
  touch $y.txt
  y=$(( $y + 1 ))
done
Don't put a high number in.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top