320 likes | 504 Views
Chapter 4 LINUX Shells. Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert Koretsky, Syed A. Sarwar. expanded by Jozef Goetz, 2006. Objectives. To describe what a UNIX/LINUX shell is To describe briefly some commonly used shells.
E N D
Chapter 4 LINUX Shells Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert Koretsky, Syed A. Sarwar expanded by Jozef Goetz, 2006
Objectives • To describe what a UNIX/LINUX shell is • To describe briefly some commonly used shells
what a UNIX/LINUX shell • acts as an interface between the user and the UNIX/LINUX kernel • goal: interpret the user’s cmds • Shell = Command Interpreter • Shellcommand can be • internal - built-incmd (part of the shell process - bg, cd, continue, echo, exec) or • externalcmd (grep, more ,cat, mkdir, rmdir, ls) • a file in the form of a binary executableprogram fileor • a shell script • to terminate a shell press <^D>
how it works • after reading the user’s cmd, it determines whether cmd is • internal • own code is executed or • externalcmd • search for a file that has the name of the cmd by searching several directories • the search path is determine in the shell variables PATH (or pathin the TC shell) • You can view the search path by using echo $PATH
PATH and path variables • searchPATHvariable defined in a hidden file called • .profile or .login in a system start-up fileor • start-up file(ha dot file - .bashrcfor Bash and .cshrcfile for TC shell) in your home directory. in Bourne, Korn, and Bash shell (separated by “:” ) in TC shell (separated by “ “ ) // set path, the search starts with ~/bin and then with . // ~ represents a home directory
PATH and path variables • To change the search path temporary - change the value of PATH PATH=~/bin:$PATH:. • The search path has been modified by 2 directories, ~/bin and . (current directory) • To change the search path permanently you need to change it in the corresponding “.” dot file
Intro to UNIX Shell • The UNIX/LINUX shell is a program that starts running when you log on and interprets the commands that you type.
Which Shell Suits Your Needs? good features of sh and csh and is a superset of sh more adv programming features than tcsh equally powerful in their interactive use reach common-level interface good programming language
Table 4.1 Shell Locations and Program Names chsh – change the default shell chsh –l available shells with a path [jgoetz jgoetz]$ chsh -l /bin/bash /bin/sh /bin/bash2 /bin/tcsh /bin/csh /bin/badsh
Shell Similarities exec /bin/csh - the same as issuing command csh
Ways to Change Your Shell When you 1st logon your default shell will be shown by typing: $ echo $SHELL /bin/csh $ chsh –l// gives you a list of all shells available You can change your shell one of 2 ways: • You can change to a new default for every subsequent login session on your system • Changingthe defaultshell: $ chsh // may not working on your system ask you for your login password then type complete path shell: e.g. /bin/tcsh 2. You can create additional shell sessions running on top of, or concurrently with, the default shell • Create or run additional shells on top of your default shell $ echo $ SHELL /usr/bin/sh $csh// new one % // usepsto test it or • exec /bin/csh
Start-up Files • Initial systemstart-up file • has initial settings of important variables for the shell and some other utilities /etc/profile • When you start particular shell the corresponding shell start-up file • found in the user’s home dir • initially configured by the administrator • executes: .profile // Bourne, Korn .cshrc .kshrc // Korn .bashrc .zshrc .tcshrc
Table 4.5 Some importantShell Startup Files for Bash and TC Shells Numbers show possible sequence of command execution from the corresponding file 1. When you log on and your login shell is Bash, it firstexecutes commands in the /etc/profile file, if this file exists. 2. It then searches for the • ~/.bash_profile, • ~/.bash_login, or • ~/.profile file, • in this order, and executes commands in the first of these that is found and is readable. 3. When you start an interactive Bash shell, it executes commands in the ~/.bashrc file, if this file exists and is readable. 4. When a login Bash exits, it executes commands in the ~/.bash_logout file. • When started non-interactively to run a shell script (see Chapters 15 and 16), Bash looks for the environment variable BASH ENV to find out the name of the file to be executed. 1 2 3 bash 4 bash
Table 4.5 Some importantShell Startup Files for Bash and TC Shells • If your shell is a TC shell, it executes commands in the • /etc/csh.cshrc or • /etc/.cshrc file, if it exists and is readable. • A login shell then executes commands in the /etc/csh.login file, if it exists. 1. Every shell (login or non-login) then executes commands in the ~/.tcshrc file (or the ~/.cshrc file if ~/.tcshrcdoes not exist), followed by reading the ~/.history file. 2. and 3. A login shellthen executes commands in the ~/.login and ~/.cshdirs files. 4. When a login TC shell exits, it executes commands in the /etc/csh.logout and ~/.logout files, if they exist and are readable 1 2 3 bash 4 bash 1 for tcsh 2 for tcsh 3 for tcsh 4 for tcsh
Shell Start-up Files and Environment Variables • set | more • gives you shell variables values • to set a variable • set history = 10 for csh (to get it do: exec /bin/csh) • setenv | more or • setenv variable settings
Environment Variables seen in shell start-up files C shell are in lowercase, others are Bourne and Korn shells
Displaying files: display all contents of f1, f2: • cat f1 f2 • more f1 f2 • pg f1 f2 // for some shells
Communication Commands • Communication Commands: • $who// check the user name to whom you want to talk • $ mesg [y|n] // permit execution write or talk $ write user [tty] $ talk user [tty] - shows 2 sections of the screen: sender and receiver $ biff [y|n] // notification on/off
Command Aliases p.55[3] • The alias command can be used to create pseudonyms (nicknames) for commands • they can be placed in ~/.profile or ~/.login • executes when you log on • but typically in a shell start files .bashrc ( .cshrc for TC shell) • every time you start Tcsh or Bash
Command Aliases • Syntax for the aliascommand is: • alias [name [ = string ] …] // for Bourne, Korn, Bash shells • alias l=‘ls –la’ • alias [name [ string ] ] // C shell • alias l ‘ls –la’ • alias • list all aliases • remove it • unalias ls // remove ls • unalias –a // remove all
alias Command Examples The \!* string is substituted by the actual parameter passed to the given command
Displaying System Up Time • uptime 1:16pm up 28 days, 10:44, 2 users, load average: 0.04, 0.01, 0.00 • displays the duration of time the system has been running since it was last booted, # of users and some additional info
Shell Metacharacters • These are the characters other than letters and digits that have special meaning to the shell. • They cannot be used in shell commands without specifying them in particular way • Allow you to specify • multiple files inmultiple directories in one command line.
Examples of Shell Metacharacters $ lpr -Pspr [0-9][a-zA-Z].html • print on the spr printer, P specifies the printer name, • then the names of all the files as follows: • 2 chr file names with the first chr digit and 2nd being an uppercase or lowercase letter $ $ ls lab[0-9]??.c lab11a.c lab1a1.c lab123.c lab4ab.c • displays 6 chr long files with .c extension
Examples of Shell Metacharacters $ lpr -Pspr [0-9][a-zA-Z].html • print on the spr printer, P specifies the printer name, • then the names of all the files as follows: • 2 chr file names with the first chr digit and 2nd being an uppercase or lowercase letter $ $ ls lab[0-9]??.c lab11a.c lab1a1.c lab123.c lab4ab.c • displays 6 chr long files with .c extension