SlideShare a Scribd company logo
RedHat Enterprise Linux Essential
Unit 11: Configuring the Bash Shell – Shell
                   script
Objectives

Upon completion of this unit, you should be able to:

 Know how to use local and environment variables

 Know how to inhibit variable expansion

 Know how to create aliases

 Understand how the shell parses a command line

 Know how to configure startup files

 Know how to handle input with the read command and
  positional parameters
Bash Variables

 Variables are named values
    Useful for storing data or command output

 Set with VARIABLE=VALUE

 Referenced with $VARIABLE


  $ HI="Hello, and welcome to $(hostname)."
  $ echo $HI
  Hello, and welcome to stationX.
Environment Variables

 Variables are local to a single shell by default

 Environment variables are inherited by child shells
    Set with export VARIABLE=VALUE

    Accessed by some programs for configuration
Some Common Variables

 Configuration variables
    PS1: Appearance of the bash prompt

    PATH: Directories to look for executables in

    EDITOR: Default text editor

    HISTFILESIZE: Number of commands in bash history

 Information variables
    HOME: User's home directory

    EUID: User's effective UID
example PS1

syntax:     PS1='[display content]'
   !                 Display history number
   #                 Display number of current command
   $                 Display $ or #
                    Display symbol 
   d                 Display current date
   h                 Display hostname
   s                 Display shell
   t                 Display current time
   u                 Display user
   W                 Display home work current
   w                 Display full path home work current

PS1='t u@h s $'
Aliases

 Aliases let you create shortcuts to commands

       $ alias dir='ls -laht'

 Use alias by itself to see all set aliases

 Use alias followed by an alias name to see alias value

       $ alias dir

           alias dir='ls -laht'
How bash Expands a Command Line
   Split the line into words
   Expand aliases
   Expand curly-brace statements ({})
   Expand tilde statements (~)
   Expand variables ($)
   Command-substituation ($() and ``)
   Split the line into words again
   Expand file globs (*, ?, [abc], etc)
   Prepare I/O redirections (<, >)
   Run the command!
