A script that takes you through the Shell basics

Source: Internet
Author: User

For example, the need to test a batch of IP in the work is still blocked, you need to write a simple script ping test.

We need two machines,

Virtual Host: centos6.7 system

Local linux:centos6.5

"Virtual Host"

Remote to the host, add IP (Learn the script, let me show off a bit)

You can use Xshell to do the following

Of course if you save with a Windows text file, you might add a failure because: Windows and Linux line breaks are not the same

And there's No "^m" edited in Linux.

You can use: sed command to deal with a bit like:

Sed ' s/^m//g ' troubles.txt >testip.txt #linux中 ^m Input Method: Ctrl + v +m

If you do not trust can be executed: cat-a testip.txt # haha, no ^m.

Execute small script to add IP: SH addip.sh # occasionally used, no need to add "execute permission"

IP Addr # view IP

"Local Linux"

Same upload trouble.txt, same fallback ^m for processing

You can also remotely copy it here:

SCP testip.txt [email protected] # hehe, intranet IP, external network can not access, forget it!!!

Ping script:

SH ipping.sh

Shell programming

The shell interprets the input to the operating system by prompting you for input.

Here with/bin/bash
Echo $SHELL

The format is as follows
#!/bin/bash# fixed format, in the first line of each script

System variables

Env #命令行输入 View

Environment variables

$HOME the current user's logon subdirectory
$PATH a colon-delimited list of subdirectories to search for
$PS 1 command-line prompt, usually a "$" character
$PS 2 Secondary prompt to prompt for subsequent input, usually ">" Characters
$IFS the delimiter for the input area. When the shell reads the input data, it considers a set of characters as separate characters between the words, usually spaces, tabs, and line breaks
$0shell the name of the script program
$# is the number of arguments passed to the script, $ #是一个数字

$* shows all parameters passed to the script, $* is a string

$? is to display the exit status of the last command, 0 means no error, others indicate an error
$$ the shell script's process ID, which is typically used by the scripting program to create unique temporary files, such as/tmp/tmpfile_$$

A first variable. The corresponding $ A is the second variable, and so on.
$UID the ID of the current user
$PWD

Custom variables (a class of character combinations that start with a letter or underscore , preferably known as a name )

In addition to system variables, others can be used,
========================
In a shell script, if is also often judged about the file attributes, such as whether it is a normal file or a directory, to determine if the files have read and write execution rights. There are several options that are commonly used:

-E: Determine if a file or directory exists

-D: Determine if the directory is not present and whether it exists

-F: Determine if the file is normal and exists

-R: Determine if the document has Read permissions

-W: Determine if Write permission is available

-X: Determine if executable

When you use the If judgment, the specific format is:
If [-e filename]; Then
echo "FileName exists"
Else
echo "No This file"

Fi
========================

First of all, the actual combat in the refining after the addition, please give more advice, more praise, thank you

A script that takes you through the Shell 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.