LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-08-2012, 02:13 PM   #1
parangsaraf
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Rep: Reputation: Disabled
best way to automate shell commands - shell script or python scripts or something els


Hi,

I want to automate a few shell commands. I want to compile my program for different options and run it. These options are stored in a file.

So, I am supposed to read all the options from the file.
Compile a particular program with one option at a time.
Run that program.
Then compile again with another option from the file.
Do this till all the options in the file are exhausted.

I am not sure, what is the best way to automate this. Should I write shell scripts or should I try python script or something else.

Thanks
Parang
 
Old 08-08-2012, 02:38 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Like being a good mechanic, being a good system admin requires using the right tool for the job.

This is something that can be done with BASH and in all honesty it's hard to give a good recommendation without knowing how in depth the program being run is, how many options and what they are, etc..


Personally, this sounds like something I would use python for as it's by design an interpretive language that is usually really good at handling input like this easily. But if it's a small set of options and a quick task something a simply as a one-line for loop could get the job done.

If you can give some more detail we can probably give you better answers, I hope some of this info helps.
 
Old 08-08-2012, 02:45 PM   #3
parangsaraf
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks Kustom42.

Here is what I want to do:

gcc uses several compile time options like -fdce or -fdse etc. There are 60 such optimization flags documented. I have copy pasted all these documented flags in a file. Now I want to use one flag at a time to compile a program. Then run that program 5 times. After that take another flag from the file and repeat this process until all flags are exhausted.

Now, someone suggested me to use python for this and someone said shell script. I am confused. I dont know both shell scripting and python. I want to learn the one that is best suited for this type of automation.

Thanks
 
Old 08-08-2012, 02:56 PM   #4
kenneth_phough
Member
 
Registered: Dec 2005
Location: Birmingham, AL
Distribution: CentOS, REHL, Vine Linux
Posts: 67

Rep: Reputation: 3
If I were you I would use bash.

Read in one line each of the flag files => compile => exec 5 times and repeat.
Example:

while read flag; do

gcc $flag somefile.c

for i in 1 2 3 4 5
do
./a.out
done

done < gccflags.txt

Last edited by kenneth_phough; 08-08-2012 at 02:57 PM.
 
Old 08-08-2012, 03:01 PM   #5
parangsaraf
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
My only concern is how to read flag options from the file and put it in a list or something. And then iterate through that list.
 
Old 08-08-2012, 03:04 PM   #6
kenneth_phough
Member
 
Registered: Dec 2005
Location: Birmingham, AL
Distribution: CentOS, REHL, Vine Linux
Posts: 67

Rep: Reputation: 3
I see, I missed that part.

What does that file look like? It all depends, for example if it is like so:

someflag - description of someflag
someotherflag - description of someother flag

then you can us:

cut -f1 -d '-'

to delimit by '-' and get the first field which is the flag name.
 
1 members found this post helpful.
Old 08-08-2012, 03:10 PM   #7
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by kenneth_phough View Post
I see, I missed that part.

What does that file look like? It all depends, for example if it is like so:

someflag - description of someflag
someotherflag - description of someother flag

then you can us:

cut -f1 -d '-'

to delimit by '-' and get the first field which is the flag name.
You don't even need to do that, if it's one option per line just a simple for loop with a cat statement.
Code:
for line in $(cat ./compileoptions.txt)
do
gcc $line somefile.c
for i in 1 2 3 4 5
do
./a.out
sleep 5
done
done
 
Old 08-08-2012, 03:12 PM   #8
parangsaraf
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Its a simple file like this:

-fdse
-fdce
.
.
.


Now how to put these options in a list to iterate over?
 
Old 08-08-2012, 03:12 PM   #9
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by parangsaraf View Post
Thanks Kustom42.

Now, someone suggested me to use python for this and someone said shell script. I am confused. I dont know both shell scripting and python. I want to learn the one that is best suited for this type of automation.

Build your fundamentals, learn BASH. Then learn another language. Scripting languages are kind of like different types of vehicles, each one gets you from a to b but they all do it slightly different and everybody has their favorite.


If you want to learn python read this, its a great tutorial: learnpythonthehardway.org/

---------- Post added 08-08-12 at 01:13 PM ----------

Quote:
Originally Posted by parangsaraf View Post
Its a simple file like this:

-fdse
-fdce
.
.
.


Now how to put these options in a list to iterate over?
Check my previous post, for loop with a cat statement.
 
1 members found this post helpful.
Old 08-08-2012, 03:16 PM   #10
parangsaraf
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks Kenneth and Kustom42.

@Kustom42: Sure. Will try to learn BASH first followed by python.
 
Old 08-08-2012, 03:25 PM   #11
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by parangsaraf View Post
Thanks Kenneth and Kustom42.

@Kustom42: Sure. Will try to learn BASH first followed by python.
Not sure what your current level of knowledge is, but I really love this book for newbies to beginners looking to learn bash: http://sourceforge.net/projects/linu...2.pdf/download

Even if you already know some of the basics this book will fill in alot of the holes and get you into some really cool bash scripting exercises toward the end.
 
1 members found this post helpful.
Old 08-08-2012, 06:17 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398

Rep: Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780Reputation: 2780
Good bash tutorials
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


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
Shell Script Commands to Automate Server or Host Telnet Login Session jakesjohn Linux - Software 4 02-08-2010 11:32 PM
Shell Script Commands to Automate Server or Host Telnet Login Session jakesjohn Linux - General 2 02-08-2010 11:28 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Shell Scripts and Networking to Automate macchanger fultron Linux - Newbie 1 02-16-2007 08:02 PM

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

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