Shell_01 Basic operations

Source: Internet
Author: User
Shell_01 what is the basic operation? what is Shellshell? shell is a command interpreter echo $ SHELL -- through the environment variable cat/etc/shells -- view the switch of the shellshell currently registered to the System 1. temporary switch #/bin/ksh2, fixed switch # vim/etc/passwd -- text... shell_01 what is the basic operation of Shell shell? shell is a command interpreter echo $ SHELL -- through the environment variable cat/etc/shells -- view the switch of the currently registered shellshell to the System 1. Temporary switch #/bin/ksh 2. fixed switch # vim/etc/passwd -- the last field in the file # chsh-s/sbin/nologin username # usermod-s/bin/bash username close the sub-shell #/bin/bash # bin/kshforks -- Copy the process Information, inherit the definition exec of the parent shell-enable the sub-shell through exec why the use of shell to solve repeated operations save time, improve work efficiency powerful functions do not need to compile the basic elements of the shell script #! <命令解释器> # -- Process control for shell commands (if/for/while/utile/case/function) man BASH_BUILTINS • shell history • Common Shell • One of the earliest Shell appearance of the Bourne Shell, the default Shell of most Unix systems, the rough cut lacks task control • nash --> initrd. img (init) • not a shell, which is not a SHELL and is a simple command interpreter designed as small as possible. It is mainly used to explain linuxrc or init in the initialization of ram disk. • csh • University of California Berkeley developed as part of its Unix implementation, currently, the most popular interactive Shell. It has many features, such as task control and history, but CShell is not suitable for writing scripts, there are also many potential defects • notorious errors • users cannot process file descriptors • users cannot put internal commands together in any proper way, you cannot use pipelines. • You cannot put stream control and commands together. • Some commands seem reasonable, but they cannot run the extension version of tcsh • C shell, remove the notorious error • ksh • Korn shell in C shell, which is compatible with the Bourne shell and has most of the features of C shell, as well as the ability to edit history, reuse old commands, and edit these commands before execution. It is more reliable than csh in terms of programming stability. • Bash • Bourne-again shell, developed by the Free Software Foundation and absorbing all of the above Shell features, widely used in open-source operating systems • internal and external commands • Shell does not need to start a separate process to run internal commands • Shell needs to create (fork) and execute (exec) A new sub-process to run external commands • Try to use internal commands to improve performance bash --> cd -- process for parsing internal commands bash --> forks --> bash (sub-process) --> ls -- process for parsing external commands # type ls -- external command ls is aliased to 'ls -- color = tty '# type cd -- internal command cd is a shell builtin file name • You you can name a file or directory with any visible or invisible characters that you think are appropriate, but don't worry about yourself. • security and convenience • case-sensitive characters • underscores • dots • comma • do not appear" Space "• extension • easy to classify, not required • The Command uses the file structure to determine its type (File)-type c file is of type c: B block (buffered) special c character (unbuffered) special d directory p named pipe (FIFO) f regular file l symbolic link; this is never true if the-L option or the-fol-low option is in effect, unless the symbolic link is broken. if you want to search for symbolic links when-L is in effect, use-xtype. s socket D door (Solari S) wildcard •? Any single character cannot match null • * any zero or multiple character groups (files starting with a vertex cannot be matched) • Any character between [AB] a or B • [a-z] a and z, including the endpoint • It is a Shell, not a wildcard processed by the command itself, the wildcard character after the command is replaced before the command is executed. • If you need to process the wildcard character with a command instead of a Shell, use "\" to escape the wildcard character, escape character # ll/dev/sd? Brw-r ----- 1 root disk 8, 0 03-14/dev/sda # ll/dev/sd *-rw-r -- 1 root 0 03-14/dev/sdbrw-r ----- 1 root disk 8, 0 03-14/dev/sdabrw-r ----- 1 root disk 8, 1 03-14/dev/sda1brw-r ----- 1 root disk 8, 2 03-14 09:03/dev/sda2brw-r ----- 1 root disk 8, 3 03-14 09:03/dev/sda3brw-r ----- 1 root disk 8, 4 03-14/dev/sda4brw-r ----- 1 root disk 8, 5 03-14/dev/sda5 # Ll/dev/sd? [1-5] brw-r ----- 1 root disk 8, 1 03-14/dev/sda1brw-r ----- 1 root disk 8, 2 03-14 09:03/dev/sda2brw-r ----- 1 root disk 8, 3 03-14 09:03/dev/sda3brw-r ----- 1 root disk 8, 4 03-14/dev/sda4brw-r ----- 1 root disk 8, 5 03-14 09:03/dev/sda5 # ll/dev/tty [1-5] [0-9] # ll/dev/sda [13] brw-r ----- 1 root disk 8, 1 03-14 09:03/dev/sda1brw-r ----- 1 root disk 8, 3 03-14 09:03/dev/sda3 # ll/dev/ Sda [1-3] brw-r ----- 1 root disk 8, 1 03-14 09:03/dev/sda1brw-r ----- 1 root disk 8, 2 03-14/dev/sda2brw-r ----- 1 root disk 8, 3 03-14/dev/sda3 escape # touch test \*\? \ [\]. Sh escape character # ls \>/root \> a \>/B bash quotes: double quotation marks "": other variable values can be referenced using the $ symbol, the content of quotation marks is treated as a whole. single quotation marks '': other variable values cannot be referenced. special symbols in shell are considered as common characters, the quotation marks are used as a whole to view the anti-apostrophes '': $ () will be preferentially executed during command execution. one line of command can be separated, during the execution, whether the last command is correctly executed is not considered. & you can separate a line of commands. during the execution, check whether the last command is correctly executed. |! Command History # echo "This system is" HOSTNAME "" This is system is HOSTNAME # echo "This system is" $ HISTNAME "" This is system is # echo "This system is" $ HOSTNAME "" This is system is your top8.example.com # echo 'This system is "$ HOSTNAME" 'This is system is "$ HOSTNAME" [root @ override TOP8 ~] # Echo $5.00.00 [root @ topics TOP8 ~] # Echo \ $5.00 $5.00 # host = 'ifconfig eth0 | grep 'inet addr '| cut-d: -f2 | cut-d ''-f1 '# host2 = $ (ifconfig eth0 | grep 'inet addr' | cut-d:-f2 | cut-D'-f1) $ (): Operator $ [] # a = $(1 + 10 )) # echo $ a # a = $[1 + 100] # echo $ a • shell configuration file (software + configuration file) • csh • $ HOME /. run $ HOME /. run $ HOME /. logout • bash: # user01 --> login --> bash -->/etc/profile --> $ HOME /. bash_profile --> $ HOME /. bashrc -->/ Etc/bashrc global configuration file:/etc/profile -- bash working environment configuration (environment variable)/etc/profile. d /*. sh --/etc/profile extension configuration file/etc/bashrc -- bash configuration file for each user: $ HOME /. bash_history -- store command history $ HOME /. bash_logout -- the script $ HOME/executed when the shell is logged out/exited /. bash_profile $ HOME /. edit the bashrc command ctrl + u -- delete the current cursor to the beginning of the line content ctrl + k -- delete the current cursor to the end of the line content ctrl + c -- interrupt ctrl + l -- clear screen ctrl + -- jump to HOMEcrtl + e at the beginning of the line -- navigate to the end of the line ENDctrl + r -- quickly search for history Command ctrl + z -- go to the background and run fg bgCtrl + d -- exit Shell, logout partition (Ctrl + p) display the previous command partition (Ctrl + n) display the next command alias -- query system all existing aliases alias = 'real name' unalias -- cancel the system alias unalias-a to delete all aliases temporarily: alias la = 'ls-a' fixed: you can write it to the following files. you need to use source to refresh the definition, or log out and log on to the user again:/etc/profile $ HOME /. bash_profile $ HOME /. bashrc/etc/profile. d /*. sh Command history: HISTSIZE = 1000 set the number of command history records history to query the history of all commands used by the current user (memory) cat $/HOME /. bash_history | grep xx (hard disk) history-w synchronize the commands in the memory to the hard disk ($/HOME /. bash_histo Ry) automatic synchronization: exit/log out of history-cecho ""> $ HOME/bash_history -- clear command history #:> $ HOME/bash_history call Command history :! 101 pass id !! History of calling the last command! Vim calls the history of the latest command starting with vim! $ Call the parameter command word in the last command history + [option] + parameter ls-l/ctrl + r to find the special symbol of the bash command history: <>>>>&> | standard input 0 standard output (screen) 1 error output (screen) 2 # ls-l/dev/std * lrwxrwxrwx. 1 root 15 Aug 3 09:34/dev/stderr->/proc/self/fd/2lrwxrwxrwx. 1 root 15 Aug 3 09:34/dev/stdin->/proc/self/fd/0lrwxrwxrwx. 1 root 15 Aug 3 09:34/dev/stdout->/proc/self/fd/1 < A3.txt < Gfdsjkgfdjgkldf> fdsfdsfds> fdsfdsf> END> -- output overwrite redirection> -- output append redirection 2> -- error overwrite output redirection 2> -- error append output redirection &> -- output correctness/error redirection & >>-- RHEL6 can achieve correct and error append redirection.> --- 2> & 1 rhel5 # cat/root/a2.txt/root &> a.txt --overwrite the attacker and the error to a.txt cat/root/a2.txt/root> a.txt 2> & 1 then [root @ I ~] # Ls/ttttt/etc/passwd-> 1.txt 2> 2.txt &> 3.txt | cat/etc/passwd | grep root | grep ^ root in the media transcoding queue give the second command for processing, similarly, bash shell's basic variables and the use of variable settings reference scope to delete variables • Set variables • VARNAME = VAR_VALUE -- it is recommended that letters in the variable name be capitalized -- the start character of the variable name cannot be data or special symbol variable name variable value • readonly VARNAME -- set the read-only variable, cannot be deleted, cannot be changed • read -- set variable • read-p through breakpoint "please input a value (number):" a read-t 5 "Please enter a value: "IP -- set the timeout value for the value assignment. # H = 192.168.0.253 # echo $ H192.168.0.253 # read-p "Please enter ip address:" H2Please enter ip address: 192.168.0.252 # echo $ H2192.168.0.252 • reference variable • echo $ VARNAME • echo $ {VARNAME} scope • Global variable (parent and child shell) • export •. or source call • local variable • variable in Program • variable in function delete variable: • unset VARNAME user-defined variable: host = 192.168.0.1ping $ host environment variable -- (user work environment) set -- can be checked, however, set can also display the user-defined variable env -- view the environment variable PATH • user command search path pwd the absolute PATH of the user's current directory HOME • contains the absolute PATH of the user's HOME directory SHE LL • absolute path for USER to log on to shell USER or LOGNAME • USER name, after logging on, the automatic setting does not change PS1 • configuration variable # PS1 = "[\ u @ \ H \ t \ w] \ $" PS2 • configuration variable PS1 at the main prompt and PS2 prompt variables, set the prompt format. PS1 is the environment variable used to set the first-level Shell prompt, also known as the main prompt string, that is, change: [root @ jselab ~] # The variables of PS1 are [\ u @ \ h \ W] \ $, \ u, \ h, \ W, and \ $, which indicate specific meanings. the \ u table shows the current user name, \ h indicates the host name, \ W indicates the current directory name, if it is a root user, \ $ indicates #, other users, \ $ indicates that PS2 is the environment variable used to set the second-level Shell prompt \ d displays the date \ H host name and domain name \ h host name \ s Shell in "week month day" format type name \ T display time in 12-hour format, the format is HH: MM: SS \ t. The Display time is in 24-hour format. the format is HH: MM: SS \ @. the display time is in 12-hour format: am/pm \ u current user name \ v bash Shell version \ V bash Shell version number and patch number \ w current working directory full path \ W current working directory name \ # Current command serial number \ $ if UID is 0, print #; otherwise, print $ built-in bash variable $ #: number of location variables in the command line $ *: content of all location variables $ @: content of all location parameters, $ * Is separated by any difference. we recommend that you use this method when using all location parameters. $?: The status returned after the previous command is executed. if the returned status is 0, the execution is normal. if the returned status is not 0, the execution is abnormal or an error $: Process ID of the current process $!: The last process number running in the background $0: the currently executed process/program name # ls &>/dev/null & echo YESYES # fdsfds &>/dev/null & echo YES -- no return value location variable: • $0 ~ $9, $ {10} $ * $ # Usage and cycle selection #! /Bin/bashecho "\ $0 = $0" echo "\ $1 = $1" echo "\ $2 = $2" echo "\ $3 = $3" echo "\ $4 = $4" echo "\ $5 = $5" echo "\ $6 = $6" echo "\ $7 = $7" echo "\ $8 = $8 "echo" \ $9 = $9 "echo" \ $10 =$ {10} "echo" \ $11 =$ {11} "echo" \ $ * = $ * "echo" \$ #=$ # "• $0 ~ $9 Location parameter variable • ${10 }~ $ {N} extended location parameter variable #. /aa. sh 1 2 3 4 5 6 7 8 $0 =. /aa. sh $1 = 1 $2 = 2 $3 = 3 $4 = 4 $5 = 5 $6 = 6 $7 = 7 $8 = 8 $9 = $10 = $11 = $ * = 1 2 3 4 5 6 7 8 $ # = 8 has a type variable • By default, bash sets the variable to a text value, when the arithmetic method is used, it is automatically converted to an integer. • The built-in command declare can modify the variable attribute. • The declare parameter-a treats the variable as an array.-f only uses the function name-F to display undefined values. function name-I regards the variable as an integer-r so that the variable read-only-x marks the variable and exports it through the environment-r: # h = 192.168.0.1 # declare-r h is equivalent to: # readonly h-x: # H = 192.168.0.100 # declare-x H # export -- query all global variables-I • example # VarA = 3 # varB = 7 # result_out = varA * varB # echo $ result_outvarA * varB # declare-I varC = 3 varD = 7 # declare-I declare_result_out # region = varC * varD # echo $ declare_result_out21 • array • An array is similar to an array in which values are stored. Each position is called an element. each element is accessed by a numerical subscript. • An array element can contain strings or numbers. • array subscript starts from 0. • The array can use declare a to display the declaration, there is no limit on the range of objects under the array, and continuous components are not required. You can access the elements in the array through $ {array [I. If no element is specified, it indicates the first element. @ Is the same as *, but it is enclosed by quotation marks and each parameter is returned in quotation marks. * is returned as a whole string. (1) name = (value1... valuen) at this time, the subscript starts from 0. you can also: names = ([2] = alice [0] = hatter [1] = duchess ). (2) name [index] = value # A = (a B c def) # echo $ {A [@]} a B c def # echo $ {A} a echo $ {A [*]} a B c def for I in "$ {[ @]} "; do echo $ I done: a B c def for I in "$ {A [*]}"; do echo $ I done: a B c def // clear the variable $ unset A $ echo $ {A [@]} $ A = B = C unset $ A the actually canceled variable is B rather than/ /clear variables, changing the value to null $ A = $ echo $ {A [@]} $ script execution: 1. the script has no execution permission: #/bin/sh/xx. sh -- start a new sub-shell to execute the script. the script does not require executable permissions #/bin/bash/xx. sh # source/xx. sh -- force the script to be executed in the current shell #. /xx. sh 2. the script has executable permissions # chmod + x/xx. sh #/xx. sh
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.