Stty Erase ‘^?’ Or Stty Erase ‘^H’ Depending Upon Your Terminal, You May Need to Type Stty Erase and Press Backspace Key, and Type Stty Erase ‘^H’ and Press Ctrl+H
Total Page:16
File Type:pdf, Size:1020Kb
Working with Your Shell Shell as Command Processor: An Introduction Shell allows you to access Linux Bourne family (/bin/sh, /bin/ksh, /bin/bash, /bin/zsh) or C Shell family (/bin/csh, /bin/tcsh) with zsh and bash are commonly used. Shell for customizing the environment Shell acts as an interface between the kernel and the application programs or user commands. A typical shell command execution: “ls –l –t note2 note2”, where ls is the command, ‐l and ‐t are options, all ‐l, ‐t, note1, and note2 are called arguments. Why UNIX Commands Are Noninteractive ? A command may take input from the output of another command. May be scheduled to run at specific times. User input can also be provided through command line arguments. Command arguments need not be known in advance. Allows designing of applications that determine their own behavior by reading configuration files. The Shells (1/2) Shell supports job control, aliases, and history. It is both an interpreter and a scripting language. When you login, an interactive shell presents a prompt and waits for your requests. In fact, an interactive shell runs a noninteractive shell when executing a shell script. Bourne shell was developed by Steve Bourne. C shell was created by Bill Joy at Berkeley. For some time, people use C shell for interactive jobs, while use Bourne shell for programming. The Shells (2/2) Shell variables are of two types: local and environment. PATH, HOME, and SHELL are examples of environment variables. They are available in user’s total environment. Local variable can be defined as: $ DOWNLOAD_DIR=/home/romeo/download The set (a shell builtin) statement displays all variables in the current shell, but the env (an external) command displays only environment variables. We can use export to implement variable inheritance. Export simply converts a local variable to an environment variable. (C shell uses setenv) Common Environment Variables Variable Significance HOME Home directory PATH List of directories searched by shell to local a command LOGNAME Login name of user USER As above MAIL Absolute pathname of user’s mailbox file MAILCHECK Mail checking interval for incoming mail MAILPATH List of mailboxes checked by shell for arrival of mail TERM Type of terminal PWD Absolute pathname of current directory (Korn and Bash only) CDPATH List of directories searched by cd when used nonabsolutely PS1 Primary prompt string PS2 Secondary prompt string SHELL User’s login shell and one invoked by programs having shell escapes Comments on Shell Variables $ PS1 = “C>” C>_ $ PS1=‘[$PWD] ’ [/home/romeo] cd /etc [/etc] _ Bash stores all previous commands in the file $HOME/.bash_history. You can use HISTFILE to assign a different filename. $ cd $HOME/foo has the same effect as $ cd ~/foo The shell executes a profile on login and an rc file when creating a sub‐shell. Example of /bin/csh example‐isp> echo $SHELL /bin/csh example‐isp> finger ejray Login name: ejray In real life: "RayComm" Directory: /home/users/e/ejray Shell: bin/csh On since Jul 23 06:58:48 on pts/16 from calvin.raycomm.com 1 minute 28 seconds Idle Time No unread mail No Plan. example‐isp> Example of /bin/bash [ejr@hobbes ejr]$ echo $SHELL /bin/bash [ejr@hobbes ejr]$ finger ejr Login: ejr Name: Eric J. Ray Directory: /home/ejr Shell: /bin/bash On since Wed Jul 22 07:42 (MDT) on tty1 3 hours 15 minutes idle On since Thu Jul 23 08:17 (MDT) on ttyp0 from calvin No mail. Project: Working on UNIX VQS. Plan: This is my plan‐work all day, sleep all night. [ejr@hobbes ejr]$ Available Shells and Making a Change [ejr@hobbes ejr]$ cat /etc/shells /bin/bash /bin/sh /bin/tcsh /bin/csh /bin/zsh [ejr@hobbes ejr]$ chsh Changing shell for ejr. Password: New shell [/bin/bash]: /bin/zsh Shell changed. ejr@hobbes ~ $ ejr@hobbes ~ $ su ‐ ejr Password: ejr@hobbes ~ $ Note: You may also type shell name to change shell. To exit a shell: type exit or Ctrl+Delete Types of Shell Commands External program on disk which could be – a binary executable (written in C, C++). – a script file (like a shell or perl script). Internal command of the shell which could be – a builtin (like cd, pwd, etc.) – an alias defined by the user that invokes the disk or internal version in a specific manner. Basic Shell Commands Using SHELL environment variable, e.g. > echo $SHELL Using finger userid, e.g. > finger sjkao Seeing which shell are available, e.g. cat /etc/shells Changing shell with chsh, e.g. > chsh or > /bin/zsh Exiting from a temporary subshell, e.g. > exit or crtl + delete Completion in bash and zsh, using tab key Viewing screen history, using keys:↑, ↓,←,→ Changing identity with su, or su – userid Fixing terminal settings with stty or reset The PATH A shell variable (or environment variable) that specifies a list of directories to search. Shell looks at PATH only when command is not used with a pathname and is also not a shell builtin. Command can still be executed if not in PATH by – Using a pathname. – Modifying PATH to include the directory containing the command. PATH can be modified in an absolute or relative manner: PATH=/usr/bin:. (Absolute) PATH=$PATH:/usr/local/bin (Relative) Modified setting is lost after user has logged out unless saved in a startup file. How the Shell Determines the Command to Run If command is invoked with a pathname (like /bin/echo), the shell runs program at the specified location. If command is invoked without a pathname, the shell first checks whether it is an alias or builtin: If alias or builtin, the shell runs it without looking in disk. If not, the shell looks at the PATH variable for directories where the command may reside. Flexibility of Command Usage Run multiple commands in the same line: for example, $ date ; echo $PATH Split a command into multiple lines: $ echo “Hello [Enter] > Dolly” [Enter] Save command output in a file (redirect): date > foo Use output of one command as input of another (pipe): date | cut ‐d” “ ‐f2 Run a command in the background with &: ls ‐lRa / > $HOME/ls‐lRa.txt & Command Classification: A Different Approach Utilities that are generally used in standalone mode (vi, stty, bc). Commands that do useful work but produce no output (mkdir, cp, rm). Commands that produce output which may need further processing (date, who, ls). Commands specially designed to accept output of other commands as their input and vice versa (grep, sort, head). The Wild Cards * Any number of characters, including none ? A single character [x‐z] A single character within the range a‐z {pat1, pat2, …} Match the patterns pat1, pat2, .. [!a‐z] A single character that is not within !(flname) All except flname The * and ? don’t match all filenames beginning with a . and the / of a pathname. Escaping and Quoting \* is escaping, the asterisk has to be treated and matched literally instead of being interpreted as a metacharacter. When a command argument is enclosed in quotes, the meanings of all encloded special characters are turned off. It is quoting. Stdin, Stdout, Stderr Stdin – the file representing input, which is connected to the keyboard. File descriptor 0. 3 possible input source: the keyboard, a file (using <), or another program using a pipeline. Stdout – the file representing output, which is connected to the display. File descriptor 1. 3 possible destinations: the terminal, a file (using> or >>), and as input to another program using a pipeline Stderr – the file representing error messages that emanate from the command or shell. It is also connected to the display. File descriptor 2. Using Completion in the bash shell bash‐2.00$ ls Complete NewProject bogus2 ftp puppy Completed News dead.letter mail temp Mail access Files public_html testme bash‐2.00$ cd public_html/ bash‐2.00$ Note: type cd pub and press the Tab key to complete the public_html. Viewing Session History in the bash Shell [ejr@hobbes clean]$ ls background.htm info.htm logo.gif [ejr@hobbes clean]$ ls (by typing [up arrow] key to reuse the previous command) background.htm info.htm logo.gif [ejr@hobbes clean]$ history 1 free 2 id deb 3 id ejr 4 uname ‐a 5 ls ... 40 cd 41 cp .bash_history oldhistory 42 vi .bash_history 43 elm … [ejr@hobbes clean]$ !40 cd [ejr@hobbes ejr]$ Note: Commands of previous session are kept in the ~/.bask_history file. Changing Your Identity with su [ejr@hobbes asr]$ ls Projects testing [ejr@hobbes asr]$ su asr Password: [asr@hobbes asr]$ ls Projects testing [asr@hobbes asr]$ su ‐ ejr Password: [ejr@hobbes ejr]$ ls Mail editme script2.sed Projects fortunes.copy scriptextra.sed Xrootenv.0 fortunesl.txt sedtest above.Htm fortunes2.txt sorted.address.temp address.book groups temp.htm address.temp history.txt tempsort axhome html.htm test html.html test2 chmod.txt mail testing.gif clean manipulate testing.wp compression nsmail typescript [ejr@hobbes ejr]$ exit [asr@hobbes asr]$ exit [ejr@hobbes ejr]$ exit Note: su without specifying a username, assume you are changing to root. Root Access Security and Hyphen (‐) Be careful when you login as the root. Use su to change to root will provide a little extra security. if you su to another user (no hyphen) and the new user doesn’t have read and execute permissions for the current directory, you will see error message. The addition of the hyphen (‐) will force a new login shell with all environment variables and defaults according to the settings for the user you are switching to. Fixing Terminal Settings with stty If Backspace and Delete keys are not working properly (usually occurs when using telnet), you may fix it using stty erase ‘^?’ or stty erase ‘^H’ Depending upon your terminal, you may need to type stty erase and press Backspace key, and type stty erase ‘^H’ and press Ctrl+H.