Licão 05 scripts exemple

download Licão 05 scripts exemple

of 12

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Licão 05 scripts exemple

PowerPoint Presentation

Lesson 5bash Shell IntroductionSaving and Running Your ScriptStarting a Script With #!example of /bin/sh scriptshell CommentsPermissions on the scriptDebug a script exemples

bash shell introductionTo create a shell script:

Use a text editor such as vi. Write required Linux commands and logic in the file. 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!/bin/bashecho "Hello, World!"echo "Knowledge is power."Save and close the fileSaving and Running Your Scriptrun the script:./hello.shcommand ./ displayed an error message on the screen.It will not run script since theres no execute permission for the script

To execute program, change permissions:bash: ./ Permission deniedchmod +x

./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/pythonIt is called a shebang or a "bang" line.It is nothing but the absolute path to the Bash interpreter.It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.All scripts under Linux execute using the interpreter specified on a first lineAlmost all bash scripts often begin with #!/bin/bash (if Bash has been installed in /bin)Ensures that Bash will be used to interpret the script, even if it is executed under another shellThe 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 shIf you do not specify an interpreter line, the default is /bin/sh. But, it is recommended that you set #!/bin/bash line.#!/bin/shFor a system boot script, use /bin/sh Is 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

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/ifconfigA 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 4chmod +x #Allowing everyone to execute the scriptThe 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 everyone to execute the scriptchmod 0700 allow owner to execute the scriptchmod u=rwx,go= allow owner to execute the scriptchmod u+x allow owner to execute the scriptls -l the permissionschmod ug=rx and the group to read and execute only chmod ug= read execute permission for group and userWhen 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!/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 scriptYou 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:" wbash -xv script-namecan 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 modeDebug 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.txtexample using set -n and set -o noexec: