SlideShare uma empresa Scribd logo
1 de 28
Baixar para ler offline
L2B Second Linux Course



Session_3


             Please visit our Facebook Group
L2B Second Linux Course
L2B Linux course



     Session Outlines:
     1­Command ine Shortcuts 
     2­Command Line Expansion
     3­History and Editing Tricks
     4­Write Simple shell scripts
     5­Lab 
     6­Redirect I/O channels to files
     7­connect commands using pipes 
     8­Use the for loops to iterate over sets of values
     9­Lab
L2B Second Linux Course
L2B Linux course


  1­Command Line shortcut ­File Globbing
        Globbing is wildcard expansion:
             * ­ matches zero or more characters
             ? ­ matches any single character
             [0­9] ­ matches a range of numbers
             [abc] ­ matches any of the character in the list
             [^abc] ­ matches all except the characters in the list
             Predefined character classes can be used
               syntax of character classes is [:keyword:],where keyword can be: 
               alpha , upper,lower,digit,alnum,punct,space.
L2B Second Linux Course
L2B Linux course


    1­Command Line shortcut ­File Globbing­Examples
    if a directory contains the following files : Ahmed.txt , 
       ahmed.txt , Mohammed.mp3, 3ali .mp3,7alid.mp3
    try these commands 
        $ls ­l *.txt                      $ls ­l  [[:alnum:]]*
        $ls ­l  *.mp3                     $ls ­l [[:punct:]]*
        $ls ­l  ?h*                       $ls ­l [[:upper:]]*
        $ls ­l [[:digit:]]*               $ls ­l [[:lower:]]*
        $ls ­l  [[:alpha:]]*              $ls ­l [^[:digit:]]*
L2B Second Linux Course
L2B Linux course


   1­Command Line shortcut­ History
   ­bash stores a history of commands you've entered, which can be used to repeat commands
   ­Use history command to see list of "remembered" commands

   $ history                    !!        repeats last command
   14 cd /tmp                   !char     repeats last command that started with char
   15 ls ­l                     !num      repeats a command by its number in history
   16 cd                        !?abc     repeats last command that started with abc
   17 cp /etc/passwd .          !­n       repeats command entered n commands back
   18 vi passwd                 ^old^new
   ... output truncated ...
L2B Second Linux Course
L2B Linux course


    1­Command Line shortcut­ More History tricks
       Use the up and down keys to scroll through previous commands
       Type Ctrl­r to search for a command in command history.
          To recall last argument from previous
           Esc,. (the escape key followed by a period)
           Alt­. (hold down the alt key while pressing the period)
           !$  (only valid for last command )
            • Ls  !$
L2B Second Linux Course
L2B Linux course


    2­Command Line Expansion­ The Tilde
       Tilde ( ~ ) ­ borrowed from the C shell
       May refer to your home directory
           $ cat ~/.bash_profile
       May refer to another user's home directory
           $ ls ~julie/public_html
L2B Second Linux Course
L2B Linux course

    2­Command Line Expansion­  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
           FILES=$(ls /etc/)
            • echo $FILES
L2B Second Linux Course
L2B Linux course


    2­Command Editing Tricks
         Ctrl-a moves to beginning of line
         Ctrl-e moves to end of line
         Ctrl-u deletes to beginning of line
         Ctrl-k deletes to end of line
         Ctrl-arrow moves left or right by word
L2B Second Linux Course
L2B Linux course


    2­Gnome­terminal
      Applications->Accessories->Terminal
      Graphical terminal emulator that supports
       multiple "tabbed" shells
        Ctrl-Shift-t creates a new tab
        Ctrl-PgUp/PgDn switches to next/prev tab
        Ctrl-Shift-c copies selected text
        Ctrl-Shift-v pastes text to the prompt
L2B Second Linux Course
L2B Linux course


    Scripting Basics   
       Shell scripts are text files that contain a series of
       commands or statements to be executed.
       Shell scripts are useful for:
         Automating commonly used commands
         Performing system administration and
         troubleshooting
          Creating simple applications
          Manipulation of text or files
L2B Second Linux Course
L2B Linux course


Creating shell scripts   
Step 1: Use such as vi to create a text file containing commands
     First line contains the magic shebang sequence: #!
        • #!/bin/bash­  used for bash scripts(most common on linux)     
        • #!/bin/sh­ used for Bourne shell scripts(common on UNIX­like systems) 
        • #!/bin/csh­used for C shell scripts (most common on BSD derived systems) 

        • #!/usr/bin/perl­used for Perl scripts(advanced scripting& programming 
          language) 
        • #!/usr/bin/python­used for python scripts(an OOP language) 
  Comment your scripts!
    Comments start with a #
