LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   best way to automate shell commands - shell script or python scripts or something els (https://www.linuxquestions.org/questions/linux-newbie-8/best-way-to-automate-shell-commands-shell-script-or-python-scripts-or-something-els-4175421061/)

parangsaraf 08-08-2012 02:13 PM

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

Kustom42 08-08-2012 02:38 PM

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.

parangsaraf 08-08-2012 02:45 PM

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

kenneth_phough 08-08-2012 02:56 PM

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

parangsaraf 08-08-2012 03:01 PM

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.

kenneth_phough 08-08-2012 03:04 PM

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.

Kustom42 08-08-2012 03:10 PM

Quote:

Originally Posted by kenneth_phough (Post 4749242)
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


parangsaraf 08-08-2012 03:12 PM

Its a simple file like this:

-fdse
-fdce
.
.
.


Now how to put these options in a list to iterate over?

Kustom42 08-08-2012 03:12 PM

Quote:

Originally Posted by parangsaraf (Post 4749224)
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 (Post 4749245)
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.

parangsaraf 08-08-2012 03:16 PM

Thanks Kenneth and Kustom42.

@Kustom42: Sure. Will try to learn BASH first followed by python. :)

Kustom42 08-08-2012 03:25 PM

Quote:

Originally Posted by parangsaraf (Post 4749252)
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.

chrism01 08-08-2012 06:17 PM

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/


All times are GMT -5. The time now is 06:18 PM.