PDA

View Full Version : How to write server-side batch scripts


filburt1
October 30 '02, 05:41 PM
If you've used Windows you probably know about batch (.bat) files. They are text files that contain a series of commands executed by the command interpreter. For example this batch file will display the contents of autoexec.bat after first clearing the screen:

cls
type c:\autoexec.bat


Here's how you can write the equivalent of a batch file on Unix, which is what most hosts run. This script dumps the database "mydb" to the file "dump.sql":

#!
mysqldump --opt -uusername -ppassword mydb > dump.sql

Then instead of remembering the whole command you can just execute the script.

So do to everything:
1. SSH/Telnet into your host
2. Switch to your home directory (cd ~/) if you're not already there
3. Type pico scriptfilename
4. Type the following:

#!
command you want to run
next command you want to run
.
.
.

5. Press Ctrl-X and Enter twice to save the file and exit.
6. Type chmod +x scriptfilename

Now whenever you want to run the script, just switch to your home directory and type ./scriptfilename! You don't even have to switch to your home directory; you can also type ~/scriptfilename.

A simple tutorial that I bet a few people knew about before, but useful nonetheless.