L2B Second Linux Course
L2B Linux course


 Creating shell scripts  
    Step 2: Make the script executable:
        $ chmod u+x myscript.sh
     To execute the new script:
        Place the script file in a directory in the executablepath ­OR­
         • echo $PATH
        Specify the absolute or relative path to the script on the command 
        line
L2B Second Linux Course
L2B Linux course


    Sample shell Script 
    #!/bin/bash
    # This script displays some information about your environment
    echo "Greetings. The date and time are $(date)"
    echo "Your working directory is: $(pwd)"
L2B Second Linux Course
L2B Linux course




                     LAB
L2B Second Linux Course
L2B Linux course


Standard input and output
   Linux provides three I/O channels to programs
      Standard input (STDIN) - keyboard by default
      Standard output (STDOUT) - terminal window by default
      Standard error (STDERR) - terminal window by default
 # ls ­l myfile fakefile
 ls: fakefile: No such file or directory
 ­rw­r­­r­­ 1 root root 0 Feb 17 02:06 myfile
L2B Second Linux Course
L2B Linux course

 Redirecting Output to a File
    STDOUT and STDERR can be redirected to files:
        command operator filename
    Supported operators include:
          > Redirect STDOUT to file
          2> Redirect STDERR to file
        &> Redirect all output to file
    File contents are overwritten by default. >> appends.
L2B Second Linux Course
L2B Linux course


 Redirecting Output to a File-Examples
    This command generates output and errors when run as
    non-root:
     $ find /etc -name passwd
    Operators can be used to store output and errors:
     $ find /etc -name passwd > find.out
     $ find /etc -name passwd 2> /dev/null
     $ find /etc -name passwd > find.out 2> find.err
L2B Second Linux Course
L2B Linux course


    Redirecting STDOUT to a Program(Piping)
       Pipes (the | character) can connect commands:
           command1 | command2
           Sends STDOUT of command1 to STDIN of command2 instead 
            of the screen.
           STDERR is not forwarded across pipes
       Used to combine the functionality of multiple tools
           command1 | command2 | command3... etc
L2B Second Linux Course
L2B Linux course


    Redirecting STDOUT to a Program Examples
       less: View input one page at a time:
        $ ls -l /etc | less
           Input can be searched with /
       mail: Send input via email:
        $ echo "test email" | mail -s "test" user@example.com
       lpr : Send input to a printer
        $ echo "test print" | lpr
        $ echo "test print" | lpr -P printer_name
L2B Second Linux Course
L2B Linux course


    Combining Output and Errors
       Some operators affect both STDOUT and STDERR
         &>: Redirects all output:
          $ find /etc -name passwd &> find.all
         2>&1: Redirects STDERR to STDOUT
          • Useful for sending all output through a pipe
          $ find /etc -name passwd 2>&1 | less
       (): Combines STDOUTs of multiple programs
            $ ( cal 2007 ; cal 2008 ) | less
L2B Second Linux Course
L2B Linux course


    Redirecting to Multiple Targets (tee)
       $ command1 | tee filename | command2
       Stores STDOUT of command1 in filename, then pipes to
       command2
       Uses:
          Troubleshooting complex pipelines
          Simultaneous viewing and logging of output
          Ls -lR /etc | tee stage1.out | sort | tee stage2.out | uniq -c | tee
          stage3.out | sort -r | tee stage4.out | less
          generate_report.sh | tee report.txt
L2B Second Linux Course
L2B Linux course


    Redirecting STDIN from a File
       Redirect standard input with <
       Some commands can accept data redirected to STDIN from
       a file:
           $ tr 'A-Z' 'a-z' < .bash_profile
           This command will translate the uppercase characters
           in .bash_profile to lowercase
       Equivalent to:
           $ cat .bash_profile | tr 'A-Z' 'a-z'
L2B Second Linux Course
L2B Linux course


 Sending Multiple Lines to STDIN
   Redirect multiple lines from keyboard to STDIN with <<WORD
      All text until WORD is sent to STDIN
      Sometimes called a heretext
 $ mail -s "Please Call" jane@example.com <<END
 > Hi Jane,
 >
 > Please give me a call when you get in. We may need
 > to do some maintenance on server1.
 >
 > Details when you're on-site,
 > Boris
 > END
