Function:: UNIX Shells
Function:: UNIX Shells
Function:: UNIX Shells
UNIX shells:
Why it is called shell??
Layer between the hardware and the user Command Line interface
User Shell HW
Function:
ls * ls f1 f2 f3 common in all shells differ in syntax from one shell and another
Interpreter
Features
History
Auto Completion
Bash Ksh tab esc \ esc = you must be in vi mode all occurance
Redirection
Default action of the command
Command takes the standard input from the keyboard and sends the standard output to the terminal window. You can modify the default action of the standard input, standard output, and standard error within the shell by redirecting stdin, stdout, and stderr.
Input redirection:
Forces a command to read the input from a file instead of from the keyboard. Using the less than (
<) metacharacter
Output redirection:
Sends the output from a command into a file instead of sending the output to the screen. As the command generates error messages, these messages are sent to the standard error. Usually error messages are sent to the terminal screen.
Using the greater than ( ) metacharacter to direct the standard output to a file instead of printing the output to the screen. If file does not exist, the system creates it. If the file exists, the redirection overwrites the contents of the file.
>
>>
Pipe Character
The piping has no meaning if there is no output before or the after need no input bash-3.00$ cal | Tue Nov 10 09:46:23 EET 2009 date
cat
-bash-3.00$ cat | date Tue Nov 10 10:26:34 EET 2009 here suspend waiting for input when enter -bash-3.00$ touch f1 Both will be touched | touch f2
date
touch f2
File will be touched but no date output will be appeared -bash-3.00$ Usage error ls | rm
-bash-3.00$ /etc/passwd
ls /etc/passwd |
cat
Aliasing
alias is within the shell
To make an alias:
alias alias c=clear ls=ls l [no option] alias name view all aliases view certain alias delete all aliases delete certain alais
To ignore alias:
User initialization file The primary purpose of the user initialization files is to define the characteristics of a users work environment, such as the command-line prompt, the environment variables, and the windowing environment. The commands in the initialization file is executed :
At Login su username User startup file It is executed whenever you start new shell
Initialization file Startup file
No startup file
Environmental variables
The shells support two types of variables: Environment variables Variables that provide information about the users environment to every shell program that is started. Local variables Variables that affect only the current shell. Any subshell started would not have knowledge of these variables unless it is exported Environment variables
LOGNAME Define the users login name HOME Sets the path to the users home directory. It is the default argument for the cd command. SHELL Sets the path to the default shell. $ Hold the id no. of current shell 0 Hold the name of the current shell PATH Sets the default path that the shell searches to find commands. MAIL Sets the path to the users mailbox. TERM Defines the terminal. LPDEST Sets the users default printer. PWD Defines the current working directory. PS1 Defines the shell prompt for the Bourne or Korn shell.
Ex:
VARIABLE = value
X=123 To export the variable:
export VARIABLE
You can export the variable while assigning it or after you assigned it Once you export a variable it can be seen by another sub shells not another terminals once you close the terminal it will no more being exists
echo printenv
$VARIABLE VARIABLE
bash-3.00# echo $$ 1572 bash-3.00# echo $SHELL /sbin/sh bash-3.00# ps -fp $$ UID PID PPID C STIME TTY root 1572 1557 0 11:21:44 pts/3 echo $0 echo $? Assignment bash-3.00# x=123 bash-3.00# y=x bash-3.00# echo $x 123
List all environmental variable (having value) + exported and un exported local variable
Example
ls ls /usr/bin/*sh /usr/bin/w*
Job control
Any command you run on terminal you cannot write another command unless the previous one is executed So if a command take a lot of time in execution would you wait for it or open new terminal or Run this command in back ground while your terminal control this command in back ground Ex:
sleep
-
100
The prevent command will prevent you from writing any command for 100 second Run this command in back ground as you know that it will take long time
sleep
-
100 &
If you dont add & from the beginning and then 2 numbers will be displayed the job id [as when more than one job is running in back ground they are put in stack ]and the process id [ we deal with this number in process management]
100
Stop the process in the back ground Run the job in the background Run this job back to the foreground List all the jobs running with their job id
Process management
View process: Options: ps : List the info [pid- process name- consume how much from cpu ] about process running on terminal ps -f : full information about process ps -e : Prints information about every process on the system ps -u username : List the process of certain user ps -p processid : Prints information about this process [ps p $$] In order to view the process on your system use
ps
pgrep
Send signals process:
processname
Kill -l : view all the signals available Kill -signal no pid : Send the signal to process 2 23 25 15
Interrupt[ctrl+c] exit without saving can be ignored Stop running [ctrl+z] Continue running default if no signal is specified with kill exit after saving can be ignored
pkill -signal no pname : Send the signal to process by the name pkill -u username : To terminate the processes owned by this
user