Download presentation
Presentation is loading. Please wait.
1
Linux Shell Script Programming
Chapter 3: Shell Basics Linux Shell Script Programming
2
Linux Shell Script Programming
Chapter Objectives In this chapter, you will: Understand the shell login and logout files Learn about shell variables Become familiar with the shell environment Learn about shell builtin commands Additional Commands Learn about shell grammar Understand command types Linux Shell Script Programming
3
Understanding the Shell Login and Logout Files
When you login, the shell executes and provides you with a shell prompt The default shell is the Bourne-Again Shell, or bash There are startup files in the Linux directory hierarchy used to set up the user’s environment Startup files are read and executed as the user logs in A logout file is read and and executed when a user logs out Linux Shell Script Programming
4
Understanding the Shell Login and Logout Files
If present, the shell reads and executes the /etc/profile script file The /etc/profile script file is read and executed for each user Place commands here that you want to affect all users Place command statements at the end so you won’t interfere with existing commands The permissions on /etc/profile are read and write for root and read for all other users Linux Shell Script Programming
5
Understanding the Shell Login and Logout Files
After the shell reads /etc/profile, for all users, ~/.bash_profile, in each user’s home directory, $HOME, is read The ~ represents each user’s own home directory Another script file, ~/.bashrc, is executed if it exists The ~/.bash_logout script file is executed as a user logs out Linux Shell Script Programming
6
Learning about Shell Variables
A shell variable is a keyword that is set by the shell Each shell variable is used for a specific purpose A shell variable is typically entered in uppercase You precede the shell variable name with a $, as in $PATH Linux Shell Script Programming
7
Learning about Shell Variables
Examples of shell variables: PWD – the most recent working directory set with the cd command OLDPWD – the previous working directory set with the cd command BASH – the full path of the bash shell RANDOM – generates a random number between 0 and 32,767 HOSTNAME – the current hostname of a Linux system Linux Shell Script Programming
8
Learning about Shell Variables
Examples of shell variables IFS – Internal Field Separator, which is used as a separator between shell words PATH – contains the list of directories used to search for commands when executing them HOME – each user’s home directory, typically /home/username, where username is a user’s login name PS1 – shell prompt TMOUT – the amount of time the shell will wait without user input before exiting the shell Linux Shell Script Programming
9
Learning about Shell Variables
Linux Shell Script Programming
10
Becoming Familiar with the Shell Environment
When you execute a command using a partial path, the shell looks to the PATH shell variable Each directory on the path is separated by a colon Search order is left to right Search stops when a match is found If the directory is not found, then an error message is displayed Linux Shell Script Programming
11
Becoming Familiar with the Shell Environment
When using the full path, the shell does not use the PATH variable Instead the shell goes directly to the directory using the specified Example: /bin/cp Linux Shell Script Programming
12
Becoming Familiar with the Shell Environment
The File System Hierarchy standard was developed by several vendors Used to standardize system directories Important use was to use /usr/local/bin to store executable programs and scripts used by all users Use of $HOME/bin to execute a script or program in a user’s home directory Linux Shell Script Programming
13
Learning about Shell Builtin Commands
A builtin command is part of the bash shell Compiled into the shell You cannot access them directly Perform man on the bash to get their manual help pages Linux Shell Script Programming
14
Learning about Shell Builtin Commands
Examples: ./filename – where filename is a script that is read and executed cd – changes the current directory declare – defines a variable echo – displays output exec – causes a command to replace the current shell with no new PID exit – exits a shell with an exit status Linux Shell Script Programming
15
Learning about Shell Builtin Commands
Examples: export – exports a variable to a spawned shell history – displays previous commands kill – sends a signal to a PID let – evaluates an arithmetic expression logout – exits a login shell pwd – displays current working directory read – read characters from the keyboard readonly – makes a variable readonly so it cannot be changed Linux Shell Script Programming
16
Learning about Shell Builtin Commands
Examples: return – returns an exit status set – set and display a shell variable shift – moves positional parameters test – evaluates expressions trap – catches a signal sent to the shell Linux Shell Script Programming
17
Linux Shell Script Programming
Additional Commands Accessed as command contained in a directory (eg. /bin, /usr/bin, etc…) Examples: Shell Programs: sh, csh, ksh, bash Directories / Files: mkdir, rmdir, rm, cd, ls, pwd, cp, mv Files: cat, lpr, touch, more, vi, uniq, sort, grep, cut, paste, diff Misc: cal, date, echo, history, man, who Linux Shell Script Programming
18
Learning about Shell Grammar
Defined as rules that are required for proper shell operation Uses strict syntax Fundamental building blocks Blank Word Name Metacharacter Control operator Reserved word Linux Shell Script Programming
19
Learning about Shell Grammar
A blank is a space or tab used to separate items A word is a sequence of characters considered to be a single unit to the shell A name is a word consisting of letters, numbers and underscore A metacharacter is a character used for a unique and specific purpose by the shell Linux Shell Script Programming
20
Learning about Shell Grammar
Metacharacters | - the pipe symbol allows you to pass output to another command & - the ampersand allows you to run a process in the background ; - the semicolon allows you to separate commands () – the left and right parentheses allow you to run commands in a subshell < - less than redirects input > - greater than redirects output Linux Shell Script Programming
21
Learning about Shell Grammar
Control operators: || - executes a command depending upon failure of another && - executes a command depending upon success of another & - execute in background ; - execute in sequence as separate commands () – execute in a subshell | - pass output to another command A reserved word has special meaning to the shell and cannot be used for another purpose. Example: if, then, else, fi Linux Shell Script Programming
22
Understanding Command Types
Command types rely upon an understanding of shell grammar Command types: Simple command – set of words separated by blanks Examples: ls, touch, pwd, clear Pipelines – sequence of simple commands separated by the pipe symbol Examples: ls | more, who | sort | more List – sequence of one or more pipelines separated by one of these ; & && || terminated by one of these ; & or newline Linux Shell Script Programming
23
Understanding Command Types
Linux Shell Script Programming
24
Understanding Command Types
The ; Operator Causes commands to be executed sequentially in the same shell Example: ls; who The & Operator Causes commands to be executed in a subshell Subshell – spawned from the parent shell Example: ls & pwd Linux Shell Script Programming
25
Understanding Command Types
The && Operator Causes a command to execute only if the preceding command completes successfully (exit status of 0) Example: rm file4.txt && ls The ls command will only execute if rm file4.txt completes successfully The || Operator Causes a command to execute only if the preceding command completes unsuccessfully (exit status of 1) Example: rm file4.txt || pwd The ls command will only execute if rm file4.txt completes unsuccessfully Linux Shell Script Programming
26
Understanding Command Types
Command Types – compound command Allow you to perform calculations, assign variables, perform decision tests and create loops Includes previous command types and Group commands Expressions Decision constructs Looping structures Linux Shell Script Programming
27
Understanding Command Types
Group commands – two forms: (list) – execute list in a subshell Example: (pwd; ls) { list; } – execute list in the current shell Example: { rm file5.txt; pwd; } Expressions used to assign values, perform calculations and test for values – two forms: ((expression)) – evaluates an arithmetic operation Example: ((y=5 + 4)) [[expression]] – tests file attributes, perform string and numeric comparisons Linux Shell Script Programming
28
Understanding Command Types
Linux Shell Script Programming
29
Understanding Command Types
Linux Shell Script Programming
30
Understanding Command Types
Linux Shell Script Programming
31
Linux Shell Script Programming
Chapter Summary The shell can be invoked by either logging in or by entering the name of the shell at the shell prompt. The shell uses startup files to customize the user’s environment. The shell has its own variables that are reserved specifically for its use. They must be entered in uppercase. The File System Hierarchy Standard supports placing scripts used by all users in the /usr/local/bin directory and having users place their own scripts in ~/bin. Linux Shell Script Programming
32
Linux Shell Script Programming
Chapter Summary Shell builtin commands are commands that are compiled as part of the shell. The basic building blocks of shell grammar are: blanks, words, names, metacharacters, control operators, and reserved words. The shell provides for several command types which are simple commands, pipelines, lists, and compound commands Linux Shell Script Programming
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.