L2B Second Linux Course
L2B Linux course


 Scripting: for loops
      Performs actions on each member of a set of values
      Example:
 for NAME in joe jane julie
 do
      ADDRESS="$NAME@example.com"
      MESSAGE='Projects are due today!'
      echo $MESSAGE | mail -s Reminder $ADDRESS
 done
L2B Second Linux Course
L2B Linux course


 Scripting: for loops-continued
     Can also use command-output and file lists:
       for num in $(seq 1 10)
         • Assigns 1-10 to $num
         • seq X Y prints the numbers X through Y
       for file in *.txt
         • Assigns names of text files to $file
L2B Second Linux Course
L2B Linux course




                     LAB
For More info Please Visit Our Facebook Group

Mais conteúdo relacionado

Mais procurados (20)

Linux
LinuxLinux
Linux
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
 
NTP Software Jan 2012 Monthly Meeting IPC Presentation
NTP Software Jan 2012 Monthly Meeting IPC PresentationNTP Software Jan 2012 Monthly Meeting IPC Presentation
NTP Software Jan 2012 Monthly Meeting IPC Presentation
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Linux
LinuxLinux
Linux
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
 
Unix reference sheet
Unix reference sheetUnix reference sheet
Unix reference sheet
 
Linux CLI
Linux CLILinux CLI
Linux CLI
 
Linux Interview Questions Quiz
Linux Interview Questions QuizLinux Interview Questions Quiz
Linux Interview Questions Quiz
 
Unit 7
Unit 7Unit 7
Unit 7
 
archive A-Z linux
archive A-Z linuxarchive A-Z linux
archive A-Z linux
 
Important Linux Commands
Important Linux CommandsImportant Linux Commands
Important Linux Commands
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Linux programming - Getting self started
Linux programming - Getting self started Linux programming - Getting self started
Linux programming - Getting self started
 
Lpi lição 01 exam 101 objectives
Lpi lição 01  exam 101 objectivesLpi lição 01  exam 101 objectives
Lpi lição 01 exam 101 objectives
 
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic CommandsChapter 4 Linux Basic Commands
Chapter 4 Linux Basic Commands
 
Comenzi unix
Comenzi unixComenzi unix
Comenzi unix
 
List command linux a z
List command linux a zList command linux a z
List command linux a z
 
UNIX/Linux training
UNIX/Linux trainingUNIX/Linux training
UNIX/Linux training
 

Semelhante a Session3

Semelhante a Session3 (20)

Using Unix
Using UnixUsing Unix
Using Unix
 
Unit 6 bash shell
Unit 6 bash shellUnit 6 bash shell
Unit 6 bash shell
 
Ch3 gnu make
Ch3 gnu makeCh3 gnu make
Ch3 gnu make
 
60761 linux
60761 linux60761 linux
60761 linux
 
