LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-01-2012, 12:38 PM   #1
philipss
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Rep: Reputation: Disabled
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
 
Old 09-01-2012, 12:48 PM   #2
joet1984
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Rep: Reputation: Disabled
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

Last edited by joet1984; 09-01-2012 at 12:53 PM. Reason: correction
 
Old 09-01-2012, 12:51 PM   #3
philipss
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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?
 
Old 09-01-2012, 02:08 PM   #4
joet1984
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Rep: Reputation: Disabled
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?
 
Old 09-01-2012, 04:59 PM   #5
speck
Member
 
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 375

Rep: Reputation: 115Reputation: 115
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 "&&".
 
Old 09-01-2012, 06:14 PM   #6
philipss
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-02-2012, 12:03 AM   #7
speck
Member
 
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 375

Rep: Reputation: 115Reputation: 115
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".
 
Old 09-02-2012, 07:05 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 09-02-2012, 07:33 AM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Member Response

Hi,

Welcome to LQ!
Quote:
Originally Posted by philipss View Post
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.


 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line question ghostrider.002 Red Hat 1 05-19-2007 01:18 AM
command line question dstrbd1 Linux - Newbie 3 01-31-2006 10:41 AM
Command line question satimis Linux - General 14 06-20-2005 09:46 PM
command line question phonecian Linux - Software 2 10-09-2004 08:36 AM
command line question... warheros Linux - Newbie 12 06-25-2003 04:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:20 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
Open Source Consulting | Domain Registration