New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Operating System General OS operation information and support

Repeating things in the terminal.


Post Reply New Thread Subscribe

 
Thread Tools
Kiarocks

 
Member Since: Jul 13, 2011
Posts: 7
Kiarocks will become famous soon enough

Kiarocks is offline
Is there a way to repeat something 30 times in terminal?
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,097
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
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>

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
Dysfunction

 
Dysfunction's Avatar
 
Member Since: Mar 17, 2008
Location: Tucson, AZ
Posts: 6,638
Dysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant futureDysfunction has a brilliant future
Mac Specs: 2008 and 2011 15" mbps, late 11 iMac, iPhone 4s, and too many ipods and other stuff

Dysfunction is offline
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

mike
This machine kills fascists
Got # ? phear the command line!
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,097
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
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.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
Kiarocks

 
Member Since: Jul 13, 2011
Posts: 7
Kiarocks will become famous soon enough

Kiarocks is offline
Would
Code:
echo 'How many times?'
read numtimes
for x in numtimes
do
touch file $filenum.txt
done
work?
QUOTE Thanks
Kiarocks

 
Member Since: Jul 13, 2011
Posts: 7
Kiarocks will become famous soon enough

Kiarocks is offline
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.
QUOTE Thanks
Kiarocks

 
Member Since: Jul 13, 2011
Posts: 7
Kiarocks will become famous soon enough

Kiarocks is offline
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.
QUOTE Thanks

Post Reply New Thread Subscribe


« iMac super slow bootup | Trying to install Leopord over Tiger, get stuck on white loading screen »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Help Fixing Terminal entry I made in Vietnam to access Facebook :( radoja OS X - Operating System 8 10-19-2013 03:20 AM
Automatically save log files of terminal sessions Stubaan OS X - Apps and Games 3 04-02-2013 11:24 AM
Open a url in mac with blocking terminal tarun91 OS X - Operating System 4 05-18-2011 10:38 AM
MacBook won't turn on 98% of the time, help! Very weird things happening! shanenegen Apple Notebooks 0 06-17-2010 02:27 PM
switchers - top 5 things you don't miss! eric Switcher Hangout 103 02-16-2010 12:04 AM

All times are GMT -4. The time now is 06:08 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?