Unit 7 standard i o
Unit 7 standard i oUnit 7 standard i o
Unit 7 standard i o
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorial(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorial
 
Raspberry Pi - Lecture 2 Linux OS
Raspberry Pi - Lecture 2 Linux OSRaspberry Pi - Lecture 2 Linux OS
Raspberry Pi - Lecture 2 Linux OS
 
Unix fundamentals and_shell scripting
Unix fundamentals and_shell scriptingUnix fundamentals and_shell scripting
Unix fundamentals and_shell scripting
 
LINUX_admin_commands.pptx
LINUX_admin_commands.pptxLINUX_admin_commands.pptx
LINUX_admin_commands.pptx
 
cisco
ciscocisco
cisco
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
3. intro
3. intro3. intro
3. intro
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
LinuxCommands (1).pdf
LinuxCommands (1).pdfLinuxCommands (1).pdf
LinuxCommands (1).pdf
 
Linux
LinuxLinux
Linux
 

Mais de Learn 2 Be

Mais de Learn 2 Be (6)

Programming Concepts 01
Programming Concepts 01Programming Concepts 01
Programming Concepts 01
 
00 Intro
00  Intro00  Intro
00 Intro
 
01 Variables
01 Variables01 Variables
01 Variables
 
Cryptography
CryptographyCryptography
Cryptography
 
Linux1
Linux1Linux1
Linux1
 
L2 B Embedded Systems
L2 B Embedded SystemsL2 B Embedded Systems
L2 B Embedded Systems
 

Último

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Último (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

Session3

  • 1. L2B Second Linux Course Session_3 Please visit our Facebook Group
  • 2. L2B Second Linux Course L2B Linux course Session Outlines: 1­Command ine Shortcuts  2­Command Line Expansion 3­History and Editing Tricks 4­Write Simple shell scripts 5­Lab  6­Redirect I/O channels to files 7­connect commands using pipes  8­Use the for loops to iterate over sets of values 9­Lab
  • 3. L2B Second Linux Course L2B Linux course 1­Command Line shortcut ­File Globbing  Globbing is wildcard expansion:     * ­ matches zero or more characters     ? ­ matches any single character     [0­9] ­ matches a range of numbers     [abc] ­ matches any of the character in the list     [^abc] ­ matches all except the characters in the list     Predefined character classes can be used syntax of character classes is [:keyword:],where keyword can be:  alpha , upper,lower,digit,alnum,punct,space.
  • 4. L2B Second Linux Course L2B Linux course 1­Command Line shortcut ­File Globbing­Examples if a directory contains the following files : Ahmed.txt ,  ahmed.txt , Mohammed.mp3, 3ali .mp3,7alid.mp3 try these commands  $ls ­l *.txt $ls ­l  [[:alnum:]]* $ls ­l  *.mp3 $ls ­l [[:punct:]]* $ls ­l  ?h* $ls ­l [[:upper:]]* $ls ­l [[:digit:]]* $ls ­l [[:lower:]]* $ls ­l  [[:alpha:]]* $ls ­l [^[:digit:]]*
  • 5. L2B Second Linux Course L2B Linux course 1­Command Line shortcut­ History ­bash stores a history of commands you've entered, which can be used to repeat commands ­Use history command to see list of "remembered" commands $ history !!   repeats last command 14 cd /tmp !char  repeats last command that started with char 15 ls ­l !num  repeats a command by its number in history 16 cd !?abc  repeats last command that started with abc 17 cp /etc/passwd . !­n  repeats command entered n commands back 18 vi passwd ^old^new ... output truncated ...
  • 6. L2B Second Linux Course L2B Linux course 1­Command Line shortcut­ More History tricks Use the up and down keys to scroll through previous commands Type Ctrl­r to search for a command in command history.    To recall last argument from previous Esc,. (the escape key followed by a period) Alt­. (hold down the alt key while pressing the period) !$  (only valid for last command ) • Ls  !$
  • 7. L2B Second Linux Course L2B Linux course 2­Command Line Expansion­ The Tilde Tilde ( ~ ) ­ borrowed from the C shell May refer to your home directory $ cat ~/.bash_profile May refer to another user's home directory $ ls ~julie/public_html
  • 8. L2B Second Linux Course L2B Linux course 2­Command Line Expansion­  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 FILES=$(ls /etc/) • echo $FILES
  • 9. L2B Second Linux Course L2B Linux course 2­Command Editing Tricks Ctrl-a moves to beginning of line Ctrl-e moves to end of line Ctrl-u deletes to beginning of line Ctrl-k deletes to end of line Ctrl-arrow moves left or right by word
  • 10. L2B Second Linux Course L2B Linux course 2­Gnome­terminal Applications->Accessories->Terminal Graphical terminal emulator that supports multiple "tabbed" shells Ctrl-Shift-t creates a new tab Ctrl-PgUp/PgDn switches to next/prev tab Ctrl-Shift-c copies selected text Ctrl-Shift-v pastes text to the prompt
  • 11. L2B Second Linux Course L2B Linux course Scripting Basics    Shell scripts are text files that contain a series of commands or statements to be executed. Shell scripts are useful for: Automating commonly used commands Performing system administration and troubleshooting Creating simple applications Manipulation of text or files
  • 12. L2B Second Linux Course L2B Linux course Creating shell scripts    Step 1: Use such as vi to create a text file containing commands First line contains the magic shebang sequence: #! • #!/bin/bash­  used for bash scripts(most common on linux)      • #!/bin/sh­ used for Bourne shell scripts(common on UNIX­like systems)  • #!/bin/csh­used for C shell scripts (most common on BSD derived systems)  • #!/usr/bin/perl­used for Perl scripts(advanced scripting& programming  language)  • #!/usr/bin/python­used for python scripts(an OOP language)  Comment your scripts! Comments start with a #
  • 13. L2B Second Linux Course L2B Linux course Creating shell scripts   Step 2: Make the script executable: $ chmod u+x myscript.sh  To execute the new script: Place the script file in a directory in the executablepath ­OR­ • echo $PATH Specify the absolute or relative path to the script on the command  line
  • 14. L2B Second Linux Course L2B Linux course Sample shell Script  #!/bin/bash # This script displays some information about your environment echo "Greetings. The date and time are $(date)" echo "Your working directory is: $(pwd)"
  • 15. L2B Second Linux Course L2B Linux course LAB
  • 16. L2B Second Linux Course L2B Linux course Standard input and output Linux provides three I/O channels to programs Standard input (STDIN) - keyboard by default Standard output (STDOUT) - terminal window by default Standard error (STDERR) - terminal window by default # ls ­l myfile fakefile ls: fakefile: No such file or directory ­rw­r­­r­­ 1 root root 0 Feb 17 02:06 myfile
  • 17. L2B Second Linux Course L2B Linux course Redirecting Output to a File STDOUT and STDERR can be redirected to files: command operator filename Supported operators include:   > Redirect STDOUT to file   2> Redirect STDERR to file &> Redirect all output to file File contents are overwritten by default. >> appends.
  • 18. L2B Second Linux Course L2B Linux course Redirecting Output to a File-Examples This command generates output and errors when run as non-root: $ find /etc -name passwd Operators can be used to store output and errors: $ find /etc -name passwd > find.out $ find /etc -name passwd 2> /dev/null $ find /etc -name passwd > find.out 2> find.err
  • 19. L2B Second Linux Course L2B Linux course Redirecting STDOUT to a Program(Piping) Pipes (the | character) can connect commands: command1 | command2 Sends STDOUT of command1 to STDIN of command2 instead  of the screen. STDERR is not forwarded across pipes Used to combine the functionality of multiple tools command1 | command2 | command3... etc
  • 20. L2B Second Linux Course L2B Linux course Redirecting STDOUT to a Program Examples less: View input one page at a time: $ ls -l /etc | less Input can be searched with / mail: Send input via email: $ echo "test email" | mail -s "test" user@example.com lpr : Send input to a printer $ echo "test print" | lpr $ echo "test print" | lpr -P printer_name
  • 21. L2B Second Linux Course L2B Linux course Combining Output and Errors Some operators affect both STDOUT and STDERR &>: Redirects all output: $ find /etc -name passwd &> find.all 2>&1: Redirects STDERR to STDOUT • Useful for sending all output through a pipe $ find /etc -name passwd 2>&1 | less (): Combines STDOUTs of multiple programs $ ( cal 2007 ; cal 2008 ) | less
  • 22. L2B Second Linux Course L2B Linux course Redirecting to Multiple Targets (tee) $ command1 | tee filename | command2 Stores STDOUT of command1 in filename, then pipes to command2 Uses: Troubleshooting complex pipelines Simultaneous viewing and logging of output Ls -lR /etc | tee stage1.out | sort | tee stage2.out | uniq -c | tee stage3.out | sort -r | tee stage4.out | less generate_report.sh | tee report.txt
  • 23. L2B Second Linux Course L2B Linux course Redirecting STDIN from a File Redirect standard input with < Some commands can accept data redirected to STDIN from a file: $ tr 'A-Z' 'a-z' < .bash_profile This command will translate the uppercase characters in .bash_profile to lowercase Equivalent to: $ cat .bash_profile | tr 'A-Z' 'a-z'
  • 24. L2B Second Linux Course L2B Linux course Sending Multiple Lines to STDIN Redirect multiple lines from keyboard to STDIN with <<WORD All text until WORD is sent to STDIN Sometimes called a heretext $ mail -s "Please Call" jane@example.com <<END > Hi Jane, > > Please give me a call when you get in. We may need > to do some maintenance on server1. > > Details when you're on-site, > Boris > END
  • 25. L2B Second Linux Course L2B Linux course Scripting: for loops Performs actions on each member of a set of values Example: for NAME in joe jane julie do ADDRESS="$NAME@example.com" MESSAGE='Projects are due today!' echo $MESSAGE | mail -s Reminder $ADDRESS done
  • 26. L2B Second Linux Course L2B Linux course Scripting: for loops-continued Can also use command-output and file lists: for num in $(seq 1 10) • Assigns 1-10 to $num • seq X Y prints the numbers X through Y for file in *.txt • Assigns names of text files to $file
  • 27. L2B Second Linux Course L2B Linux course LAB
  • 28. For More info Please Visit Our Facebook Group