SlideShare a Scribd company logo
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/bash
echo "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.sh
Hello, 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/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 <martin.pitt@ubuntu.com>
case "$1" in
start)
mkdir -p /var/run/PolicyKit
chown root:polkituser /var/run/PolicyKit
chmod 770 /var/run/PolicyKit
;;
stop|restart|force-reload)
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 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 script
lesson 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 files
cp * trash
rm -rf trash
mkdir trash
echo “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 command
Bash 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 commands
echo "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/bash
set -n # only read command but do not execute them
set -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:

More Related Content

PDF
Scripting and the shell in LINUX
PDF
Intro to Linux Shell Scripting
PDF
Linux shell scripting tutorial
PDF
Shell script-sec
PDF
Shell scripting
PPT
Unix Shell Scripting Basics
PDF
Linux fundamental - Chap 14 shell script
ODP
OpenGurukul : Language : Shell Scripting
Scripting and the shell in LINUX
Intro to Linux Shell Scripting
Linux shell scripting tutorial
Shell script-sec
Shell scripting
Unix Shell Scripting Basics
Linux fundamental - Chap 14 shell script
OpenGurukul : Language : Shell Scripting

What's hot (20)

PPTX
Easiest way to start with Shell scripting
PPT
Unix Shell Scripting Basics
PPTX
Shell Script Tutorial
PDF
Shell scripting
PPT
Shell Scripting
PDF
Linux shell ggsipu-lug
PPT
Unix And Shell Scripting
ODP
PPTX
Licão 02 shell basics bash intro
PDF
Introduction to shell scripting
PDF
Quick start bash script
PPTX
Bash Shell Scripting
ODP
Shellprogramming
PDF
PDF
PDF
The Shell Game Part 2: What are your shell choices?
PDF
The hacker choice
PDF
Statyczna analiza kodu PHP
PDF
Real programmers use programming languages (Not shell scripts)
KEY
Linecook - A Chef Alternative
Easiest way to start with Shell scripting
Unix Shell Scripting Basics
Shell Script Tutorial
Shell scripting
Shell Scripting
Linux shell ggsipu-lug
Unix And Shell Scripting
Licão 02 shell basics bash intro
Introduction to shell scripting
Quick start bash script
Bash Shell Scripting
Shellprogramming
The Shell Game Part 2: What are your shell choices?
The hacker choice
Statyczna analiza kodu PHP
Real programmers use programming languages (Not shell scripts)
Linecook - A Chef Alternative
Ad

Viewers also liked (13)

PDF
101 3.8.1 vim course book
PPT
101 2.4 use debian package management
PPT
101 4.5 manage file permissions and ownership
PPT
101 4.3 control mounting and unmounting of filesystems
PPT
101 apend. networking generic a
PPT
Apend. networking linux
PPT
101 apend. networking generic b - details
PPT
101 4.1 create partitions and filesystems
PPT
101 2.5 use rpm and yum package management
PPTX
Licão 12 decision loops - statement iteration
PPTX
Licão 03 vi editor
PPT
101 2.1 design hard disk layout v2
PPT
Linux Networking Commands
101 3.8.1 vim course book
101 2.4 use debian package management
101 4.5 manage file permissions and ownership
101 4.3 control mounting and unmounting of filesystems
101 apend. networking generic a
Apend. networking linux
101 apend. networking generic b - details
101 4.1 create partitions and filesystems
101 2.5 use rpm and yum package management
Licão 12 decision loops - statement iteration
Licão 03 vi editor
101 2.1 design hard disk layout v2
Linux Networking Commands
Ad

Similar to Licão 05 scripts exemple (20)

PDF
Shell scripting _how_to_automate_command_l_-_jason_cannon
PPTX
Linux System Administration
PDF
Shell scripting1232232312312312312312312
PDF
Shell-Scripting-1.pdf
PDF
3.1.d manual bash script guide lsstv 2.0r11
PPT
Bash shell
PDF
Shell Programming_Module2_Part2.pptx.pdf
PDF
Course 102: Lecture 10: Learning About the Shell
DOCX
What is a shell script
PDF
Text mining on the command line - Introduction to linux for bioinformatics
PPTX
Shell & Shell Script
PPTX
Shell & Shell Script
PDF
Shell tutorial
PPTX
Bash Shell Scripting
PDF
Bash production guide
PPT
Spsl by sasidhar 3 unit
ODP
Introduction to Shell script
PPTX
Shell scrpting(payal harne)
PPT
390aLecture05_12sp.ppt
Shell scripting _how_to_automate_command_l_-_jason_cannon
Linux System Administration
Shell scripting1232232312312312312312312
Shell-Scripting-1.pdf
3.1.d manual bash script guide lsstv 2.0r11
Bash shell
Shell Programming_Module2_Part2.pptx.pdf
Course 102: Lecture 10: Learning About the Shell
What is a shell script
Text mining on the command line - Introduction to linux for bioinformatics
Shell & Shell Script
Shell & Shell Script
Shell tutorial
Bash Shell Scripting
Bash production guide
Spsl by sasidhar 3 unit
Introduction to Shell script
Shell scrpting(payal harne)
390aLecture05_12sp.ppt

More from Acácio Oliveira (20)

PPTX
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
PPTX
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
PPTX
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
PPTX
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
PPTX
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
PPTX
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
PPTX
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
PPTX
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
PPTX
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
PPTX
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
PPTX
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
PPTX
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
PPTX
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
PPTX
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
PPTX
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
PPTX
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
PPTX
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
PPTX
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
PPTX
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
PPTX
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptx

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Big Data Technologies - Introduction.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
Teaching material agriculture food technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
Big Data Technologies - Introduction.pptx
The AUB Centre for AI in Media Proposal.docx
Teaching material agriculture food technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Modernizing your data center with Dell and AMD
NewMind AI Monthly Chronicles - July 2025
Digital-Transformation-Roadmap-for-Companies.pptx
Review of recent advances in non-invasive hemoglobin estimation

Licão 05 scripts exemple

  • 1. 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
  • 2. 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/bash echo "Hello, World!" echo "Knowledge is power." Save and close the file
  • 3. 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.sh Hello, World! Knowledge is power.
  • 4. 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.
  • 5. 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/sh Is the standard command interpreter for the system.
  • 6. 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 <martin.pitt@ubuntu.com> case "$1" in start) mkdir -p /var/run/PolicyKit chown root:polkituser /var/run/PolicyKit chmod 770 /var/run/PolicyKit ;; stop|restart|force-reload) ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 exit 3 ;; esac :
  • 7. 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.
  • 8. permissions on a script lesson 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.
  • 9. Make a script $ cat trash.sh #!/bin/bash # this script deletes some files cp * trash rm -rf trash mkdir trash echo “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:
  • 10. 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 command Bash 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.
  • 11. Debug a script Exemple #!/bin/bash ### Turn on debug mode ### set -x # Run shell commands echo "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
  • 12. Debug a script Exemple #!/bin/bash set -n # only read command but do not execute them set -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: