1 / 32

RHA030 Linux Computing Essentials

RHA030 Linux Computing Essentials. Workbook 6 The Bash Shell Part 1 – Chapters 1 - 3. Workbook 6. The Bash Shell. This Week Chapter 1. Introduction to Bash Chapter 2. Command Lists and Scripts Chapter 3. Bash Variables Next week Chapter 4. Command Line Expansion

inoke
Télécharger la présentation

RHA030 Linux Computing Essentials

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RHA030 Linux Computing Essentials Workbook 6 The Bash Shell Part 1 – Chapters 1 - 3

  2. Workbook 6. The Bash Shell This Week Chapter 1. Introduction to Bash Chapter 2. Command Lists and Scripts Chapter 3. Bash Variables Next week Chapter 4. Command Line Expansion Chapter 5. Shell Customizations Chapter 6. Sourcing Shell Scripts and Shell Initialization This workbook is really about local user profiles RHA030 Linux Computing Essentials

  3. Chapter 1 – Introduction to Bash Key Concepts • The default shell in Red Hat Enterprise Linux is the bash shell. • The bash shell can be used interactively, or as a powerful scripting language. • bash allows users to customize their shell & local environment. • All shells have functionality – such as history. • The bash shell maintains a history of the command lines that it executes. • Command lines can be retrieved from the history using various history expansions that begin with "!". RHA030 Linux Computing Essentials

  4. What is a Shell ? • By now we know that a shell is just a program: • And there are many different shell programs available. /bin/sh /bin/ksh /bin/ csh /bin/tcsh /bin/bash • BASH is the most commonly used. • They are run from an executable file. • The executable files for shells are stored in /bin • They act as an interface between the user and the system. RHA030 Linux Computing Essentials

  5. Interactive Shells vs. Shell Scripts • A shell can be used 2 ways: • Interactively, to run commands • Provides the user a command-line interface to the system. • Interpreting the commands as they are run. • Manipulating the commands input and output • Or as a powerful scripting language. • Called Shell Script files • They are written using the basic shell commands • And common programming concepts • Used for many many linux configuration files RHA030 Linux Computing Essentials

  6. Interactively, to run commands • A shell interprets the contents of the commands as they are run • A command is really just another program which the shell runs. • For every action performed there are 2 possible results. • A shell can use redirection to manipulate the location of a commands Input and output 2> RHA030 Linux Computing Essentials

  7. It uses standard I/O devices on the system to manipulate the commands input/output. RHA030 Linux Computing Essentials

  8. Shells functionality allow us to use redirection • The shell provides a way to redirect flow of information to and from a command away from these standard devices. • It also allows you to separate the error outputs. RHA030 Linux Computing Essentials

  9. This shells functionality also allow us to use command piping • A pipe passes the output from one command to become the input of another command. Figure 8-2: Piping information from one command to another RHA030 Linux Computing Essentials

  10. Piping can also get very complicated. • Example of pipe & redirection: • ls –l /etc| sort –r | head -12 > savefile RHA030 Linux Computing Essentials

  11. Using the Shell HistoryFunction • A shell keeps a history of each command entered by the user history – allows you to view your current history. • It is held in the file called~/.bash_history • Arrow keys give you access to history • The shells functionality also offers the use of substitution to provides a variety of ways for the user to use the commands held in history. • !! Previous command • !n Command number • n !-n The nth most recent command • !cmd The most recent command that began cmd RHA030 Linux Computing Essentials

  12. The .bashrc file • As part of its initialization, the bash shell will look for a file called .bashrc in a user's home directory. • This file is used to customize the bash shell • Everytime you start a new bash shell it will read the ~/.bashrcandexecute any commands it finds in it. • ~/.bashrc = one of the local user profile script files • It is read upon login and every time a new shell (subshell) is started. • Allows users to customize their local environment & shell functions. RHA030 Linux Computing Essentials

  13. Modifying the .bashrc file The RHA online exercise will get you to modify your ~/.bashrc • Use vi to open and edit the file # comment line = this will run date to collect a timestamp date >> .bash_timestamps • Or use echo >> to append this command to ~/.bashrc echo “# This will run date to collect a timestamp” >> ~./.bashrc echo date >> .bash_timestamps >> ~./.bashrc RHA030 Linux Computing Essentials

  14. Chapter 2 – Command lists & Scripts Key Concepts • Multiple commands can be separated with a ; • Upon exiting, every command returns an integer to its parent called a return value. • The shell variable $? expands to the return value of previously executed command. • && and || conditionally separate multiple commands. RHA030 Linux Computing Essentials

  15. Running Multiple Commands • These workbook examples are being used to help develop your understanding of subshells. • Example 1 - Multiple commands can be separated with a ; • But w hen using a “;” to run multiple commands the shell's current working directory is changed as the commands are run. [elvis@station elvis]$ cd /etc/X11; ls applnk prefdm sysconfig xorg.conf.backup xkb desktop-menus proxymngr twm xorg.conf.wbx Xmodmap fs rstart X xorg.conf.works Xresources gdm serverconfig xdm XftConfig.README-OBSOLETE xserver lbxproxy starthere xorg.conf xinit xsm [elvis@station X11]$ RHA030 Linux Computing Essentials

  16. In this workbook example they now use a sub-shell in which to run the commands • Example 2 - commands are wrapped in ( parenthesis) • When using “parenthesis” to run multiple commands the shell's current working directory is left unchanged. • When bash encounters (parenthesis) on a command line, it spawns an entirely new child bash process (called a subshell). • And runs the commands within the subshell. • After running the commands, the subshell exits • BUT the user is left in the original (unchanged) shell. [elvis@station elvis]$ (cd /etc/X11; ls) applnk prefdm sysconfig xorg.conf.backup xkb desktop-menus proxymngr twm xorg.conf.wbx Xmodmap fs rstart X xorg.conf.works Xresources gdm serverconfig xdm XftConfig.README-OBSOLETE xserver lbxproxy starthere xorg.conf xinit xsm [elvis@station elvis]$ RHA030 Linux Computing Essentials

  17. What is a return value ? • Upon exiting, every command returns an integer to the system called a return value. • The shell uses this to determine whether the command worked or failed. • The current value is held in the shell variable $? • It can be displayed to see the if the previously executed command succeeded. • This example shows how the ls command has succeeded. [elvis@station elvis]$ ls -l /etc/passwd -rw-r--r-- 1 root root 3694 Aug 15 16:26 /etc/passwd [elvis@station elvis]$ echo $? 0 • In contrast, the following example shows how the ls command has failed [elvis@station elvis]$ ls -l /etc/password ls: /etc/password: No such file or directory [elvis@station elvis]$ echo $? 127 RHA030 Linux Computing Essentials

  18. Conditionally - Running Multiple Command • You can join commands with the “ ; “ • But then will actually all independently - one after the after • The bash shell also allows for you to join 2 commands conditionally. • Using either && or || [elvis@station elvis]$ command1 <condition > command2 • How they work: • When commands are conditionally joined, • The first will always execute. • The second command may Or may Not execute. • This will depend on the return value of the first command • Which will be either succeed and be a ‘0’ or fail and be a ‘1’ RHA030 Linux Computing Essentials

  19. Difference of && and || • When using && - 2nd command will only run if the 1st command succeeds • Meaning it has a return value of 0. [elvis@station elvis]$ mkdir /tmp/boring && mv numbers.txt /tmp/boring • When using || - 2nd command will only run if the 1st command "fails" • Meaning it has a return value of 1. [elvis@station elvis]$ ls –l /tmp/boring || mkdir /tmp/boring RHA030 Linux Computing Essentials

  20. Chapter 3 – Bash Variables • Key Concepts • Shell variables are assigned using an A=apple syntax. • Variables are examined with the $ character, as in echo $A. • At the kernel level, every process (such as a subshell) has a collection of environment variables, which are inherited by child processes. • The export command converts a shell variable into an environment variable. • The set and env commands list shell variables and environment variables, respectively. RHA030 Linux Computing Essentials

  21. What is a variable? • A variable is a just a placeholder for information • A variable has a name which holds a value. <variable-name>=value sheila=teacher • It contains accessible information that can be changed. • Changing the value of a variable is called setting the variable. • You use and display the value of a variable: • You do this by putting a $ sign infront of the variable name. • This tells the shell you are referring to the value of the variable • NOT the variable itself. echo $sheila RHA030 Linux Computing Essentials

  22. All operating systems use variables • A multiuser environment needs them just to run. • Configuration scripts commonly use variables • allows you to change the information being used within the system • Without having to actually edit every system configuration files or script. USER=Sheila $ echo “Welcome to the system $USER” $ Welcome to the system Sheila • Because the variable name never changes • It is the value of the variable which is changed • This then causes the change in all the system configuration files & scripts. RHA030 Linux Computing Essentials

  23. The shell manages the variables There are 2 types of variables: • Local user-defined variables: • Commonly called local variables • Created and defined by the local user • A user can create their own local variables in any shell. • Environment variables: • Contains system information or properties which system needs to run. • These are used by the system and programs in the running of the overall of the system and accessed regularly. • Such as required for each user profile. • Such as your unique login id. RHA030 Linux Computing Essentials

  24. Creating or Changing value of a variable: • To create a new local variable name you use the following format. <variable-name>=value Sheila=teacher • Displayying the value of the variable echo $Sheila Teacher • Double quotes should surround an entire string. Sheila= “Sheila is the teacher” echo $Sheila RHA030 Linux Computing Essentials

  25. Environment Variables • System variables • Most variables available in the system are global or system wide environment variables. They are used to allow a networking or multiuser environment. And are automatically setup for each user as you login. • They are created in a global or system wide environment startup files such as in the /etc/profile /etc/bashrc RHA030 Linux Computing Essentials

  26. Examples - Environment Variables Table 8-3 (continued): Common BASH environment variables RHA030 Linux Computing Essentials

  27. Displaying Environmental Variables • Displays all currently available variables & their current values • Displays both local & global variables. setcommand: • Environment variables are always in capital letters: • To view contents of a specified variable you must always use a $ sign infront of the variable name. echo $HOME echo ${HOME} RHA030 Linux Computing Essentials

  28. Exporting Variables • For a local variable to be automatically available to the entire or global environment it needs to be exported or declared for use. • A local variables needs to be exported so it becomes an environment variable • Then the entire system becomes aware of them. • It declares the existence of defined variables within the system. • The export command Is used to do this. export - lists all the currently exported / declared available variables export teacher - Used with an existing variable it will export/declare it RHA030 Linux Computing Essentials

  29. Global + Local Environment Startup Files • The global or system wide environment startup files are in /etc /etc/profile /etc/bashrc • Each user also has a set of local shell environment startup files. ~/.bash_profile ~/.bashrc • A user can use these local files to create local permanent variables • Or to modify some of the global variable values to suit their specific needs in their own local environment • Next week we will discuss how these files work together in detail. RHA030 Linux Computing Essentials

  30. Creating or changing variables User-defined variables • A user can create their own local variables in any shell. • Or modify the value of some of the global variables. • But it will still only be available in the current shell. Unless you export them. • This make them available to any new subshells & processes Even then they will still only be temporary • To make them available permanently you will need to put them into your shell’s local script startup environment files. • More on this next week. RHA030 Linux Computing Essentials

  31. Modifying the local value of a global variable Example = $PATH Environment Variable 1. Displays the current settings on the system path. echo $PATH 2. Backup the current path settings into a new variable OLDPATH=$PATH 3. Add your new directory ensuring you don’t overwrite the existing settings already being used on the system path. PATH=$PATH: newpathname to be added 4. Now you need to export the system path to declare the new value. export path RHA030 Linux Computing Essentials

  32. Now do these exercises 1. Handout – Understanding Shell Functions + Variables 2. RHA Workbook 6 – online exercises - chapters 1 – 3 3. Handout – 3rd Practice Assessment RHA030 Linux Computing Essentials

More Related