Successfully reported this slideshow.



Published on

  • Be the first to comment

  • Be the first to like this


  1. 1. Lecture 4<br />ShellUserUNIX shells:<br />HWWhy it is called shell??<br /><ul><li>Layer between the hardware and the user
  2. 2. Command Line interface
  3. 3. Function:
  4. 4. Interpreter ls * ls f1 f2 f3 common in all shells
  5. 5. Shell scripts (programs) differ in syntax from one shell and another
  6. 6. Features </li></ul> <br /><ul><li>History
  7. 7. Auto Completion</li></ul>Bash tab<br />Kshesc you must be in vi mode <br />esc = all occurance<br /><ul><li>Redirection
  8. 8. Default action of the command</li></ul>Command takes the standard input from the keyboard and sends the standard output to the terminal window.<br />You can modify the default action of the standard input, standard output, and standard error within the shell by redirecting stdin, stdout, and stderr.<br /><ul><li>19558090170
  9. 9. Input redirection:
  10. 10.
  11. 11. Forces a command to read the input from a file instead of from the keyboard.
  12. 12. Using the less than (<) metacharacter </li></ul>command < filename<br /><ul><li>Output redirection:
  13. 13. 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.
  14. 14. 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.</li></ul>command > filename<br /> >> Append instead of overwrite <br /><ul><li>Redirecting Standard Error
  15. 15. No error messages appear on the screen.</li></ul>command 2> /dev/null<br /><ul><li>Redirecting the standard output to a file and the standard error to the same file. </li></ul>command 1> filename 2>&1<br /><ul><li>Redirecting standard error to the file. </li></ul>command 2>filename<br /><ul><li> Pipe Character</li></ul> BufferIf there is output will be put in the buffer only if required <br /> input ow won’t look at it<br /><ul><li>The piping has no meaning if there is no output before or the after need no input
  16. 16. bash-3.00$ cal | date
  17. 17. Tue Nov 10 09:46:23 EET 2009
  18. 18. -bash-3.00$ date | cat
  19. 19. Tue Nov 10 10:25:45 EET 2009
  20. 20. -bash-3.00$ cat | date
  21. 21. Tue Nov 10 10:26:34 EET 2009
  22. 22. here suspend waiting for input when enter
  23. 23. -bash-3.00$ touch f1 | touch f2
  24. 24. Both will be touched
  25. 25. -bash-3.00$ touch f1 | date
  26. 26. Tue Nov 10 10:25:45 EET 2009
  27. 27. -bash-3.00$ date | touch f2</li></ul>File will be touched but no date output will be appeared<br />-bash-3.00$ ls | rm<br />Usage error<br />-bash-3.00$ ls /etc/passwd | cat<br />/etc/passwd<br /><ul><li>Aliasing
  28. 28. alias is within the shell
  29. 29. To make an alias:
  30. 30. alias c=clear
  31. 31. alias ls=’ls –l’
  32. 32. To display all alias:
  33. 33. alias [no option] view all aliases
  34. 34. alias alias name view certain alias
  35. 35. To delete all alias:
  36. 36. unalias -a delete all aliases
  37. 37. unalais certain alias delete certain alais
  38. 38. To ignore alias:
  39. 39. ls or /usr/bin/ls to ignore alias
  40. 40. Startup and initialization file</li></ul>User initialization file<br />The primary purpose of the user initialization files is to define the characteristics of a user’s work environment, such as the command-line prompt, the environment variables, and the windowing environment.<br /><ul><li>The commands in the initialization file is executed :
  41. 41. At Login
  42. 42. su – username</li></ul>User startup file<br />Startup fileInitialization fileIt is executed whenever you start new shell<br /><ul><li>No startup file
  43. 43. Environmental variables</li></ul>The shells support two types of variables:<br /><ul><li>Environment variables
  44. 44. Variables that provide information about the user’s environment to every shell program that is started.
  45. 45. Local variables
  46. 46. Variables that affect only the current shell. Any subshell started would not have knowledge of these variables unless it is exported
  47. 47. Environment variables </li></ul>LOGNAME Define the user’s login name<br />HOME Sets the path to the user’s home directory. It is the default <br /> argument for the cd command.<br />SHELL Sets the path to the default shell.<br />$ Hold the id no. of current shell<br />0 Hold the name of the current shell<br />PATH Sets the default path that the shell searches to find commands.<br />MAIL Sets the path to the user’s mailbox.<br />TERM Defines the terminal.<br />LPDEST Sets the user’s default printer.<br />PWD Defines the current working directory.<br />PS1 Defines the shell prompt for the Bourne or Korn shell.<br /><ul><li>To set any variables:</li></ul>VARIABLE = value<br />Ex: X=123<br /><ul><li>To export the variable:</li></ul>export VARIABLE = value<br /><ul><li>Or
  48. 48. export VARIABLE
  49. 49. You can export the variable while assigning it or after you assigned it
  50. 50. 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
  51. 51. To view specific variable:</li></ul>echo $VARIABLE<br />printenv VARIABLE<br /><ul><li>bash-3.00# echo $$
  52. 52. 1572
  53. 53. bash-3.00# echo $SHELL
  54. 54. /sbin/sh
  55. 55. bash-3.00# ps -fp $$
  57. 57. root 1572 1557 0 11:21:44 pts/3 0:00 bash
  58. 58. echo $0
  59. 59. echo $? Assignment</li></ul>bash-3.00# x=123 <br />bash-3.00# y=x <br />bash-3.00# echo $x <br /><ul><li>123
  60. 60. To view all variables:</li></ul>List all environmental variable (having value) + exported local variableenv<br />printenv<br />List all environmental variable (having value) + exported and un exported local variableexport<br />set <br /> <br /><ul><li>File name globing </li></ul>* : Zero or more matching <br />? : any character matching<br />[]: Range matching [a-c] as example<br /><ul><li>Example
  61. 61. ls /usr/bin/*sh
  62. 62. ls /usr/bin/w*
  63. 63. Job control</li></ul> <br /><ul><li>Any command you run on terminal you cannot write another command unless the previous one is executed
  64. 64. 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
  65. 65. Ex:
  66. 66. sleep 100
  67. 67. The prevent command will prevent you from writing any command for 100 second
  68. 68. Run this command in back ground as you know that it will take long time
  69. 69. sleep 100 &
  70. 70. If you don’t 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]
  71. 71. sleep 100
  72. 72. ctl + z : Stop the process in the back ground
  73. 73. bg jobid : Run the job in the background
  74. 74. fg jobid : Run this job back to the foreground
  75. 75. jobs : List all the jobs running with their job id
  76. 76. Process management</li></ul>View process:<br /><ul><li>In order to view the process on your system use ps</li></ul>Options:<br /><ul><li>ps : List the info [pid- process name- consume how much from cpu ] about process running on terminal
  77. 77. ps -f : full information about process
  78. 78. ps -e : Prints information about every process on the system
  79. 79. ps -u username : List the process of certain user
  80. 80. ps -p processid : Prints information about this process [ps –p $$]</li></ul>Know process id of process:<br />pgrep processname<br />Send signals process:<br /><ul><li>Kill -l : view all the signals available
  81. 81. Kill -signal no pid : Send the signal to process
  82. 82. 2Interrupt[ctrl+c] exit without saving can be ignored23Stop running [ctrl+z]25Continue running15“default if no signal is specified with kill” exit after saving can be ignored9Terminate without saving and cannot be ignored
  83. 83. pkill -signal nopname : Send the signal to process by the name
  84. 84. pkill -u username : To terminate the processes owned by this user