Bash Scripting Introduction PDF
Document Details
Uploaded by UsefulAlgebra
2005
Tags
Summary
This document is an introduction to Bash scripting. It covers the basics of shell programming concepts, execution, variables, command-line arguments, command substitution, and basic coding principles. The document also provides examples of shell scripts and commands.
Full Transcript
Introduction to BASH Scripting To introduce the concept of shell programming To discuss how shell programs are executed To describe the concept and use of shell variables To discuss how command line arguments are passed to shell programs T...
Introduction to BASH Scripting To introduce the concept of shell programming To discuss how shell programs are executed To describe the concept and use of shell variables To discuss how command line arguments are passed to shell programs To explain the concept of command substitution To describe some basic coding principles To write and discuss some shell scripts To cover the commands and primitives Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Introduction Shell script: a shell program, consists of shell commands to be executed by a shell and is stored in ordinary UNIX file Shell variable: read/write storage place for users and programmers to use as a scratch pad for completing a task Program control flow commands (or statements): allow non sequential execution of commands in a shell script and repeated execution of a block of commands Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Running a Bourne Shell Script Ways of running a Bourne Shell – Make the script file executable by adding the execute permission to the existing access permissions for the file $ chmod u+x script_file $ – Run the /bin/bash command with the script file as its parameter $ /bin/bash script_file $ – Force the current shell to execute a script in the Bourne Again shell, regardless of your current shell #!/bin/bash Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Shell Variables Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Read-only Shell Variables Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Display Shell Variables $ set AUTHSTATE=compat CGI_DIRECTORY=/usr/lpp/internet/server_root/cgi-bin ERRNO=10 FCEDIT=/usr/bin/ed HOME=/home/inst/msarwar IFS=’ ‘ LOCPATH=/usr/lib/nls/loc LOGIN=msarwar LOGNAME=msarwar MAIL=/usr/spool/mail/msarwar MAILCHECK=600 MAILMSG=’[YOU HAVE NEW MAIL]’ PATH=/usr/lpp/workbench/bin:/usr/lpp/Java/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/local/bin:/ usr/local/share/bin:/home/inst/msarwar/bin:/usr/bin/X11:/sbin:. PPID=18632 PS1=’$ ‘ PS2=’> ‘ PS3=’#? ‘ PS4=’+ ‘ PWD=/home/inst/msarwar SHELL=/usr/bin/ksh TERM=vt100 TMOUT=0 TZ=PST8PDT USER=msarwar $ Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Display Shell Variables $ env LANG=en_US LOGIN=msarwar NLSPATH=/usr/lib/nls/msg/%L/%N:/usr/lib/nls/msg/%L/%N.cat IMQCONFIGCL=/etc/IMNSearch/dbcshelp PATH=/usr/lpp/workbench/bin:/usr/lpp/Java/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/ usr/local/bin:/usr/local/share/ bin:/home/inst/msarwar/bin:/usr/bin/X11:/sbin:. IMQCONFIGSRV=/etc/IMNSearch CGI_DIRECTORY=/usr/lpp/internet/server_root/cgi-bin LOGNAME=msarwar MAIL=/usr/spool/mail/msarwar LOCPATH=/usr/lib/nls/loc USER=msarwar DOCUMENT_SERVER_MACHINE_NAME=pccaix AUTHSTATE=compat SHELL=/usr/bin/ksh ODMDIR=/etc/objrepos DOCUMENT_SERVER_PORT=80 HOME=/home/inst/msarwar TERM=vt100 MAILMSG=[YOU HAVE NEW MAIL] PWD=/home/inst/msarwar DOCUMENT_DIRECTORY=/usr/lpp/internet/server_root/pub TZ=PST8PDT $ Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Reading and Writing Shell Variables variable1=value1[ariable2=value2…variableN=valueN] Purpose: Assign values ‘value1,…,valueN’ to ‘variable1, …, variableN’ respectively –no space allowed before and after the equals sign Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Reading and Writing Shell Variables $ name=John $ echo $name John $ name=John Doe Doe: not found $ name=”John Doe” $ echo $name John Doe $ name=John* $ echo $name John.Bates.letter John.Johnsen.memo John.email $ echo “$name” John* $ echo “The name $name sounds familiar!” The name John* sounds familiar! $ echo \$name $name $ echo ‘$name’ $name $ Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Command Substitution Command Substitution: When a command is enclosed in back quotes, the shell executes the command and substitutes the command (including back quotes) with the output of the command `command` Purpose: Substitute its output for `command` Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Command Substitution Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Exporting Environment export [name-list] Purpose Export the names and copies of the current values in the ‘name-list’ to every command executed from this point on Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Resetting Variables unset [name-list] Purpose Reset or remove the variable or function corresponding to the names in ‘name-list’, where ‘name- list’ is a list of names separated by spaces Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Creating Read-Only Defined Variables readonly [name-list] Purpose Prevent assignment of new values to the variables in ‘name-list’ Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Reading from Standard Input read variable-list Purpose Read one line from standard input and assign words in the line to variables in ‘name-list’ Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Passing Arguments to Shell Scripts shift[N] Purpose Shift the command line arguments N positions to the left set [options] [argument-list] Purpose Set values of the positional arguments to the arguments in ‘argument-list’ when executed without an argument, the set command displays the names of all shell variables and their current values Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Special Characters for the echo Command Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Passing Arguments to Shell Scripts $ cat cmdargs_demo #!/bin/sh echo “The command name is: $0.” echo “The number of command line arguments passed as parameters are $#.” echo “The value of the command line arguments are: $1 $2 $3 $4 $5 $6 $7 $8 $9.” echo “Another way to display values of all of the arguments: $@.” echo “Yet another way is: $*.” exit 0 $ cmdargs_demo a b c d e f g h i The command name is: cmdargs_demo. The number of command line arguments passed as parameters are 9. The value of the command line arguments are: a b c d e f g h i. Another way to display values of all of the arguments: a b c d e f g h i. Yet another way is: a b c d e f g h i. $ cmdargs_demo One Two 3 Four 5 6 The command name is: cmdargs_demo. The number of command line arguments passed as parameters are 6. The value of the command line arguments are: One Two 3 Four 5 6. Another way to display values of all of the arguments: One Two 3 Four 5 6. Yet another way is: One Two 3 Four 5 6. $ Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Passing Arguments to Shell Scripts $ cat shift_demo #!/bin/sh echo “The program name is $0.” echo “The arguments are: $@” echo “The first three arguments are: $1 $2 $3” shift echo “The program name is $0.” echo “The arguments are: $@” echo “The first three arguments are: $1 $2 $3” shift 3 echo “The program name is $0.” echo “The arguments are: $@” echo “The first three arguments are: $1 $2 $3” exit 0 $ shift_demo 1 2 3 4 5 6 7 8 9 10 11 12 The program name is shift_demo. The arguments are: 1 2 3 4 5 6 7 8 9 10 11 12 The first three arguments are: 1 2 3 The program name is shift_demo. The arguments are: 2 3 4 5 6 7 8 9 10 11 12 The first three arguments are: 2 3 4 The program name is shift_demo. The arguments are: 5 6 7 8 9 10 11 12 The first three arguments are: 5 6 7 $ Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Passing Arguments to Shell Scripts Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Comments and Program Headers Put comments in your programs to describe the purpose of a particular set of commands Use program header for every shell script you write, including: – Name of the file containing the script – Name of the author – Date written – Date last modified – Purpose of the script – A brief description of the algorithm used to implement the solution to the problem at hand Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Program Control Flow Commands Used to determine the sequence in which statements in a shell script execute The three basic types of statements for controlling flow of a script are: – Two-way branching – Multiway branching – Repetitive execution of one or more commands Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Program Control Flow Commands Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Program Control Flow Commands Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Operators for the test Command Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Example Script Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Program Control Flow Commands Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Example Script Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Program Control Flow Commands Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Example Script Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Program Control Flow Commands Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The for Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The for Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The for Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The while statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The while statement $ cat while_demo #!/bin/sh secretcode=agent007 echo “Guess the code!” echo “Enter your guess: \c” read yourguess while [ “$secretcode” != “$yourguess” ] do echo “Good guess but wrong. Try again!” echo “Enter your guess: \c” read yourguess done echo “Wow! You are a genius!!” exit 0 $ while_demo Guess the code! Enter your guess: star wars Good guess but wrong. Try again! Enter your guess: columbo Good guess but wrong. Try again! Enter your guess: agent007 Wow! You are a genius!! $ Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The until Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The until Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The break and continue Statements Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The case Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The case Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The case Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved. The case Statement Copyright © 2005 Pearson Addison-Wesley. All rights reserved.