Shell Script Basics

Source: Internet
Author: User

# # # #shell脚本基础 #####

1. Basic Concepts

Purpose: Many instructions are assembled together, circulated, judged

format: Name . SH (with color)

operating Environment : #! /bin/bash

Run File: 1>chmod +x file Direct plus absolute address to make file executable

2> sh file

Reference:

1> Weak reference: "" preserves literal values of all characters in a string, except for $ ' \

2> Strong reference: ' preserves literal values of all characters in a string, and disables all extensions

3> escape: \ Preserves the literal value of the next character,

Bash-x file.sh #查看过程 \

# + + indicates the command executed first

# + indicates the command executed after

# The final result

2. Specific content

###{} reference variable eg

Dd=q

Pp=k

echo ${dd}_$pp Results: Q_k

# # #备份/All things in the mail group

~~~~~~

#!/bin/bash

time=$ (Date +%h:%m:%s)

Mkdir/mnt/mail. $TIME

Find/mnt-group mail-exec CP-RP {}/mnt/mail. $TIME/\;

~~~~~

# # # #新建用户 # #

#!/bin/bash

xuhao= ' wc-l/mnt/username |cut-d ""-F 1 '

For NUM in $ (SEQ 1 1 $XUHAO)

Do

Username= ' Sed-n ${num}p/mnt/username '

passwd= ' Sed-n ${num}p/mnt/passwd '

Useradd $USERNAME

echo $PASSWD | passwd--stdin $USERNAME

Done

~

~

# # #ping数台机器看能否通 # #

~~~~~

For IP in {1..35}

Do

Ping-c1-w1 172.25.254. $IP &>/dev/null && echo 172.25.254. $IP OK | | echo 172.25.254. $IP Error

Done

~~~~

# # #ping任意一台ip # #

~ ~ ~

#!/bin/bash

PING-C1-W1 172.25.254.$1 &>/dev/null && echo 172.25.254.$1 is OK | | Echo 172.25.254.$1 is error

~ ~ ~

# # #ping是否有ip

# # #其他 # # #

Read ""-s (not displayed directly to the CPU) interactive variable

echo $? 0 for normal exit value # Indicates whether the previous command was output correctly

Exit 1 Set the exit value to 1

Test= ""

# # #建立用户 # # #

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8E/67/wKiom1i_jTuyNhmFAADkqMMaJhE293.png-wh_500x0-wm_ 3-wmp_4-s_1153996144.png "title=" screenshot from 2017-03-07 21-29-22.png "alt=" Wkiom1i_ Jtuynhmfaadkqmmajhe293.png-wh_50 "/>

# # #if命令 (comparison && can judge Eslf indefinitely) # # #

if + condition

Then + command

else does not meet other commands

Fi (end)

determine if the value of a variable is null "-Z"

determine If the ping is available and determine if there is an IP

~ ~

#!/bin/bash

If

[-Z $*]

Then

echo "Error:you not give me IPAddress"

Else

PING-C1 (one row only)-w1 (which executes 1s) $* () executes the first parameter followed by &>/dev/nul && (correct) echo $* is up | | Echo $* is down

Fi (end)

~ ~

# # # #case # #

and if function basically the same, but the judgment process at the same time judgment, high efficiency

Eg: when the input is Westos output Linux input Linux, the output westos

~~~~~~

Case $ in

Westos)

Echo Linux

;;

Linux

Echo Westos

;;

*)

echo Error

Esac

~ ~ ~

# # # #expect ######

extension:send: Sends a string to the process;

Expect: Accepts a string from a process;

Spawn: Start a new process;

Interact: Allow user interaction;

Yum Install expect

Expect EOF (end)

# # # # #环境变量 # #

User environment variable settings:vim/root/bash_profile--->export PATH export a=1---> Detection: Echo A

system environment variable settings:vim/etc/profile---export path= $PATH:/MNT export a=2

#alias

Eg:alias Xie=vim can open vim temporary method ile Xie method

Long-term setup: vim/etc/bashrc-->alias xi= ' vim '-->SOURCE/ETC/BASHRC

# # # #脚本简写 (function) # # #

#! /bin/bash

Ifcmd () {

If

[$] #表示函数IFCMD the first and second digits behind

Then

Echo $ is $

Fi

}

Ifcmd-f $ file

Ifcmd-d $ direcotry

Ifcmd-b Block

# # # #end #####


Shell Script Basics

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.