Shell Learning (i)

Source: Internet
Author: User
Tags stdin

One, free of dense interaction

--stdin

1. Reading strings from standard input

such as: passwd--stdin heruguo246

[Email protected] mnt]# passwd--stdin heruiguo246
Changing password for user heruiguo246.
123456 ---Input modified heruiguo246 user password is 123456
Passwd:all authentication tokens updated successfully.

2, can be from the keyboard, can also be given from another command

such as: Echo 1234567 |passwd--stdin herugiu246

[Email protected] mnt]# echo 1234567 | passwd--stdin heruiguo246
Changing password for user heruiguo246.
Passwd:all authentication tokens updated successfully.

This time there is no manual password entered, complete script implementation.

Second, ignore irrelevant output

Black hole Equipment/dev/null

A one-way file that can only be written and unreadable, and the data stored in it will be lost.

Usage: executable statement &>/dev/null

echo 1234567|passwd--stdin heruiguo246 &>/dev/null

[Email protected] mnt]# echo 1234567|passwd--stdin heruiguo246 &>/dev/null
[Email protected] mnt]#

Note that there are no spaces between:& and > and > and/dev/null, or error

Third, record error message

Usage: Executable statement 2>/path/log file name

such as: sh/mnt/adduser.sh 2>/mnt/adderror.log

Iv. Logical Segmentation

1, | | The logical relationship is "or", and any command that executes succeeds is expected, and the subsequent command executes only if the preceding command fails.

such as: ID Test | | Useradd test-Indicates that a user is created when the test user does not exist.

V. The difference between double and single quotation marks

Double quotes:

(1) in double quotes, you can use the $ extension to represent variables, such as:

[Email protected] mnt]# a=5
[[email protected] mnt]# echo "Your value is: $a"
Your value is: 5

(2) When a special character appears, you can use \ to indicate escape, \ t for tab, \ n for line break, such as:

[Email protected] mnt]# a= "a\tb\tc\td\ne\tf\tg\th"
[Email protected] mnt]#
[[email protected] mnt]# ECHO-E $a-e parameter means parsing special escape characters
A B c D
E F g H

(3) When the variable value does not include spaces, tabs, double quotes are usually omitted, such as:

[Email protected] mnt]# a=centos6.5
[Email protected] mnt]# b= $a server
-bash:server:command not found
[Email protected] mnt]# b= "$a server"
[Email protected] mnt]# echo $b
centos6.5 Server

Single quotes:

(1) All strings are treated as characters themselves (no special) such as:

[Email protected] mnt]# A=centos
[Email protected] mnt]# echo ' $a '
$a
[Email protected] mnt]#

(2) do not allow \ Escape

Vi. usage of the read fetch value

Basic format

Read variable name

READ-P "hint Information" variable name

[[email protected] mnt]# Read name
123
[Email protected] mnt]# echo $name
123
[Email protected] mnt]#
[[email protected] mnt]# read-p "Please enter your username:" Name
Please enter user name: Xiaoming
[Email protected] mnt]# echo $name
Xiaoming

Silent Value plus-S does not appear on the screen when you enter a password

[Email protected] mnt]# read-s-P "Please enter your password:" passwd
Please enter your password:
[Email protected] mnt]# echo $passwd
123456

Shell Learning (i)

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.