Shell script Learning (1): Shell variables and some common commands

Source: Internet
Author: User
Shell script Learning (1): Shell variables and some common commands

Shell allows you to create variables to store data, but does not support basic data type variables (such as int, float .....), shell interprets any value assigned to a variable as a string of characters. The naming rules are the same as those of C ++.

I. Shell has the following four variables: 1. A good understanding of user-defined variables is User-Defined variables, such as X, Y, and count (usually all variables in shell are capitalized. location variable (that is, the command line parameter)

(1) $0: indicates the script file name.

(2) $1, $2... $-9 command line parameters

(3) $ #: Number of command line parameters, less than one in C

(4) $ @: that is, "$1, $2... $9"

(5) $? : The exit status of the previous command. The normal exit value is 0, and the exceptional exit value is 1 or 2.

(6) $ *: It seems the same as $...

(7) $: ID of the current process

3. predefined variables (default variables) 4. Environment Variables 2. Common shell commands

(1) echo "some text": print the text on the screen.

(2) unset: Delete the value assignment of a variable.

(3) ls: file list

(4) WC-l filewc-W filewc-C file: calculate the number of file lines. Calculate the number of words in the file. Calculate the number of characters in the file.

(5) CP sourcefile destfile: file copy

(6) mV oldname newname: rename a file or move a file

(7) RM file: delete an object

(8) grep 'pattern' file: searches for strings in a file, such as grep 'searchstring' file.txt.

(9) Cut-B colnum file: specify the content range of the file to be displayed, and output them to the standard output device, for example: output 5th to 9th characters in each line cut-b5-9 file.txt do not confuse with Cat command, this is two completely different commands

(10) Cat file.txt: output file content to the standard output device (screen)

(11) file somefile: get the file type

(12) read var: Read a row from the standard input and assign the input value to the variable VAR.

(13) Sort file.txt: sorts the rows in the file.txt file.

(14) uniq: Delete the columns in a text file, for example, sort file.txt | uniq

(15) expr: perform mathematical operations example: Add 2 and 3 expr 2 "+" 3

(16) Find: search for a file. For example, search for find.-Name filename-print based on the file name.

(17) tee: outputs data to the standard output device (screen) and files such as: somecommand | tee OUTFILE

(18) basename file: returns the file name that does not contain the path. For example, basename/bin/tux will return tux.

(19) dirname file: Path of the returned file, for example, dirname/bin/tux,/bin is returned.

(20) Head file: print the beginning of a text file.

(21) tail file: number of rows at the end of the text file

(22) test: Test three types of objects (string, integer, and file attribute). 0 is returned for true, and 1 is returned for false.

(23) Sed: SED is a basic search replacement program. You can read text from standard input (such as command pipeline) and

The result is output to the standard output (screen ). This command uses a regular expression (see references) for search. Do not confuse with wildcards in shell. For example, replace linuxfocus with linuxfocus: CAT text. File | SED's/linuxfocus/'> newtext. fileawk: awk to extract fields from text files. By default, the field delimiter is a space. You can use-F to specify other separators.



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.