LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Command line automization question (https://www.linuxquestions.org/questions/linux-general-1/command-line-automization-question-4175425147/)

philipss 09-01-2012 12:38 PM

Command line automization question
 
Hello

I m a complete newbie on linux (know a little r).

I am using imagemagick and need to excecute a simple command in different directories. Because the command takes some time I would have to enter it... then wait then , once its done (beacuse parallel would crash the system), change the folder , enter commandline again, etc you get the picture.

I m looking for a way that I could do that automatically.

so the process is

cd a
montage something make asomething
cd ..
cd b
montage something make bsomething
etc

How could i enter that(commandline folderchange; command line folder change) in the terminal so i could get it to just run all night?


Thank you very much. very much
philip

joet1984 09-01-2012 12:48 PM

You can write a bash script.

For example, add this to a file and save


#!/bin/bash
cd a
montage something make asomething
cd ..
cd b
montage something make bsomething
etc


Then
$ chmod +x myfilename <- Makes the file executable
$ ./myfilename <-runs the scripts

philipss 09-01-2012 12:51 PM

yes i figured this much.

But how would i do that?

Cant i just type the commands: so path/ commandline
next path commandline
next path commandline
?

how would such a script have to look?

joet1984 09-01-2012 02:08 PM

A bash script is just a regular terminal. The commands would be the same as you would type in the terminal

just add #!/bin/bash to the top of your script and type exactly what you would type if you were entering the commands in yourself

If you were doing it manually with the commands:
cd a
montage something make asomething
cd ..
cd b
montage something make bsomething
etc

put that right in the bash script. You can cd in a bash script, you can do anything you could normally do at a terminal. There is no extra syntax. Is this what you're asking? Or am I understanding wrong?

speck 09-01-2012 04:59 PM

You can also just put it all in one line on the command line:

Code:

cd a && montage something && make asomething && cd .. && cd b && montage something && make bsomething
The "&&" between each command means that the next command will execute only if the previous command was successful. If you don't care if the previous command was successful and just want the next command to execute no matter what, use ";" instead of "&&".

philipss 09-01-2012 06:14 PM

thanks
it seems to be runnign like a charme.
i wonder though because the script hasnt gotten there. is it with or without spacing the entire thing?

Well I ll see tomorrow morning. But this helps me trmendously!!!!

Thank you
p

speck 09-02-2012 12:03 AM

A script of what I typed would look like the following, just one command on each line.

Code:

#!/bin/sh

cd a
montage something
make asomething
cd ../b
montage something
make bsomething

This script would be similar to you just putting a ";" between commands instead of "&&". You can (should) also combine the "cd .. && cd b" to just "cd ../b".

pixellany 09-02-2012 07:05 AM

If you create a list of directories with files to be processed, then you could loop thru that list.


Code:

((dirlist is a file containing all the directory name))
while read dirname; do
    cd $dirname
    <<do stuff with imagemagick>>  (Insert valid code here)
done < dirlist


onebuck 09-02-2012 07:33 AM

Member Response
 
Hi,

Welcome to LQ!
Quote:

Originally Posted by philipss (Post 4770051)
yes i figured this much.

But how would i do that?

Cant i just type the commands: so path/ commandline
next path commandline
next path commandline
?

how would such a script have to look?

You can use 'RUTE' as a good overall tutorial. For 'bash', I would start at 4,5 & 6 below while the other links will enhance your experience.

Just a few links to aid you to gaining some understanding.



1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Utimate Linux Newbie Guide

The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!


FYI: I suggest that you look at 'How to Ask Questions the Smart Way' so in the future your queries provide information that will aid us in diagnosis of the problem or query.




All times are GMT -5. The time now is 12:41 PM.