Linux BASH Shell Scripting Basics of Creating and Utilizing BASH Scripts

Linux BASH Shell Scripting Basics of Creating and Utilizing BASH Scripts

The information presented here should act as a guide to creating quality scripts using the Linux built­in  BASH scripting language. BASH (Bourne Again Shell) is a scripting language as well as the default  command interpretor in most Linux distributions, including  Red Hat Linux. The ability to create  quality scripts is arguably the most time and error saving aspect of Linux Systems Administration. The  fact that the default shell or user environment in Linux is BASH  makes makes learning to create scripts both very valuable as well as simple. In the following document we will look at fundamentals of  scripts, best practices creating and testing of scripts. Shell scripts can be very simple or extremely  complex involving dozens of support files, but regardless of the complexity, they should be created in a  consistent, self­explanatory and easy to read format.

We won’t cover regular expressions, or shell utilities like loops since that information is readily  available on the Internet and in books. If after looking at an example, you do not understand an aspect  like “while read x in `ls`”, be sure to research how these functions work, or ask a more experienced  staff member for help. Mimicking a process without understanding is of little or no value, and  misunderstanding the process can lead to dangerous scripts and frustration.

What can I do with a script?

Any task you can run at the command line can be incorporated into a script. This is because working at  the command line and writing a script are essentially the same thing. Unix and Linux utilities and  commands tend to be very simple and singular in task. However, stringing a series of such utilities  together can create very flexible, powerful and extensible command line or script. There is no  environment that can be manipulated to perform with such flexibility and adaptability than the  command line and it’s related script files.

Any task that is complicated, repetitive or needs to be performed numerous times should be scripted. This saves time, eliminates the chances of typos, and often can be reused later.Sample Script Template: (this is not a physical template, but a general format you may want to adopt)#!/bin/bash## file: myscript.sh# created by Pete Nesbitt May 2006# this script will perform some fancy task.## set some variables to be used throughout the scriptTARGET=”remote.host.com”DATE_STAMP=”`date+%y%m%d`”EMAIL_RECIPIENTS=”admin@yourdomain.com”

A simpler and more efficient  method would be to create a loop that would rename each file  automatically. This is very similar to the way we created the files. This time though, we will do each  step separately. Note the prompt changes when BASH expects more information before it can complete  the command string. This is called the second level prompt (the primary prompt is a $ for most users  and a # for the root user).$ for x in `ls`> do> n= »`echo $x|cut ­d_ ­f2` »> mv $x new_file_$n> doneWe now have 10 files, named new_file_0 thru new_file_9

How to use INCLUDE FILES:You can store functions in one or more files outside your main script. In order to utilize these  functions, you must have an “include” statement, then you can just call them as you would any other  locally defined function. The include line does not actually have a syntax, it simply reads in additional  functions.Here we are adding the file install_lib, which contains a series of functions. . src/install_libs  (note the leading ‘dot space’ meaning ‘look in this file plus…’)

INTERIGATING FOR VARIABLES:Although the BASH shell is included in most Unix and Linux systems, some of the system commands  or utilities are located in different directories. If you want your script to be truly portable, you must  take this into account. Below is one of many ways to do it.

Then we search for them and save the output to an include file:(the shortfall here is the ‘which’  command must be available, a more complex method is used in the  Flac Jacket scripts) while read CMD_VARdo  CMD_NAME= »`echo $CMD_VAR|\` which tr\` [:lower:]  [:upper:]` »  echo “${CMD_NAME}=`which  ${CMD_VAR}`” >> command_include_filedone < command_list.txt

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *