LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-05-2013, 02:33 PM   #1
zoraze
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Rep: Reputation: Disabled
How I can script to run a command in terminal repeatedly?


Hi Everyone,

I am working on my project in linux ( Ubuntu 12.04 ) , I am using a commandline based simulator to simulate my scripts written in C++. i have to run one script 10 times just changing a one veriable value which i can change from terminal command as follows

./waf --run "scratch/Vanet_v1 --RngRun=1"

so i have to run the script till RngRun=10 . At the moment i have to run the command manually by using the arrow up key and change the value of RngRun to 2 and so on.

Is it possible i can automate this through a shell or even a C++ script ?

Somthing like whenever my one simulation is complete successfully just change the value of the RngRun argument in command and execute the next run.

Please help me in this, i will really be thankfull.

Regards,
Zoraze
 
Old 03-05-2013, 03:19 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,569

Rep: Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130
Like this?

Code:
for i in {1..10}; do ./waf --run "scratch/Vanet_v1 --RngRun=$i"; done
 
2 members found this post helpful.
Old 03-05-2013, 06:15 PM   #3
zoraze
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hello ,

is this should be in shell script ? or it can be typed in directlly in terminal ?

thank you so much for your reply.

Regards,
Zoraze
 
Old 03-05-2013, 06:33 PM   #4
zoraze
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks a lot i run that directly in terminal and i its working .

thank you so much. I have a request , i am very much interested to leran shell scripting can you please suggest me some good sources.

Thanks once again .

Regards,
Zoraze
 
Old 03-05-2013, 08:14 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,569

Rep: Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130Reputation: 2130
To answer your earlier question, it can be put in a script or typed on the command line, doesn't matter. That's all a script is really, a sequence of ordinary commands. In fact if you took care of the command line arguments to it, most scripts could be copy-pasted onto the command line and work just fine. That said, if you were to make a script out of what I posted, it would be best to split up the separate commands onto their own lines for readability:
Code:
for i in {1..10}; do
   ./waf --run "scratch/Vanet_v1 --RngRun=$i"
done
What this code does is the following:
"{1..10}" is a shell globbing pattern which is expanded to the string "1 2 3 4 5 6 7 8 9 10" when it's run

"for i in <something>" is a for loop that splits up <something> into pieces using the contents of the environment variable IFS as a delimiter. By default, IFS is an empty space, so "for i in <something>" splits up <something> into individual space-separated strings. In our case, the string "1 2 3 4 5 6 7 8 9 10" is divided up into 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10, so our for loop executes 10 times. Every iteration of the loop, the next phrase is placed into the variable "i" (since we said "for i in <something>", had we said "for stuff in <something>" then each phrase would be placed into the variable "stuff").

Inside our loop, we can access the contents of the variable i by sticking a dollar sign in front of it, like so: "$i", hence the ...RngRun=$i

As for reading material, I like this one:
http://tldp.org/LDP/abs/html/

Last edited by suicidaleggroll; 03-05-2013 at 08:21 PM.
 
Old 03-05-2013, 08:20 PM   #6
zoraze
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks alot i am surely going to look into that .

Regards,
Zoraze
 
Old 03-06-2013, 12:07 PM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 493

Rep: Reputation: 211Reputation: 211Reputation: 211
Quote:
Originally Posted by zoraze View Post
Thanks alot
Click.

Also, for a version of the script that does not require bash extensions you can use seq command:
Code:
for i in $(seq 10); do
    ./waf --run "scratch/Vanet_v1 --RngRun=$i"
done
 
Old 03-06-2013, 04:37 PM   #8
zoraze
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi Mina ,

ok i will this also .

Thanks alot.

Regards,
Zoraze
 
Old 03-06-2013, 04:48 PM   #9
zoraze
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
So my script should be like this .

#!/bin/bash
# Vanet
for i in {1..10}; do
./waf --run "scratch/Vanet_v1 --RngRun=$i"
done

And in Mina case

#!/bin/bash
# Vanet
for i in $(seq 10); do
./waf --run "scratch/Vanet_v1 --RngRun=$i"
done

And i will run it ./Vanet and for permissions i will do like chmod 755 Vanet. Is it right guys?

Regards,
Zoraze
 
Old 03-06-2013, 05:48 PM   #10
Nbiser
Member
 
Registered: Oct 2012
Location: Maryland
Distribution: Fedora, Slackware, Debian, Ubuntu, Knoppix, Helix,
Posts: 302
Blog Entries: 7

Rep: Reputation: 44
Here is a link for shell scripting http://http://www.freeos.com/guides/lsst/

Another link (my favorite) http://http://tldp.org/LDP/abs/html/

Hope this helps you learn bash shell scripting!
 
  


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] Writing a script that will open a terminal window and run another script jeffery1 Linux - General 5 02-16-2013 04:30 PM
[SOLVED] udev to run script not behaves like when run in terminal matiasar Linux - Software 6 04-17-2012 08:56 AM
[SOLVED] How do I run a command on a separate terminal in a bash script? Nathan.eth0 Programming 15 04-07-2011 12:59 PM
Is command line invocation of gnome-terminal to run more than one command possible? narnie Programming 4 02-17-2010 11:39 PM
Run a command on another terminal. mpyusko Slackware 3 06-23-2007 04:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:53 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration