Chapter 9 Linux Shell 1 Shell and Kernel Shell and desktop enviroment provide user interface 2 1
Shell Shell is a Unix term for the interactive user interface with an operating system A shell usually implies an interface with a command syntax User types the command at prompt, and shell convert and execute it, or pass to the kernel 3 Shell functions Shell provides : Interactive user interface Interpret user commands which can be read from standard input device, or from a file. User enviroment Providing a user enviroment, which be configured individually using configuration files Shell programming 4 2
Shell types sh or Bourne Shell The original shell on Unix systems bash or Bourne Again shell The standard shell. Expanded from sh csh or C shell tcsh or TENEX C shell ksh or the Korn shell 5 Shell types The default shell for user account specified in /etc/passwd file echo $SHELL List of valid login shells on the system cat /etc/shells Switch to another shell (ex. sh) and exit $ sh $ exit Change the default shell chsh s /bin/sh Current shell echo $0 6 3
Login shell & non-login shell When a user logs into the system, the user is given a shell, which is knows as a login shell There are many non-login shell When a user starts instance of a shell within the X Window system, that is a nonlogin shell When a start another shell at the shell prompt bash>bash bash> Sub-shell Non-login shell 7 The Interactive User Interface Command syntax Built-in Commands Support Command History Support Command Completion Support editing commands Run multiple command Redirection output and input Command Substitution Special characters 8 4
Command syntax command [options] [arguments] Command : name of program Options : options of program, begin or - - Arguments : filename, directory, string, [ ] : optional, not required Using \ to enter a command in multiple line Using Ctrl_C to stop executing a command 9 Built-in Commands Built-in command are contained within the shell itself. Bash built-in commands: alias, bind, builtin, cd, command, declare, echo, enable, help, let, local, logout, printf, read, shopt, type, typeset, ulimit, unalias 10 5
Support command history All command that you entered at the shell prompt is saved in the ~/.bash_history Select one command from history list by: Using Up Arrow key Using history command $ history $ history a $!! $!N 11 Support Command Completion a helpful feature when you need to enter a very long file name in a command line Press TAB key while entering a command tar zxvf /software/units-1.74.tar.gz Press TAB Press TAB 12 6
Support editing commands Bash shell allows to edit commands you type at the shell prompt, using Readline Library editor 13 Run multiple command in command line command1 ; command2 ; command3 ; [commandn] $ who ; date ; echo $SHELL ; ls / Commands be executed in succession 14 7
Input - Output Redirection There are three file descriptor for every command stdin (0) : is the input provided to a particular command to process stdout (1) : is the output from a particular command stderr (2) : is the error code generated by command 15 Input - Output Redirection Default, stdin is the input that read from the keyboard, stdout (stderr) is the output that send to the screen. cat./vd.txt In Linux, It s possible to send output to file or to read input from file, with 3 redirection symbols >, >>, < 16 8
Input - Output Redirection Redirecting output : command output>file_or_device tail /var/log/messages 1>lastmessage.txt tail /var/log/messages >lastmessage.txt cat./file0.txt cat./file0.txt cat./file0.txt 2>errorfile.txt 1>readme.txt 2>errorfile.txt 1>readme.txt 2>&1 cat./file1.txt >> readme.txt 17 Input - Output Redirection Redirecting Input : command <input_file sort sort <./inputfile or sort inputfile sort <./inputfile >./outputfile stdin inputfile sort stdout outputfile ls?? 18 9
Pipe A pipe is a way to connect the output of one command to the input of another command without any temporary file command1 command2 ls more ls sort ls wc l ls grep vd who sort who wc l cat /var/log/messages grep dev more 19 Command Substitution Allows the output of one command to be used as arguments to another ls l ` find./ -name vd* ` or ls l $( find./ -name vd* ) 20 10
Special Characters Include \ ( ; # $? & * ( ) [] ` + These characters have a special meaning or function If they are used in way that not have special meaning, must be use with \ (back-slash) echo ` find./ -name vd* ` echo \` find./ -name vd* \` echo $PS1 echo $PS1 //double quotes //forward tics : remove the special meaning of a character or string 21 Manage User Enviroment Setting up user enviroment by runing configuration files and set enviroment variables Manage Enviroment Variables 22 11
Configuration files Script files that get run at login, or logout Includes /etc/profile, ~/.bashrc, ~/.profile (on SuSe) ~/.bash_profile and ~/.bash_logout (on Fedora) The login shell reads these file the following in order : /etc/profile => ~/.profile (on SuSe) /etc/profile => ~/.bash_profile (on Fedora) /etc/profile file contains configuration parameters that applied all users (system-wide). ~/.profile file contains configuration parameters that applied a user itself The nonlogin shell reads ~/.bashrc /etc/bash.bashrc 23 Manage Enviroment Variables Enviroment Variables are used to define critical parameters for the shell run properly. Enviroment variables are frequently accessed and used by programs cd command check the value of $HOME when it runs 24 12
25 Manage Enviroment Variables Viewing a / all variable echo $PATH env more set more Setting the value of a variable variable=value PATH=$PATH:/var/opt/mydb Most of the value of ev assigned by the system There are many ev that you should not change 26 13
Manage Enviroment Variables Using export command to apply the new value of the variable to all shells in a sessions (including any subshells) PATH=$PATH:/var/opt/mydb echo $PATH export PATH 27 Manage Enviroment Variables To make persistence of the new value of the variable across system restarts, you need to edit one of bash configuration files. 28 14
Make a shell scripts cat myscript #!/bin/bash #a simple script echo hello exit 0 Shell scripts 29 Shell scripts Run a shell scripts : two ways $ bash myscript $ bash./myscript $ chmod 755 myscript $./myscript 30 15
script1 #!/bin/bash echo bạn muốn thêm ñường dẫn nào vào biến PATH? read NEWPATH echo thêm ñường dẫn $NEWPATH vào biến PATH PATH=$PATH:$NEWPATH export PATH echo biến môi trường hiện tại là echo $PATH exit 0 31 Script2 #!/bin/bash echo nhap so thu nhat read NUM1 echo nhap so thu hai read NUM2 SUM=$NUM1+$NUM2 echo tong hai so la $SUM exit 0 #!/bin/bash declare -i NUM1 declare -i NUM2 declare -i SUM echo nhap so thu nhat read NUM1 echo nhap so thu hai read NUM2 SUM=$NUM1+$NUM2 echo tong hai so la $SUM exit 0 32 16
33 17