Shell-related commands

Source: Internet
Author: User

What is 1.shell?
From the user perspective: The shell is a bridge between users and Linux
From the programmer's Point of view: The shell itself is a program written in C language
The shell acts as a translator, translating the commands entered by the user into instructions that Linux can recognize.
The shell is a command interpreter.
The shell is a programming language.
The shell is the protection shell of the kernel.

2.shell type
Cat/etc/shells viewing the shell on the current system
echo $SHELL View the currently used SHELL

3.bash Advantages:
1. Command editing
Memory used commands, the upper and lower keys can find the previous instructions

History View Historical Instructions

Vim. Bash_history history instruction saved in this file, 2000 records are saved by default

Vim. BASHRC-histfilesize=200 can modify the maximum number of saved records

2. Full complement function (table)
Command completion
File name completion

3. Command aliases (alias) setting function
Ls-al->LM
Set alias: Alias lm= ' Ls-al '
Alias rm= ' Rm-i '

4. Job control, foreground background control &
CTRL + Z let the running program switch to the background run
Jobs view the jobs that are running
FG 1/2/3 cut back to the foreground run

Using pre-background controls can make your work smoother

5.shell Script
DOS has a bunch of instructions written together called a batch file (. bat)
Shell script is more powerful in Linux

4. Enter commands in Bash
command [-options] Parameter1,parameter2,...
Directive option Parameters
1. Directives and options, parameters with a space, no matter how empty the shell is considered a grid of
2. If the instruction is long, you can use the \ character to make the instruction continuous to the next line
3. Generally, the option is-option, sometimes a full name is required-option
LS --help

5. Piping (|)
commonly used to connect processes
to enter the output of the previous command as input to the next command
PS | sort | more

6. REDIRECT
Simply put the current data to another place
greater than > is the result of directing the output to the file after >
1. If the file does not exist,
2 is created automatically. If present, empty, then write the data to

Standard input code 0stdin<
Standard output code 1STDOUT1>
standard error code 2STDERR2>

Basic instruction format:
instruction > Device/file
Directive 1> device/File
Instruction 2> device/file
Directive < device/file

redirect normal output and error output to the same file:
Ls-l file 1> out.txt 2>&1

Input redirection <:
simply means that the data originally required to be entered by the keyboard is read through the file
more < Err.txt
WC err.txt
Output lines, number of words, bytes

ls qqqqq 2>/dev/nu ll (empty device)

----------------------------
Shell syntax:
Variable
Condition
Program Control
Function
Variable:
1. Environment variable
General refers to some parameters that are used in the operating system to specify the operating environment of the system
Path= $PATH:.
Ld_library_path= $LD _library_path:/usr/local/lib
HOME
PWD
LANG
Oldpwd (CD-)
Export ps1= ' [\[email  protected]\h:]$ '

1. Permanent
is declared with the Export command and saved to the configuration file
2. Temporary

2. Custom variable
1. The simplest and most common example is the path name
Export mydir=/mnt/hgfs/Chinese path
CD $myDir
2. Another place where custom variables are required is in the script file (refer to # define, modify maintenance more convenient)
3. Special variables
$? Indicates the exit code (return value) of the previous command

rules for setting variables:
1. The variable and variable contents are connected with "="
2. The equal sign cannot be directly connected to the whitespace
3. Variable names can only be letters and numbers, where numbers cannot be the opening character
4. If there is a space, use double or single quotation marks to combine the contents of the variable
5. You can add a $ to the variable name by Symbol to access its contents
6. Usually uppercase characters are system preset variables, custom variables can use lowercase characters, convenient to judge

Condition:
Test condition
or [condition]
Test string:
string1 = string2 Same
String1! = string2 different
-n string if the string is not empty, the result is true
-zstring if the string is empty, the result is true
Test arithmetic expression:
Expression1-eq Expression2 equals
-nq not equal to
-gt greater than (greater)
-lt less Than (little)
-ge greater than equals
-le less than equals
!expression if the expression is true, the result is, False and vice versa
Test file condition:
-D files are directories true
-f file is normal file True
-e file file exists True
-R file files are readable then true
-W file file writable True
-X File executable is true

Program:
1.if statements
If condition
Then
Statement
Elif conditions
Then
Statement
Else
Statement
Fi
Note: if test conditions have spaces on both sides of the equal sign
If you use [] to test the condition [] there is also a space between the conditions
First Script program:
1.#!/bin/bash tells the system to execute my script file with the program behind #!
2. The other line's # begins with a comment
3.echo for output (equivalent to printf), read for input, equivalent to scanf
4. At the end of the script, exit 0
Execution of the script program:
1./bin/bash xxx.sh
2.chmod +x xxx.sh
./xxx.sh

2.for statements
For variable in value 1, value 2, ...
Do
Statement
Done

' Instruction ': Gets the execution result of the instruction
$ (Directive): Gets the execution result of the instruction

3.while statements
While condition
Do
Statement
Done


#!/bin/bash

#这是我的第一个脚本, which is used to output Hello Wrold

var= "Hello World"
Echo $var

Exit 0


#!/bin/bash

#这是用if语句来测试字符串
echo "Are you hungry?" Please answer yes or NO "

Read Var
Echo $var
If ["$var" = "YES"]
Then
Echo, "We'll eat when we get there."
elif [$var = "NO"]
Then
Echo, "Let's play it again."
Else
echo "Input not correct, please re-enter"
Fi

Exit 0

#!/bin/bash

#这是用if来测文件

Read Var
If [-F $var]
Then
echo "This is a file"
elif [-D $var]
Then
echo "This is a directory"

Else
Echo does not support this format
Fi
Exit 0


#!/bin/bash


#这是测试for循环

For var in Apple pear grape
Do
Echo $var
Done

Exit 0


#!/bin/bash


#打印当前目录下以. Sh End of File

#for var in ' ls *.sh ' # ' command ': Indicates the result of getting instruction execution
For Var in $ (LS *.sh)
Do
Echo $var
Done

Exit 0


#!/bin/bash
#这是while语句示例
#密码检查

echo "Please enter your password:"
Read Var
while [$var! = "123456"]; Do

echo "Password is incorrect, please re-enter"
Read Var

Done
echo "Password correct, login successful"
Exit 0

Homework:
Summation 1-100
Learn the second way to do a for statement

#! /bin/bash
I=1
J=0
While:
Do
j=$ ((j + i))
((i = =)) && break
((i++))
Done
Echo $j



For loop version

#! /bin/bash
J=0

for (I=1; i<=100; i++)
Do
j=$ ((j + i))
Done
Echo $j





#!/bin/sh
S=0
For i in ' SEQ 1 100 '
Do
s= ' expr $s + $i '
#s =$ (expr $s + $i)
Done
Echo $s

Shell-related commands

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.