Shell aliases, pipelines, user profiles, variables, read

Source: Internet
Author: User
Tags aliases

I. Shell Basics

Cat/etc/shells the shell that the current operating system can use


What is a shell?

A text file that implements a function that has permission to execute


Command-line Interpreter


Built-in commands (commands provided internally by the system)

External command (command to install the corresponding package)

Type cd//See what type of command the CD belongs to



Support shortcut keys

Support for Tab completion

History command History-c//Clear History command

-w//immediately save the newly executed command in. bash_history



Alias

How to view aliases when a system has an alias command

How to customize the alias command How to remove a custom alias command alias grep= ' grep--color ' unalias grep

How to permanently define alias commands/ETC/BASHRC ~/BASHRC

Defined alias command All users can use the/ETC/BASHRC

How to set an alias command for a user ~/.BASHRC



Historical command

grep 1000/etc/profile Save 1000 commands

Source/etc/profile will take effect immediately.

Cat ~/.bash_history stored history command reboot system in effect



Pipeline |


Command 1 | Command 2 | Command 3


redirect

> Ping-c 3 loalhost >/dev/null

>> ping-c 3 localhost >>/tmp/err.txt


< Mail-s "First mail '-U root </etc/error.txt

<<


2> redirect error output, overwriting cd/etc/2>/tmp/err.txxt

2>> Append


&>/dev/null Redirect to null, whether right or wrong

&>> Append


"The function of executing the command itself echo ' date +%m '

; Sequential execution of commands, previous command errors do not affect subsequent command execution cd;ls;cd/etc/

&& previous command execution succeeds after command to execute cd/asf &>/tmp/err.txt && ls

|| The previous command failed to execute the following command cd/asf &>/dev/null/| | echo "No file"


Check that the host is not online

Pinc-c 2 127.0.0.1 &>/dev/null &7 echo "Yes" | | echo "No"



Shell variables

The value of a variable with a fixed string of characters

When writing a script, change data is represented by variables.


Shell Variable Type

1, custom variables: The program according to the needs of the script to define variables, when defining variables, to follow the rules of the use of variables


Define variables:

Variable name = variable Value


Naming rules for variable names:


1. Alphanumeric underlines can be used, but cannot start with a number and a pure number

2. Assign two values to the same variable and the last value to take effect

3. When assigning a value to a variable, there must be no spaces on either side

4. Case-sensitive


Set displays all variables already defined within the current system

Set | grep name


ENV displays only the environment variables that are already in the system


Define variables (the type of the default variable is character type)

Variable name = variable Value

Name=wsyht

Age=24



Use variable $ variable name $name


The value of the output variable echo $ variable name echo $name $age


Undo variable unset Variable name unset name


2, System environment variables: When the system starts, load the system configuration file defined variables,

Variable names and variable values are system settings, variable names are usually defined in uppercase letters, and the value of the variable is specified, usually

It is not recommended to modify the value of the system environment variable


3, pre-defined variables: The current script uses a predefined variable in the shell, the variable name is fixed, the value of the variable is not modified


Predefined variables: Pre-defined variables, variable names and values are fixed.

$? Return status of the previous command execution result 0 successful execution

Non-0 execution failure


The script name and path of the currently executing script


$! The last running process in the current terminal background


$$ PID number of the current terminal running process//TAP PS to see the process PID number


Number of $# script positional parameters


$* the values of all positional parameters of the script



4. Positional variables: give self-passed values when executing a script or calling a function

$ ... ... $n

${10} ... ${n}


DirName #获取脚本路径

BaseName #获取脚本名


Positional variables:

$1~ $9 ${Number}

The first positional variable of a script

The second position variable of the $ $ script

The third position variable of the $ A script

The position variable is represented when the number of positional variables in the script is greater than 9

${10} The 10th positional variable of a script

${11} The 11th positional variable of a script


#!/bin/bash

Echo $#

echo $ $4 $ $6 $7 $8 $9 ${10}


The life cycle of a variable

Variables defined in the script, only valid during script execution



The scope of the variable: By default only in the current shell can be used,

To define a variable to be used in all shells, define the variable as a global variable,

Format

1.

Export variable name = value


2.

Variable name = value

Export variable Name


variable is defined as a global variable, this variable can be used in the current shell and the current Shell's

Child shell Use


Character terminal, the files that are loaded when a user opens a new terminal

System-level configuration files (valid for all users logged into the system)

1./etc/profile

2./etc/bashrc

/etc/profile.d/


User-level profiles (valid only for corresponding users)

3.~/.bashrc

4.~/.bash_profile


The files that are loaded by the user each time a new terminal is opened under the graph run level

1,/ETC/BASHRC

2, ~/.BASHRC


SOURCE/ETC/BASHRC//Immediate effect




Writing shell scripts

Vim test.sh

#!/bin/bash//Specify the execution environment of the script, default is/bin/bash, can omit, generally do not omit

#注释信息不会在执行脚本时显示出来, the programmer gives self-view

The functional body of the script


The idea of writing a script:

1, clear the script to implement the function

2, which data is changed, the change of the data with variables to represent

3. What kind of process control is used

4. Which system commands are used


Run the script

1.sh xxx.sh or Bash xxx.sh

2. Plus x permissions./xxx.sh or Path/xxx.sh

3.source xxx.sh or. xxx.sh


' Prohibit references to other variable values, $ as ordinary characters//can not refer to $ variables, as ordinary characters treat

"" Allow referencing of other variable values through the $ symbol//can reference $ variable

"Output the result of the command execution to the variable//hold command, output to the variable


Command | awk ' {print} '

Head-1/etc/passwd | Awk-f ":" ' {print $} '

free-m | grep Mem | awk ' {print $} '


Read receive user input data from the health disk


Read option variable name

-P "Prompt for Output"

-T-n waits for user input time-out


Receive user input data from the health disk can be read or $# (positional parameters)


Example: receiving data with positional parameters

#!/bin/bash

#read-P "Please enter the IP address to be detected:" IPAddr


[$#-eq 0] && exit


# [-Z $ipaddr] && exit


echo $

Ping-c 3 &>/dev/null && echo "is Online" | | echo "Not online"


Example 2: receiving data with read

#!/bin/bash

Read class

Read-p "Please enter your name"-T-Name

Read-p "Please enter your age"-t

echo $class $name $age



This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1786322

Shell aliases, pipelines, user profiles, variables, read

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.