Linuxshell script from getting started to a little bit more bumpy road than getting started

Source: Internet
Author: User
Tags egrep

a simple introduction to Scripts

* This article to just get started Linux learning script of the small white, in order to facilitate the understanding of professional words used not much, to the exact system of learning some concepts, but also need to learn, this article is for reference only

Before the script, the word script is only in Windows in some software problems, will be prompted "Current script error" and the like, of course, as a computer can only play League of Legends and Linux only basic command of the small white, at this time on the script is still not much concept, But even if this dish of the foot, I still have to use my IQ-negative brain to kill the thing called the script.
Yes, right! In Linux, a script is written when the command is hit. What runs the script is a thing called an interpreter. The default interpreter in Linux is Bashshell, and there are many interpreters such as Python. Will write script words, do not say anything else, at least do not knock so many smelly and long commands, are thrown into the script file, execute the script file on everything OK, of course, see this post of you guys should also learn basic commands such as the most basic LS, ifconfig, vim text editor, and grep, Egrep and so on Basic Text Processing command, very happy to tell you, just started to learn the script, these are enough (big God pass this post please lightly spray crab crab)
Learning it students with legs want to know, every learning as new language from the "HelloWorld" began, here is the same

HelloWorld Getting Started
             #!bin/bash             # This is first scriptof“hello world”             echo “hello world”

#!/bin/bash is the name of the interpreter called by the script (except for the # of the first line in the entire script, the # of the other lines is commented,)
#!/This is first script of ' Hello World ' is a scripting comment, that is, the contents of this line will not be executed
Open the Vim editor, tap into the command character above, save the exit and give the script execute permission just written

    [[email protected] ~]# chmod a+x

When the script has execute permissions, it can run the

    [[email protected] ~]# ./    hello world    

Similarly, at the beginning of the study, you can throw a lot of irrelevant commands into the script file, the execution of the script file will also be in the script file you write the command executed in sequence and displayed in the terminal, like this:

Open the Vim editor to knock the commands in, then give the script file a execute permission such as:

Execute the script file as follows:

[[email protected] ~]# ./

The results of the implementation are as follows:

    不是很难对吧?    太简单?    好吧,那就加点参数吧    相信能看到这里的各位同学也应该知道什么是参数了,小弟在此就不再赘述了
script with Parameters

The main explanation is that this thing, not only the dollar, but also a very common and critical character in shell scripts

$1代表位置参数,可以解释为执行脚本文件的同时在后面打一个参数,具体参数以脚本中要求的为准$(XXX)这个格式为取变量值,图中的意思则为取$(cat $1 |wc -l)的值并赋值给linesecho是用来输出信息的,大体上应该也能看懂吧,就是取$(XXX)的变量并赋值,然后输出这个值

Follow the steps and execute the results below, and note the following parameters while executing the script:

Do not have to tangle $ things, learn this stuff is plenty

parameter Filtering

Obviously, well, not much to say, the specific look at the parameters how to filter
Grep,egrep, as well as cut, TR and other text processing tools
Take IP as an example, I want to test whether an IP can ping, if the IP is to determine whether the condition of IP, For example 1192.18.0.256, it is clear that the first IP address of each bit up to three digits, 1192 does not meet, followed by each of the maximum value of 255, so 256 does not meet, so it is necessary to determine the input IP is eligible, and then ping operation, the script content is as follows:

When you're done, take a look at the following:

The execution result is easy to understand, but does the script content look very complex? Don't worry, actually all of the above is a line, but the screen is displayed as multiple lines, very simple
The location parameter, which is the IP address to be entered here,
Egrep-q is the silent output, that is, the execution of the success will not have any prompt results, the longest segment is the filter IP compliance requirements, compliance, then execute && after the statement, the input IP is thrown to the ping command execution (-c1 to ping only one packet,-W1 for one second) &>/dev/null to throw out the results

The specific logical relationship to see is clear:

The use of if in scripts
这个逻辑关系看着不难,但是脚本里面写成这个样子着实不好看,很容易出错,那就用点逻辑清晰的语句来写吧,换一个例子:例:给定脚本一个参数为普通文件,判断是否为以 .sh结尾的普通文件,是则给其赋予执行权限(x),否则输出“非用户脚本文件”:

What, it feels complicated, but it doesn't look organized, not as messy as the last script file?
Look closely, the same color box is a complete logical statement, if inside can continue to use if, need to pay attention to ensure the integrity of the statement, the first use of such a way to pay attention to grammatical errors at the same time, be careful not to forget the similar "fi" such as the conclusion of the sentence

* The contents of test and grep in the back of each if [] are not mentioned here until the script is learned.

First, the gray box is to determine whether the file exists, and then continue to execute the red box, otherwise, the direct output "file does not exist" script execution ends
Executes the red box to determine whether the given parameter file is a normal file, yes, continue to execute the blue box; or exit directly, end of script execution
Execute the Blue box to determine if the file ends with ". Sh", yes, add execute permission for it, no, output "non-user script file", End of script execution

    • Experience

Due to the current stage of logical judgment and the learning of the circular sentence is limited, can only write so much, but the basic command and the various options can be used at the same time now only the script with if write out is also very good, the script at this stage is the processing of the given parameters and the basic output, there is not too much complex other things, The basics are important! It's very important!! Do not underestimate the basis of the command or anything, the most basic LS command options are good dozens of, commonly used those who do not guarantee that all remember, so it is necessary to review more practice, so write scripts to use, otherwise, stumbled not to say, options can not remember, command function is not clear, grammar format is not clear, writing scripts must be difficult ,


Linuxshell script from getting started to a little bit more bumpy road than getting started

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: 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.