Familiar with shell programming

Source: Internet
Author: User

This is the first thing I did in my company that is directly related to programming and requires shell commands. In fact, I feel that the shell under Linux is the same as the DOS batch processing under windows.

 

Question:

1. Use the shell language stream control combined with ifconfig, grep, cut, Echo, sed and other commands to obtain from the interface

IP address, subnet mask, and Ethernet hardware address program showif. Sh.

① Call the program in the form of showif. Sh <parameter>.

② Output the result of <parameter>.

③ If the input parameter is incorrect, the help information is printed.

④ The execution and output are as follows:

#./Showif. Sh IPv4

#192.168.0.172

 

2. Use shell language stream control combined with ifconfig, grep, cut, Echo, sed and other commands to collect interface traffic in real time

Program speed. Sh.

Requirement: ① The Statistical interval is 3 seconds.

② Call the program in the form of speed. Sh <if Name>.

③ If the input parameter is incorrect, the help information is printed.

④ The execution and output are as follows:

#./Speed. Sh eth0

# Eth0: recive-> 128.36 K transmit-> 48.75 K

# Eth0: recive-> 268.20 K transmit-> 78.00 K

 

 

I spent a day and a half on the specific implementation:

1. The function is basically implemented here, but it is not very correct in using regular expressions, and the search method is relatively simple.

#! /Bin/bash
# Filename: showif
If test $ # = 0; then
Echo "the number of parameters cannot be 0. Enter the correct parameter [IPv4 | submask | Mac]"
Else if test $1 = "IPv4"; then
Echo "output IPv4 address :"
/Sbin/ifconfig | awk '{if ($1 = "Inet" & $3 ~ /^ Bcast/) Print $2} '| cut-F2-d ":"
Else if test $1 = "submask"; then
Echo "output submask subnet mask :"
Echo '/sbin/ifconfig | awk-F "Mask"'/mask/{print $2} ''| cut-F2-d ":"
Echo '/sbin/ifconfig | awk-F "Mask"'/mask/{print $2} ''| cut-F3-d ":"
Echo '/sbin/ifconfig | awk-F "Mask"'/mask/{print $2} ''| cut-F4-d ":"
Echo '/sbin/ifconfig | awk-F "Mask"'/mask/{print $2} ''| cut-F5-d ":"
Else if test $1 = "Mac"; then
Echo "output Mac physical address :"
Echo '/sbin/ifconfig | awk-F "hwaddr"'/hwaddr/{print $2} ''| cut-F1-d ""
Echo '/sbin/ifconfig | awk-F "hwaddr"'/hwaddr/{print $2} ''| cut-F2-d ""
Echo '/sbin/ifconfig | awk-F "hwaddr"'/hwaddr/{print $2} ''| cut-F3-d ""
Else
Echo "enter the correct parameter [IPv4 | submask | Mac]"
Fi
Fi
Fi
Fi

 

2. the filtering method (Regular Expression) used is not good, and the input of the invalid parameter is not completed (parameter n has been defined ):

#! /Bin/bash
# Filename: Speed
Delay = 1
# Here, n is used to detect input parameter errors, but it is not implemented in detail.
N = 1
Now_get = '/sbin/ifconfig $1 | awk-F "RX Bytes:"'/Rx/{print $2} '| cut-F1-d ""'
Now_up = '/sbin/ifconfig $1 | awk-F "TX Bytes:"'/Tx/{print $2} '| cut-F1-d ""'
While (test $ n-Ne 0)
Do
Have_get = $ now_get
Have_up = $ now_up
Now_get = '/sbin/ifconfig $1 | awk-F "RX Bytes:"'/Rx/{print $2} '| cut-F1-d ""'
Now_up = '/sbin/ifconfig $1 | awk-F "TX Bytes:"'/Tx/{print $2} '| cut-F1-d ""'
Recv_bytes = 'expr $ now_get-$ have_get'
Recv_bytes = 'echo "scale = 0; $ recv_bytes/$ delay" | bc'
# Recv_bytes = 'echo "scale = 0; $ recv_bytes/delay" | bc'
Recv_kb = 'echo "scale = 3; $ recv_bytes/1024" | bc'
Tran_bytes = 'expr $ now_up-$ have_up'
Tran_bytes = 'echo "scale = 0; $ tran_bytes/$ delay" | bc'
# Tran_bytes = 'echo "scale = 0; $ tran_bytes/delay" | bc'
Tran_kb = 'echo "scale = 3; $ tran_bytes/1024" | bc'
# Echo "Download:" $ recv_bytes "B/S upload:" $ tran_bytes "B/S"
Echo "Download:" $ recv_kb "kb/s upload:" $ tran_kb "kb/s"
Sleep $ Delay
Done

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.