First, Introduction
From the programmer's point of view, the shell itself is a program written in C language, from the user's point of view, the shell is the user and the Linux operating system communication Bridge. The user can either enter command execution or use shell scripting to do more complicated operations. In the increasingly perfect Linux GUI today, in the field of system management, shell programming still plays a role that can not be ignored. An in-depth understanding and proficiency in shell programming is one of the required courses for every Linux user.
There are many types of shell in Linux, common: Bourne shell (/usr/bin/sh or/bin/sh), Bourne Again Shell (/bin/bash), C Shell (/usr/bin/csh), K Shell ( /usr/bin/ksh), Shell for Root (/sbin/sh), and so on. Different shell language syntax differs, so it cannot be exchanged for use. Each shell has its own characteristics, and basically, mastering either of these is enough. In this article, we focus on bash, the Bourne Again Shell, which is widely used in daily work due to ease of use and free, and bash is the default shell for most Linux systems.
Ii. Examples of programming
Program 1: Batch parallel telnet
In the network testing process, we need to telnet from a server to a number of other servers, such as Port 22, if the ability to execute Telnet peer-to-peer port in bulk parallel, will greatly improve the testing efficiency, using shell programming can achieve this function. #文件结构
test10-was%tree.| -- ip.list #测试ip列表|--telnet_port. sh #测试Shell脚本|-- telnetAlive.txt #连通结果集 |-- telnetNotAlive.txt #未连通结果集 ' --TelnetResult.txt #日志
#文件名: Ip.list
#功能: Storing list of IP to be tested
#格式如下
10.87. 30.10 10.87. 30.11 10.87. 30.12 10.87. 30.13 10.87. 30.14 #此处需下空一行 and write the final IP10.87again. 30.14
#文件名: telnet_port.sh
#功能: Network Testing
#!/bin/BashRMTelnetResult.txt >/dev/NULL 2>&1RMTelnetAlive.txt >/dev/NULL 2>&1RMTelnetNotAlive.txt >/dev/NULL 2>&1Testport=30000#要测试的端口号 forIinch$(Catip.list) Do(telnet $i $TestPort <<eof>>telnetresult.txt &) >>telnetresult.txt2>&1Sleep 1quiteof Done
cat telnetResult.txt | grep -B 1 \] | grep [1 -9 ] | awk " {print $ " | cut -D " . "-F 1 , 2 , 3 , 4 > TelnetAlive.txt cat ip.list telnetAlive.txt | sort | uniq -u > TelnetNotAlive.txt
Run:
Shell Programming Examples