Licão 05 scripts exemple

12
Lesson 5 bash Shell Introduction Saving and Running Your Script Starting a Script With #! example of /bin/sh script shell Comments Permissions on the script Debug a script exemples

Transcript of Licão 05 scripts exemple

Lesson 5• bash Shell Introduction

• Saving and Running Your Script

• Starting a Script With #!

• example of /bin/sh script

• shell Comments

• Permissions on the script

• Debug a script exemples

bash shell introduction

To create a shell script:

1. Use a text editor such as vi. Write required Linux commands and logic in the file.

2. save and close the file (exit from vi).

3. make the script executable.

4. test the script, move it to the production environment.

5. simplest program in Bash consists of a line that tells the computer a command.

vi hello.sh

#!/bin/bashecho "Hello, World!"echo "Knowledge is power."

Save and close the file

Saving and Running Your Script

run the script:

./hello.sh

command ./hello.sh displayed an error message on the screen.It will not run script since there’s no execute permission for the script hello.sh.

To execute program, change permissions:

bash: ./hello.sh: Permission denied

chmod +x hello.sh

./hello.shHello, World!Knowledge is power.

Starting a Script With #!

The #! syntax used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. Most Linux shell and perl / python script starts with the following line:

#!/bin/bash#!/usr/bin/perl!/usr/bin/python

1. It is called a shebang or a "bang" line.

2. It is nothing but the absolute path to the Bash interpreter.

3. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.

4. All scripts under Linux execute using the interpreter specified on a first line

5. Almost all bash scripts often begin with #!/bin/bash (if Bash has been installed in /bin)

6. Ensures that Bash will be used to interpret the script, even if it is executed under another shell

7. The shebang was introduced by Dennis Ritchie between Version 7 Unix and 8 at Bell Laboratories. It was then also added to BSD line at Berkeley.

Ignoring An Interpreter Line (shebang) and using sh

If you do not specify an interpreter line, the default is /bin/sh. But, it is recommended that you set #!/bin/bash line.

#!/bin/sh

For a system boot script, use /bin/shIs the standard command interpreter for the system.

example of /bin/sh script : /etc/init.d/policykit

#! /bin/sh### BEGIN INIT INFO# Provides: policykit# Required-Start: $local_fs# Required-Stop: $local_fs# Default-Start: 2 3 4 5# Default-Stop:# Short-Description: Create PolicyKit runtime directories# Description: Create directories which PolicyKit needs at

runtime,# such as /var/run/PolicyKit### END INIT INFO# Author: Martin Pitt <[email protected]>

case "$1" instart)

mkdir -p /var/run/PolicyKitchown root:polkituser /var/run/PolicyKitchmod 770 /var/run/PolicyKit

;;stop|restart|force-reload)

;;*)

echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2exit 3;;

esac:

Shell Comments

#!/bin/bash# A Simple Shell Script To Get Linux Network Information# Vivek Gite - 30/Aug/2009 echo "Current date : $(date) @ $(hostname)" echo "Network configuration" /sbin/ifconfig

• A word or line beginning with # causes that word and remaining characters on that line to be ignored.

• These notes are called comments. It is nothing but explanatory text about script.

• It makes source code easier to understand. These notes are for humans and other sys admins.

• It helps other sys admins to understand your code, logic and it helps to modify the script.

permissions on a scriptlesson 4

chmod +x script.sh #Allowing everyone to execute the script

The chmod is a shell command in Linux. It can change file system modes of files and directories. The modes include permissions and special modes. Each shell script must have the execute permission.

chmod 0755 script.sh #Allowing everyone to execute the script

chmod 0700 script.sh #Only allow owner to execute the script

chmod u=rwx,go= script.sh #Only allow owner to execute the script

chmod u+x script.sh #Only allow owner to execute the script

ls -l script.sh #view the permissions

chmod ug=rx script.sh #user and the group to read and execute only

chmod ug= script.sh #Remove read execute permission for group and user

When script is executed using either bash cmd or dot (.) cmd, you do not have to set executable permissions on script but read permissions need to be set.

Make a script

$ cat trash.sh#!/bin/bash# this script deletes some filescp * trashrm -rf trashmkdir trashecho “Deleted all files!”

We write a program that copies all files into a directory, and then deletes the directory along with its contents. This can be done with the following commands:

$ mkdir trash$ cp * trash$ rm -rf trash$ mkdir trash

Instead of having to type all that interactively on the shell, write a shell program instead:

Debug a script

You need to run a shell script with -x option from the command line:

bash -x script-name

#!/bin/bash -x echo "Hello ${LOGNAME}" echo "Today is $(date)" echo "Users currently on the machine, and their processes:" w

bash -xv script-name

can also modify shebang line to run an entire script in debugging mode:

Use of set builtin commandBash shell offers debugging options which can be turned on or off using set command.

set -x : Display commands and their arguments as they are executed.

set -v : Display shell input lines as they are read.

set -n : Read cmds but do not execute them. Used to check a shell script for syntax errors.

Debug a script Exemple

#!/bin/bash### Turn on debug mode ###set -x

# Run shell commandsecho "Hello $(LOGNAME)"echo "Today is $(date)"echo "Users currently on the machine, and their processes:"w

### Turn OFF debug mode ###set +x

# Add more commands without debug mode

Debug a script Exemple

#!/bin/bashset -n # only read command but do not execute themset -o noexec

echo "This is a test"

# no file is created as bash will only read commands but do not executes them

>/tmp/debug.txt

example using set -n and set -o noexec: