Sie sind auf Seite 1von 7

SysAd313: Shell Scripting for System Administrators The Basic

Objectives:
Bring confidence to students in using the command line. Understand what a shell is and what they are capable of. Learn how important shell scripting is for system administrators. Learn how to write shell scripts. Familiarize with the different Bash commands.

Concepts
Introduction
System administrator can make use of shell scripting to improve and efficiently complete their task. In this activity, we will demonstrate how we can minimize work for commands that we repeatedly execute. Thus shell scripting is a vital skill that every system administrator should have as well as familiarization with the command line utilities will be quite handy. For beginners, the familiarization in the CLI environment will be advantageous; knowing what is happening under the hood will make every student capable of performing whether a simple or a complicated task. For now, students should discover that shell scripting is a simple as saving the commands in a file. Giving the file an executable permission and then finally executing the file. A competent system administrator can build a variety of customized shell scripts that will help him automate task that he would normally perform on routinely basis.

What is a shell?
Shell A program that takes input from the user , translate it into instructions that the operating system can understand, and conveys the operating systems output back to the user Is a file containing one or more commands that you would type on the command line.

Shell script

Are there other types of shell? bash - Bourne-again shell (GNU) csh C shell (BSD) jsh Job control shell (SVR4)

ksh Korn shell (Bell Labs) rsh Remote shell (TCP/IP) sh Bourne shell tcsh Popular extension of theC shell zsh Popular extension of the Korn shell

The Basics of Shell Scripting


The bash shell has the following features: Variables o User variables o Command line arguments variables o Environment variables Shells can evaluate expressions Control structures (i.e. for, while etc) and conditions (if, if-else, etc) Declare functions (just like in programming)

The Bash shell command set


Take note that the table below is a listing of Bash commands. (Linux commands have a different set of commands). COMMAND Description alias Create an alias break Exit from loop case Evaluate one of several scripts cd Change directory command Run a command ignoring shell functions continue Resume next iteration of a loop declare Declare variables and give them attributes dirs Display a list of remembered directories enable Enable and disable built-in shell commands eval Evaluate several commands/arguments exec Execute a command exit Exit the shell export Set an environment variable for Expand words and execute commands hash Remember the full path name of a name argument history Command History if Conditionally perform a command let Perform arithmetic on shell variables local Create variables logout Exit a login shell popd Restore the previous value of the current directory

pushd read readonly return set shift shopt source times type ulimit COMMAND umask unalias unset until while

Save and then change the current directory Read a line from standard input Mark variables/functions as read only Exit a shell function Manipulate shell variables and functions Shift positional parameters Shell options Run commands from a file `.' User and system times Describe a command Limit user resources Description Users file creation mask Remove an alias Remove variables and function names Execute commands (until error) Execute commands

Creating A Shell Script


1. When creating a shell script file, you must specify the shell you are using in the first line of the file. (Not required but highly recommended).

#!/bin/bash
The pound sign is used as a comment line, but the first line of the shell script is a special case and the pound sign ( # ) followed by the exclamation point ( ! ) tells the shell what shell to run the script As mentioned, comments can be added by using the pound sign ( # ) . An example looks like this:

# This line is a comment.


2. Save the file (usually with a .sh extension, without any will do). 3. Give your script an executable permission using the command:

$ chmod u+x myfirstscript.sh


4. Finally, run your script.

$ ./myfirstscript.sh

Examples #!/bin/bash #Title: myfirstcript.sh #Date: June 25, 2011 #Description: Example script the uses echo to print a string #Options: none #Creator: SysAdmin date echo Hello Students! echo Welcome to System Administration (SysAd313)

#!/bin/bash #Title: myfirstcript.sh #Date: June 25, 2011 #Description: Example script showing the use of tilde ( ~ ) #Options: none #Creator: SysAdmin date cd ~ echo My current directory is: pwd

#!/bin/bash #Title: myfirstcript.sh #Date: June 25, 2011 #Description: Example script showing the use of tilde ( ~ ) #Options: none #Creator: SysAdmin echo The date today is: date echo Welcome! You are logged in as who echo Lets try to create a directory named scripts under your home directory mkdir ~/scripts ls ~

Variables
Variables can be handy whenever you want to process other information in your shell script. Variables will allow you to temporarily store information in your shell script.

User Variables Shell scripts allow you to set and use your own variables within your script making it feels like a computer program. Rules to follow in setting user variables: Can be any text string of up to 20 letters, digits or an underscore character. Case sensitive Values are assigned using an equal sign ( = ) No spaces can appear between the variable, the equal sign and the value Variables are referenced using the dollar sign The shell script can determine the data type used for the value of the variable.

Environment Variables The bash shell uses the environment variables to store information about the shell session and the working environment. This capability allows you to store data in memory that can be accessed not just with your script but by any program as well. Persistent data such as user account, system, shell are stored in environment variables. There are two types of environment variables in the bash shell: Global variables Local variables

Global Variables Global variables are visible from the shell session and any child processes that the shell spawns. Global variables are useful in applications that spawn child processes that require information from the parent process. The Linux system sets several environment variables when you start your bash session. The system environment variables always use all capital letters to differentiate them from normal user environment variables. To view the global environment variables, use the printenv command:

$ printenv
HOSTNAME=testbox.localdomain TERM=xterm SHELL=/bin/bash HISTSIZE=1000

SSH CLIENT=192.168.1.2 1358 22 OLDPWD=/home/rich/test/test1 SSH TTY=/dev/pts/0 USER=rich LS COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35: bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32: *.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32: *.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31: *.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31: *.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31: *.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35: *.xpm=00;35:*.png=00;35:*.tif=00;35: MAIL=/var/spool/mail/rich PATH=/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/bin:/usr/bin: /home/rich/bin INPUTRC=/etc/inputrc PWD=/home/rich LANG=en US.UTF-8 SSH ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass SHLVL=1 HOME=/home/rich LOGNAME=rich CVS RSH=ssh SSH CONNECTION=192.168.1.2 1358 192.168.1.4 22 LESSOPEN=|/usr/bin/lesspipe.sh %s G BROKEN FILENAMES=1 =/usr/bin/printenv

Local Environment Variables As the name implies, these are variables that can be seen only in the local process in which they are defined. The Linux system also defines standard local environment variables for you by default. Trying to see the list of local variables is done using the set command. The set command displays the entire environment variables set for a specific process. However, this also includes the global environment variables.

Examples #!/bin/bash #Title: myfirstcript.sh #Date: June 26, 2011 #Description: Displaying a list of system global environment variables #Options: none #Creator: SysAdmin

date echo Here are the environment variables shown using the printenv command printenv

#!/bin/bash #Title: myfirstcript.sh #Date: June 26, 2011 #Description: Displays the value stored in a global environment variables #Options: none #Creator: SysAdmin date echo HOSTNAME: $HOSTNAME echo HOME: $HOME echo USER: $USER

Das könnte Ihnen auch gefallen