Sie sind auf Seite 1von 16

Linux Scripts What is a Script Simply put a script is a file containing meaningful text (commands) which is interpreted by the

shell or command line interpreter of the operating system. Scripts can be created using any text editor be it vi, emacs or kedit. Basic scripts are relatively simple to write, as the user would write out the commands on the script as he would on the shell prompt Scripts are essential to System Admins because they save time and effort (having to type several commands), consistency and reliability and help automate tasks without user intervention for example: -A script that deletes the browsing/file history when the user logs off -A script that performs daily/weekly backups -A script that monitors resources and emails results Question, Can Anyone Think Of Another Useful Script? As an system admin it is of great importance to understand and interpret shell scripts because most of the Linux system is run by shell scripts. For example the files stored in the location /etc/init.d/ are all scripts that run at start up to restore the systems settings. This can be desktop preferences or network settings etc. An admin can create new commands tailor made for his needs. For the purposes of this presentation we will be looking at scripts designed for the BASH shell as this is usually the default shell in most Linux distributions.

Note: BASH shell is an integer arithmetic shell which means if we create a script with arithmetic operations where the resulting output is not an integer the script will return the wrong answer. For float-point arithmetic operations we can use the z shell (zsh)
But What Does A Script Actually Look Like? As mentioned previously a script is text file but it can be divided into different sections -Header -Body -Exit (not essential) Now we will look at a sample script written by Vivek G. from http://www.nixcraft.com/ Header The first line of any script describes which shell the script will be used. #!/bin/bash Although the # is usually used to denote a comment the ! that follows tells the shell which shell to run the script under this means the user could be working in the bash shell and summon a script to run under the ash shell.

Note: It is not necessary to specify which shell the script should run under, if no specification is made the script uses the same shell from which it was invoked. Question, Can Anyone Think of another Shell?
Body The body contains the commands that the shell will interpret and execute sequentially except for loops and other functions. Lines after the # are comments that are ignored by the shell and are included in the script by the author to help the user understand the commands that the script is performing. Comments can be written in at any point during the script and span until the end of that line. If a user starts a new comment and goes onto a new line without adding a # the interpreter will regard the text as a command and will often return an error. Any command that can be used on the shell prompt can be written onto the body of the script. Exit When the shell reaches the end of the script it returns to the shell prompt but will also return an Exit Status of the last command that was completed by the shell. In this case it will be the last command of the script The Exit Status of a command that completes correctly is usually 0. In order to verify that our command has completed correctly we can use the $? Command alongside the echo command to display the status code of the last command In advance scripting we use nested if then statements to evaluate the output of the exit status code of a command and what to do next. Building a Script One of the main tasks that system admins are faced with is backing up! Backing up is essential to any company and can be achieved by many means. Within the Linux environment there are many programs that make it very simple to backup and archive directories. For the purpose of this presentation I will use the sample script provided by the book Command Line & Scripting Bible,Blum Richard,

In the book, author Richard Blum proposes a simple script that backs up the contents of a directory using the tar command. #!/bin/bash # archive a working directory tar -cf backup.tar /home/Richard/Data 2 /dev/null echo backup completed exit 0

This script as it is works perfectly fine but it can be adjusted to add more functionality.

Note: This script will

work under several assumptions There are 3 important predefined directories that will be backed up These directories are kept in /home/riichall/testscript The directories are Financial Data, System Settings and Pictures

My first modification to the script is to allow the user to select which directory to archive. I also added the gzip to compress the resulting file. If we take a close look at the modified code we can see that with added complexity more lines are added to the code.

Note: When the first option is selected I used quotation marks ( ) so that the tar command would recognize the /home/riichall/testscript/Financial Data as a whole address In the second instance however the directory System Settings also has a space but this time instead of using the quotation sings ( ) I used the escape character \ Adding More Features
After testing the initial script I decided to add more functionality, the first was an option to exit the script. This was relatively simple I just added a new line to the nested else if statements. The second addition was the ability to edit the crontab of the user so that the script could be run every day at midnight and would backup all three directories. I did this by first creating a temporary crontab, reading the crontab and outping the lines onto the temporary file and later appending the temp file with the tar command for all three directories. From here I replaced the original crontab with the temp one without losing any jobs that might have already been there.

From the screenshot we can see that this option needs further troubleshooting but upon closer inspection of the tab we can see that the fault lies in crontab permissions and not in the script.

Slides

Das könnte Ihnen auch gefallen