Shell Programming Examples

Source: Internet
Author: User

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

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.