Getting started with SHELL programming

Source: Internet
Author: User

Getting started with SHELL programming-Shell basics-Process Control syntax-Introduction of Common commands-demonstration of examples-Tips summary} troubleshooting of interface failures} faster command line interface} better management} bsh (Bourne Shell )} ksh (Korn Shell)} csh (C Shell)} The most common Shell is sh, and most Unix systems connect sh to bsh; IBM connects sh to the ksh} Shell program, which is a combination of a series of Unix commands, equivalent to the batch processing commands in the DOS system. Unix commands can be internal commands, compiled high-level language programs, or even other Shell programs.} Shell programs Support Branch and loop structures, signal processing is supported.} The Shell program can add comments starting with "#" and ending at the end of the line.} How can I know what Shell is used to execute my Shell script? Refer to the following three principles: if the first character of the script file is not "#", start bsh to execute this script. If the first character of the script file is, but the second character is not "! ", Start csh to execute this script. If the first two characters of the script file are "#!", It must be followed by the full path of the Shell. Start the specified Shell to execute this script, for example :#! The syntax of the if statement in/usr/bin/ksh} Shell is as follows:} if command 1 # if command 1 is successfully executed (0 is returned )} then # execute the list1 command list} list1 # If the execution fails (a non-0 value is returned )} elif command 2 # Execute Command 2} then # If command 2 is executed successfully} list2 # execute list2 command list} else # If command 2 is not executed successfully} list3 # execute list3 command list} fi} if statement ended with fi} Syntax of case statement in Shell :} case word in # If word matches pattern1,} pattern1) # execute list1 command list} list1 # If word matches pattern2,}; # execute list2 command list} pattern2) #............} List2 };;}............} The esac} case statement ends with esac, and the two ";" after each branch is required.} the pattern in the case statement can use the following structure} "or, for example, "a | B | c" matches a, B, or c} [range], for example, "[0-9]" matches a single number} "?" Match any single character} "*" to match any zero or multiple characters} Use "*", equivalent to the C language default branch} and above structures can be used in combination, such :} "[0-9] | 0x [0-9A-Fa-f]"} the syntax of the while loop in Shell is as follows :} while command} do} list} done # The loop statement must end with done} 1. Execute command} 2. If the execution is successful (the return value is 0 ),} execute the list command list} 3. Repeat 1 until the command fails (return a non-zero value)} the syntax of the until loop in Shell is as follows :} until command} do} list} done # The loop statement must end with done} 1. Execute command} 2. If the execution fails (the returned value is not 0 ),} execute the list command list} 3. Repeat 1 until the command is successfully executed (0 is returned)} the syntax of the for Loop in Shell is as follows:} for name in word1 word2... wo RdN} do} list} done # The loop statement must end with done} name as the variable name, And word1 to wordN are word lists separated by spaces} for each execution of the loop, variable name is set to the next word in the list} for Loop is often used to process the file set} for example: Modify the file names of all files in a directory, add ". bak "} for file in $ HOME/*} do} mv $ file $ {file }. bak} done} special characters in Shell }*? [] '"\ $; & () | ^ <> Line feed space tab} single quotation mark"' ": All special characters in single quotation marks lose their special meaning, including linefeed} "'" must appear in pairs. "'" Cannot be embedded between a pair of "'". It cannot be escaped with "\".} "'" is a set of commands. Shell replaces the back quotes with the output of the command. The processing of special characters in double quotation marks is basically the same as that in double quotation marks.} double quotation marks "": Most characters in double quotation marks lose special meanings, except for the following characters: replacement $ used to replace variable quote '. The quotation mark is used to replace variable \ $ with a dollar sign without special meaning, without special meanings, escape \ "uses the embedded double quotation mark escape \ uses the embedded backslash to escape other \ characters as common characters} The definition variable name = value cannot contain blank variables. the name can only contain letters, numbers, or underscores, and can only start with a letter or underline} When referencing a variable, add a $ character} special variable before the variable name =$ $ the process ID of the currently executing Shell =$ 1... $9 command line parameter $ @ or $ * command line parameter environment variable: A sub-process can inherit the environment variable of the parent process. In sh, use the export command to set a variable as an environment variable} In ksh, $ {# name} is the length of the variable name value} command> filename redirects the standard output to a new file} command> filename redirects the standard output to a file (append)} command 1> fielname redirects the standard output to a file} command> filename 2> & 1 redirects the standard output and standard error together to a file} command 2> filename redirects the standard error redirect to a file} command 2> filename redirects the standard output to a file (append )} command> filename 2> & 1 redirects the standard output and standard error together to a file (append)} command <filename> filename2 uses the command file as the standard input, using the filename2 file as the standard output} command <filename c o m a n d command using the f I l e n a m e file as the standard input} command <delimiter from the standard read in input, until the d e l I m I t e r separator} command <& m uses the file descriptor m as the standard input} command> & m redirects the standard output to the file descriptor m} command <&-Disable Standard Input} 1. Specify the debug mode execution in the command line.} specify the SHELL-x program name, example:} sh-x update_iuser214.sh} 2. execute SHELL} foreground execution:}> sh program name}> program name} background execution:}> sh program name &}> program name &} searches for and edits the text. It is mainly used for searching, replacing, and inserting in specified mode, delete file content} directly type the command} Sed [-option] command_line filename} to insert the sed command into the script file, and then call sed} Sed [-option]-f program_file filename} option :} e use the command line as the sed edit command} n do not print command meaning P print match line = display text line number a \ add new text information after locate line number I \ Insert before locate line number new text information d Delete positioning line c \ replace positioning text with new text s use replacement mode r read text from a file w write text to a file Sed print file second line sed- n'2pa' filename Sed print the line sed-n Matching test '/test/P' filename Sed to replace the text sed's/night/NIGHT 'filename sed's/night/NIGHT /g'filename Delete the row sed that only contains empty rows '/^ [] * $ d' filename Awk is a program language, modify, compare, extract, and other processing formats of documents: awk [-F char] 'COMMAND _ line' filename-F char determine interval to delete even row awk' {if (NR % 2 = 1) print $0} 'filename get the maximum number of fields in the input line awk' {if (NF> max) max = NF} END {print max} 'filename outputs the row awk with more than 80 characters' {length ($0)> 80} 'good filename comments can increase code readability. Script Execution and definition separation improve code scalability} understand requirements, what kind of interface should be provided externally} break down requirements into small steps} Add necessary comments} differentiate the use of variables, use of global variables and local variables} write pseudocode, extract common parts using function implementations} gradually implement small requirements, and gradually debug} use variables as much as possible to provide scalability and ensure subsequent maintenance of scripts} reduce frequent external interactions

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.