How to Write a Script in Linux
When you're working in a Linux environment, you often need a script to complete a task for you, interact with a user or simply complete a process for you on a regular basis with crontab. Once you know the series of commands that you need to process, you can enter them in a file and create a script from them.
Instructions
-
-
1
Open up a shell. This can be a bash shell, bourne shell, or any other shell that you prefer. Be sure that the commands for your script will work in that shell.
-
2
Start up a text editor. UI is included with nearly all Linux distributions, but if you want to use another editor, it should work as long as it can save the file in plain text.
-
-
3
Write your script commands. Inside the editor, put one of your script commands on each line. You can put more than one command on a line, but make sure that all your lines are valid in your shell, or it will generate errors.
-
4
Save your file and exit the editor. Since it is Linux, it doesn't really matter what you name your file, but using descriptive file names makes things easier later.
-
5
Change the permissions on the file to give the file execute access. A usual file will not have permissions for execution, so you will need to give it those permissions. One way of doing this is with "chmod u+x name_of_file."
-
6
Execute the file by typing the file name or, depending on your environment, you may need to use /bin/bash name_of_file. Once your script file works, you can add it to crontab and have it run whenever necessary.
-
1
Tips & Warnings
Be sure to test your script throughly if you plan to put it in a crontab.