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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-08-2012, 02:13 PM
|
#1
|
LQ Newbie
Registered: Aug 2012
Posts: 5
Rep:
|
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
|
|
|
08-08-2012, 02:38 PM
|
#2
|
Senior Member
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604
|
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.
|
|
|
08-08-2012, 02:45 PM
|
#3
|
LQ Newbie
Registered: Aug 2012
Posts: 5
Original Poster
Rep:
|
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
|
|
|
08-08-2012, 02:56 PM
|
#4
|
Member
Registered: Dec 2005
Location: Birmingham, AL
Distribution: CentOS, REHL, Vine Linux
Posts: 67
Rep:
|
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.
|
|
|
08-08-2012, 03:01 PM
|
#5
|
LQ Newbie
Registered: Aug 2012
Posts: 5
Original Poster
Rep:
|
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.
|
|
|
08-08-2012, 03:04 PM
|
#6
|
Member
Registered: Dec 2005
Location: Birmingham, AL
Distribution: CentOS, REHL, Vine Linux
Posts: 67
Rep:
|
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.
|
08-08-2012, 03:10 PM
|
#7
|
Senior Member
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604
|
Quote:
Originally Posted by kenneth_phough
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
|
|
|
08-08-2012, 03:12 PM
|
#8
|
LQ Newbie
Registered: Aug 2012
Posts: 5
Original Poster
Rep:
|
Its a simple file like this:
-fdse
-fdce
.
.
.
Now how to put these options in a list to iterate over?
|
|
|
08-08-2012, 03:12 PM
|
#9
|
Senior Member
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604
|
Quote:
Originally Posted by parangsaraf
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
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.
|
08-08-2012, 03:16 PM
|
#10
|
LQ Newbie
Registered: Aug 2012
Posts: 5
Original Poster
Rep:
|
Thanks Kenneth and Kustom42.
@Kustom42: Sure. Will try to learn BASH first followed by python.
|
|
|
08-08-2012, 03:25 PM
|
#11
|
Senior Member
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604
|
Quote:
Originally Posted by parangsaraf
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.
|
08-08-2012, 06:17 PM
|
#12
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,398
|
|
|
|
All times are GMT -5. The time now is 01:58 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|