The shell thing.

Source: Internet
Author: User

Which language is most helpful to you in your daily work? I think it's a non-shell. The earliest contact with the shell should be in college, such as the Linux file system cut will use some commands, such as Find, tar, Xargs, CP and so on, and put them together through the shell. But the first contact with the shell script in a real project is actually a batch script for Windows, and the bat script is used for automated testing during EFI testing. It was a huge set of scripts and a shock to the student age.

After graduating from work, the work environment is mainly under Linux, will inevitably use a variety of commands, and try to use these commands to solve some practical problems, such as recursively enumerate all the text files in the current directory, when the system after browsing the grep man document, in fact, only need a command, such as you want to know/ Whether some commands are simple shell scripts under the bin directory:

?
12345 $ grep -Irl ‘.*‘/bin/bin/unicode_start/bin/unicode_stop/bin/redhat_lsb_init/bin/gawk

-I is used to specify that no binary matches any binaries,-r recursively searches subdirectories,-l when matching the previous line when the file name is printed and skips the current file because the match we provide is a universal match, so all possible non-empty text files are enumerated.

For example, want to count how many lines of code have recently been written:

?
12345 $ find service/ -name *.java |xargs wc -l   46 service/ServiceFactory.java   94 service/IShopService.java 1779 service/ShopService.java 1919 total

Of course, the occasional use of awk or SED, such as the above command rewrite again, to achieve the following cumulative function:

?
123456789 $ find service -name *.java |xargs wc -l|grep -v total | awk ‘> BEGIN {sum=0;fnum=0;}> {sum=sum+$1; printf"%-40s %s\n",$2,$1; fnum=fnum+1; }> END {printf"Total files: %s,\tTotal lines: %s\n",fnum,sum}> ‘service/ServiceFactory.java              46service/IBookShopService.java            94service/BookShopService.java             1779Total files: 3,     Total lines: 1919

Later, after joining Real-world Performance Group, the drive to write scripts went out of hand. This is largely due to the strong demand for the shell from everyday projects, and to a foreign colleague who is well versed in the shell. Some of the previously useless utility functions began to become working habits. One of the most useful features is how to locate a bash script error. The debug option-X can print out the execution of the entire shell script, which plays a huge role in the scripting process. There is also how to set default values for parameters and how to handle arrays dynamically using set. At the same time, we learned a lot of ways to use the shell to write the actual case business logic of the project. such as how to implement multi-process concurrency and queueing, when a process completes, the first process in the queued queue gets the opportunity to continue running.

Back to the shell language is designed to be practical and convenient, sometimes it may give people a sense of not rigorous. Unlike the C statement, the carriage return will affect the parsing of the shell statement. The IF statement has at least the following three types:

?
12345678910111213 # first form if  condition then    commands fi    # Second form if  < Code class= "CPP Plain" >condition; Then &NBSP;&NBSP; commands fi &NBSP;  # third form if  condition; then  commands; fi

A line of code is basically equal to a line of statements, if you want to include more than one statement in a row, you must use, separate, understand this, you will not miss some semicolons or add more unnecessary semicolon.

Also, condltion can actually be a command, depending on the success of the command to decide whether or not to execute the corresponding command, it seems to be very convenient design, such as

?
1234 ifgcc demo.c -o demothen  ./demofi

Of course, you can take advantage of the command's return value $? To make a judgment, a successful $? will have a value of 0:

?
12345 gcc demo.c -o demoif[ $? = 0 ]then  ./demofi

Of course, the above logic can be implemented simply &&:

?
1 gcc demo.c -o demo &&  ./demo

Compared to advanced statements such as C/c++/java,shell should be the most easy to get started, but also to bring the most convenient tool language, whether it is the writing program to do the concurrent stress test, or to achieve more complex control logic, Shell statement development efficiency is generally higher than those of high-level language, do not need to compile, immediately after writing can be tested. In this era of the pursuit of efficiency, not familiar with the shell are embarrassed to say that will be performance tuning AH.

The shell thing.

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.