Preventing Expansion

 Backslash (  ) makes the next character literal
       $ echo Your cost: $5.00
        Your cost: $5.00
 Quoting prevents expansion
    Single quotes (') inhibit all expansion

    Double quotes (") inhibit all expansion, except:
       • $ (dollar sign) - variable expansion

       • ` (backquotes) - command substitution

       •  (backslash) - single character inhibition

       • ! (exclamation point) - history substitution
Login vs non-login shells

 Startup is configured differently for login and non-login shells
 Login shells are:
    Any shell created at login (includes X login)

    su –
 Non-login shells are:
    su

    graphical terminals

    executed scripts

    any other bash instances
Bash startup tasks: profile


 Stored in /etc/profile (global) and ~/.bash_profile (user)

 Run for login shells only

 Used for
    Setting environment variables

    Running commands (eg mail-checker script)
Bash startup tasks: bashrc


 Stored in /etc/bashrc (global) and ~/.bashrc (user)

 Run for all shells

 Used for
    Setting local variables

    Defining aliases
Bash exit tasks


 Stored in ~/.bash_logout (user)

 Run when a login shell exits

 Used for
    Creating automatic backups

    Cleaning out temporary files
Scripting: Taking input
                with positional Parameters
 Positional parameters are special variables that hold the
  command-line arguments to the script.

 The positional parameters available are $1, $2, $3, etc. .
  These are normally assigned to more meaningful variable
  names to improve clarity.

 $* holds all command-line arguments

 $# holds the number of command-line arguments
Scripting: Taking input with the read command

 Use read to assign input values to one or more shell variables:
    -p designates prompt to display

    read reads from standard input and assigns one word to each variable

    Any leftover words are assigned to the last variable

    read -p "Enter a filename: " FILE
   #!/bin/bash
   read -p "Enter several values:" value1 value2
   value3
   echo "value1 is $value1"
   echo "value2 is $value2"
   echo "value3 is $value3"
While
 #!/bin/bash
  # SCRIPT: method1.sh
  # PURPOSE: Process a file line by line with PIPED while-
  read loop.

  FILENAME=$1
  count=0
  cat $FILENAME | while read LINE
  do
  let count++
  echo "$count $LINE"
  done

  echo -e "nTotal $count Lines read"
While with redirect
 #!/bin/bash
  #SCRIPT: method2.sh
  #PURPOSE: Process a file line by line with redirected
  while-read loop.

  FILENAME=$1
  count=0

  while read LINE
  do
  let count++
  echo "$count $LINE"

  done < $FILENAME

  echo -e "nTotal $count Lines read"
Unit 11 configuring the bash shell – shell script
Ad

More Related Content

What's hot (20)

Linux shell scripting
Linux shell scriptingLinux shell scripting
Linux shell scripting
Mohamed Abubakar Sittik A
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
jinal thakrar
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
Rahul Pola
 
Shell programming in ubuntu
Shell programming in ubuntuShell programming in ubuntu
Shell programming in ubuntu
baabtra.com - No. 1 supplier of quality freshers
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
Chandan Kumar Rana
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scripting
Akshay Siwal
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
Gaurav Shinde
 
Shell programming
Shell programmingShell programming
Shell programming
Moayad Moawiah
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
Narendra Sisodiya
 
Unix Basics
Unix BasicsUnix Basics
Unix Basics
Dr.Ravi
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarsystem management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
GauravRaikar3
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
Geeks Anonymes
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
Amit Ghosh
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
Prof. Dr. K. Adisesha
 
Pipes and filters
Pipes and filtersPipes and filters
Pipes and filters
bhatvijetha
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Sudharsan S
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
Dr.Ravi
 
Using Unix
Using UnixUsing Unix
Using Unix
Dr.Ravi
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell Scripting
Mustafa Qasim
 

Viewers also liked (6)

Unit 4 user and group
Unit 4 user and groupUnit 4 user and group
Unit 4 user and group
root_fibo
 
Unit2 help
Unit2 helpUnit2 help
Unit2 help
root_fibo
 
Unit 13 network client
Unit 13 network clientUnit 13 network client
Unit 13 network client
root_fibo
 
Unit 9 basic system configuration tools
Unit 9 basic system configuration toolsUnit 9 basic system configuration tools
Unit 9 basic system configuration tools
root_fibo
 
Administration 1 sw2012
Administration 1 sw2012Administration 1 sw2012
Administration 1 sw2012
Lin Liyue
 
Unit 12 finding and processing files
Unit 12 finding and processing filesUnit 12 finding and processing files
Unit 12 finding and processing files
root_fibo
 
Unit 4 user and group
Unit 4 user and groupUnit 4 user and group
Unit 4 user and group
root_fibo
 
Unit 13 network client
Unit 13 network clientUnit 13 network client
Unit 13 network client
root_fibo
 
Unit 9 basic system configuration tools
Unit 9 basic system configuration toolsUnit 9 basic system configuration tools
Unit 9 basic system configuration tools
root_fibo
 
Administration 1 sw2012
Administration 1 sw2012Administration 1 sw2012
Administration 1 sw2012
Lin Liyue
 
Unit 12 finding and processing files
Unit 12 finding and processing filesUnit 12 finding and processing files
Unit 12 finding and processing files
root_fibo
 
Ad

Similar to Unit 11 configuring the bash shell – shell script (20)

390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
mugeshmsd5
 
Bash shell
Bash shellBash shell
Bash shell
xylas121
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
Acácio Oliveira
 
003 scripting
003 scripting003 scripting
003 scripting
Sherif Mousa
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Introduction to shell scripting ____.ppt
Introduction to shell scripting ____.pptIntroduction to shell scripting ____.ppt
Introduction to shell scripting ____.ppt
nalinisamineni
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
Brahma Killampalli
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
Ohgyun Ahn
 
Customizing the unix environment
Customizing the unix environmentCustomizing the unix environment
Customizing the unix environment
Dr. Girish GS
 
KT on Bash Script.pptx
KT on Bash Script.pptxKT on Bash Script.pptx
KT on Bash Script.pptx
gogulasivannarayana
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
Kenneth Geisshirt
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
Sasidhar Kothuru
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commands
Acácio Oliveira
 
PHP Programming and its Applications workshop
PHP Programming and its Applications workshopPHP Programming and its Applications workshop
PHP Programming and its Applications workshop
S.Mohideen Badhusha
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell Scripting
Open Gurukul
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
Vineet Kumar Saini
 
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptxPHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
mugeshmsd5
 
Bash shell
Bash shellBash shell
Bash shell
xylas121
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
Acácio Oliveira
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Introduction to shell scripting ____.ppt
Introduction to shell scripting ____.pptIntroduction to shell scripting ____.ppt
Introduction to shell scripting ____.ppt
nalinisamineni
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
Ohgyun Ahn
 
Customizing the unix environment
Customizing the unix environmentCustomizing the unix environment
Customizing the unix environment
Dr. Girish GS
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
Kenneth Geisshirt
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commands
Acácio Oliveira
 
PHP Programming and its Applications workshop
PHP Programming and its Applications workshopPHP Programming and its Applications workshop
PHP Programming and its Applications workshop
S.Mohideen Badhusha
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell Scripting
Open Gurukul
 
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptxPHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
kamalsmail1
 
Ad

Recently uploaded (20)

Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptxTop 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
mkubeusa
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
Com fer un pla de gestió de dades amb l'eiNa DMP (en anglès)
CSUC - Consorci de Serveis Universitaris de Catalunya
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptxTop 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
Top 5 Benefits of Using Molybdenum Rods in Industrial Applications.pptx
mkubeusa
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 

Unit 11 configuring the bash shell – shell script

  • 1. RedHat Enterprise Linux Essential Unit 11: Configuring the Bash Shell – Shell script
  • 2. Objectives Upon completion of this unit, you should be able to:  Know how to use local and environment variables  Know how to inhibit variable expansion  Know how to create aliases  Understand how the shell parses a command line  Know how to configure startup files  Know how to handle input with the read command and positional parameters
  • 3. Bash Variables  Variables are named values  Useful for storing data or command output  Set with VARIABLE=VALUE  Referenced with $VARIABLE $ HI="Hello, and welcome to $(hostname)." $ echo $HI Hello, and welcome to stationX.
  • 4. Environment Variables  Variables are local to a single shell by default  Environment variables are inherited by child shells  Set with export VARIABLE=VALUE  Accessed by some programs for configuration
  • 5. Some Common Variables  Configuration variables  PS1: Appearance of the bash prompt  PATH: Directories to look for executables in  EDITOR: Default text editor  HISTFILESIZE: Number of commands in bash history  Information variables  HOME: User's home directory  EUID: User's effective UID
  • 6. example PS1 syntax: PS1='[display content]'  ! Display history number  # Display number of current command  $ Display $ or #  Display symbol  d Display current date  h Display hostname  s Display shell  t Display current time  u Display user  W Display home work current  w Display full path home work current PS1='t u@h s $'
  • 7. Aliases  Aliases let you create shortcuts to commands $ alias dir='ls -laht'  Use alias by itself to see all set aliases  Use alias followed by an alias name to see alias value $ alias dir alias dir='ls -laht'
  • 8. How bash Expands a Command Line  Split the line into words  Expand aliases  Expand curly-brace statements ({})  Expand tilde statements (~)  Expand variables ($)  Command-substituation ($() and ``)  Split the line into words again  Expand file globs (*, ?, [abc], etc)  Prepare I/O redirections (<, >)  Run the command!
  • 9. Preventing Expansion  Backslash ( ) makes the next character literal $ echo Your cost: $5.00 Your cost: $5.00  Quoting prevents expansion  Single quotes (') inhibit all expansion  Double quotes (") inhibit all expansion, except: • $ (dollar sign) - variable expansion • ` (backquotes) - command substitution • (backslash) - single character inhibition • ! (exclamation point) - history substitution
  • 10. Login vs non-login shells  Startup is configured differently for login and non-login shells  Login shells are:  Any shell created at login (includes X login)  su –  Non-login shells are:  su  graphical terminals  executed scripts  any other bash instances
  • 11. Bash startup tasks: profile  Stored in /etc/profile (global) and ~/.bash_profile (user)  Run for login shells only  Used for  Setting environment variables  Running commands (eg mail-checker script)
  • 12. Bash startup tasks: bashrc  Stored in /etc/bashrc (global) and ~/.bashrc (user)  Run for all shells  Used for  Setting local variables  Defining aliases
  • 13. Bash exit tasks  Stored in ~/.bash_logout (user)  Run when a login shell exits  Used for  Creating automatic backups  Cleaning out temporary files
  • 14. Scripting: Taking input with positional Parameters  Positional parameters are special variables that hold the command-line arguments to the script.  The positional parameters available are $1, $2, $3, etc. . These are normally assigned to more meaningful variable names to improve clarity.  $* holds all command-line arguments  $# holds the number of command-line arguments
  • 15. Scripting: Taking input with the read command  Use read to assign input values to one or more shell variables:  -p designates prompt to display  read reads from standard input and assigns one word to each variable  Any leftover words are assigned to the last variable  read -p "Enter a filename: " FILE #!/bin/bash read -p "Enter several values:" value1 value2 value3 echo "value1 is $value1" echo "value2 is $value2" echo "value3 is $value3"
  • 16. While  #!/bin/bash # SCRIPT: method1.sh # PURPOSE: Process a file line by line with PIPED while- read loop. FILENAME=$1 count=0 cat $FILENAME | while read LINE do let count++ echo "$count $LINE" done echo -e "nTotal $count Lines read"
  • 17. While with redirect  #!/bin/bash #SCRIPT: method2.sh #PURPOSE: Process a file line by line with redirected while-read loop. FILENAME=$1 count=0 while read LINE do let count++ echo "$count $LINE" done < $FILENAME echo -e "nTotal $count Lines read"
  翻译: