Brief Introduction to the Collection programming language of Linux shell commands

Source: Internet
Author: User
Tags linux shell commands

There are many things worth learning about in Linux. Here we mainly introduce Linux shell, including Fedora Shell programming. In most cases, you can use test commands to test conditions in Linux. For example, you can compare strings, determine whether a file exists, and whether the file is readable...

In Linux, Linux shell must be a flexible system tool. Shell is not only a collection of commands, but also a great programming language. You can use Linux shell to automate a large number of tasks. shell is especially good at system management tasks, especially for tasks that are easier to use, maintainability, and portability than efficiency.

Next, let's take a look at how Linux shell works:

Create a script
◆ There are many different shells in Linux, but we usually use bash (bourne again shell) for Linux shell programming, because bash is free and easy to use.
◆ #! /Bin/sh
◆ # Assign values to variables:
◆ A = "hello world"
◆ # Print the content of variable a now:
◆ Echo "A is :"
◆ Echo $
Enter the preceding content in your editor and save it as a file first. Then run chmod + x first
◆ Make it executable, and input./first to execute the script.
◆ This script will output:
◆ A is:
◆ Hello world

Below are some common command syntaxes and functions
Echo "some text": print the text on the screen
Ls: file list
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.
Cp sourcefile destfile: file copy
Mv oldname newname: rename a file or move a file
Rm file: delete an object
Grep 'pattern' file: searches for strings in a file, for example, grep 'searchstring' file.txt.
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
Cat file.txt: output file content to the screen of the standard output device)
File somefile: get the file type
Read var: prompt the user to input and assign the input value to the variable.
Sort file.txt: sorts the rows in the file.txt file.
Uniq: Delete the columns in a text file, for example, sort file.txt | uniq
Expr: perform mathematical operations Example: add 2 and 3 expr 2 "+" 3
Find: search for a file. For example, search for find.-name filename-print based on the file name.
Tee: outputs data to the standard output device (screen) and files such as: somecommand | tee outfile
Basename file: returns a file name that does not contain a path, for example, basename/bin/tux.
Dirname file: the path of the returned file. For example, dirname/bin/tux will return/bin.
Head file: prints the first few lines of a text file.
Tail file: number of rows at the end of a text file

Sed: Sed is a basic search replacement program. You can read text from a standard input, such as a command pipeline, and output the result to the standard output screen ). This command uses the regular expression see reference) for search. Do not confuse with wildcards in shell. For example, replace Linuxfocus with LinuxFocus: cat text. file | sed's/linuxfocus/LinuxFocus/'> newtext. file
Awk: awk is used to extract fields from text files. By default, the field delimiter is a space. You can use-F to specify other separators. Cat file.txt | awk-F, '{print $1 "," $3}', which is used here as a field delimiter and prints both the First and Third fields. If the file contains the following content: Adam Bor, 34, IndiaKerry Miller, 22, and USA, the output result is Adam Bor, IndiaKerry Miller, USA.

Concept: pipelines, redirection, and backtick

These are not system commands, but they are really important.
The pipeline (|) uses the output of a command as the input of another command.
Grep "hello" file.txt | wc-l
Search for a row containing "hello" in file.txt and calculate the number of rows.
Here, the grep command output serves as the wc command input. Of course, you can use multiple commands.

Redirection: output the command result to a file instead of a standard output screen ). Write the file and overwrite the old file to the end of the file to keep the content of the old file.

Backlash

You can use a backslash to output a command as a command line parameter of another command.
Command:
Find.-mtime-1-type f-print
Used to search for files modified in the past 24 hours-mtime-2 indicates the past 48 hours. If you want to pack all the searched files, you can use the following script:
#! /Bin/sh
# The ticks are backticks (') not normal quotes ('):
Tar-zcvf lastmod.tar.gz 'Find.-mtime-1-type f-print'

Process Control

If the expression "if" is true, the part after then is executed:
If...; then
....
Elif...; then
....
Else
....
Fi
In most cases, you can use test commands to test conditions in Linux. For example, you can compare strings, determine whether a file exists, and whether the file is readable...
"[]" Is usually used to represent a conditional test. Note that spaces are important. Make sure that the square brackets have spaces.
[-F "somefile"]: determines whether it is a file.
[-X "/bin/ls"]: determines whether/bin/ls exists and has the executable permission.
[-N "$ var"]: determines whether the $ var variable has a value.
["$ A" = "$ B"]: determines whether $ a and $ B are equal.

Run man test to view all types of test expressions that can be compared and judged.
Directly execute the following script:
#! /Bin/sh
If ["$ SHELL" = "/bin/bash"]; then
Echo "your login shell is the bash (bourne again shell )"
Else
Echo "your login shell is not bash but $ SHELL"
Fi
The variable Linux SHELL contains the name of the logon shell. We compared it with/bin/bash.

  1. Linux shell
  2. Teach you three methods of automatic Linux Shell Interaction
  3. Change the output of linux shell
  4. How to use Linux Shell Variables
  5. Use ftp to automatically download and upload files in Linux shell

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.