Shell script FAQ

Source: Internet
Author: User
Document directory
  • Determine whether the file is a device file
  • Add User:
  • IP address access statistics:
  • Calculate the sum of 2 numbers
  • Text Analysis
  • File Sorting
  • Print the swap partition size of the Local Machine
  • Output the time used to create 20000 directories on the local machine
  • Print the current sshd port and process ID
  • Print the number of executable files that can be used by the root user
  • Compile all. c files in the current directory:
  • Change the extension of all files in a directory to Bak.

#/Bin/sh

Max_cpu = 0
Avg_cpu = 0
Total_time = 1

Process = $1
Interval = $2

# Check the parameters
If [$ #-ne 2]; then
Echo "Usage: $0 processname interval"
Exit
Fi

Logfile = "per.txt"
Echo "'date'"> $ logfile

While sleep $ Interval
Do
Top-D 1-N 1 | grep $ process | grep-V grep | awk '{print $9 "\ t" $10}'> $ logfile
Done

 

Determine whether the file is a device file

 

#/Bin/bash

Echo-e "the program will judge a file is or not a device file. \ n"

Read-P "input a filename:" filename

If [-B $ filename-o-c $ filename]; then

Echo "$ filename is a device file"

Exit 0

Else

Echo "$ filename is not a device file"

Exit 1

Firead-P: Used to output prompt information when reading data

 

Note! [There is a space between: If! [-F $ filename]; then. Generally, if [! *]

 

Add User:

 

#/Bin/bash

 

Groupadd-F class1

For I in {9909... 9911}

Do

Xx = 'echo $ I | SED's/99 // g''

Useradd-G class1 STD $ {XX}

Echo STD $ {XX} | passwd STD $ {XX} -- stdin

Echo-e "User STD $ {XX} passwd is STD $ {XX}" >>/ root/newuser.txt

Done

Exit 0

 

Note that there should be no spaces before and after the equal sign: xx = 'echo $ I | SED's/99 // g''

If there are characters before and after the variable, it must be braces.

 

IP address access statistics:

You need to analyze the Apache access logs to find out the top 100 IP addresses. The log size is around 78m. The following is an Apache access log excerpt.

 

202.101.129.218--[26/MAR/2006: 23: 59: 55 + 0800] "Get/online/stat_inst.php? PID = d065 HTTP/1.1 "302 20-"-""-"" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 )"

 

 

# Awk '{print $1}' log | sort | uniq-c | sort-r | head-N10

5 221.224.78.15

3 221.233.19.htm

1 58.63.148.135

1 222.90.66.142

1 222.218.90.239

1 222.182.95.155

1 221.7.249.206

1 221.237.232.191

1 221.235.61.109

1 219.129.183.122

 

Here is a question about why sort is required before uniq is used.

 

Calculate the sum of 2 numbers

#/Bin/bash

Typeset first second

Read-P "input the first number:" First

Read-P "input the second number:" second

Result = $ [$ first + $ second]

Echo "result is: $ result"

Exit 0

 

 

Text Analysis

Retrieve the number of times the shell appears in the password
Result of the first method:
4/bin/bash
1/bin/Sync
1/sbin/halt
31/sbin/nologin
1/sbin/Shutdown
Result of Method 2:
/Bin/Sync 1
/Bin/bash 1
/Sbin/nologin 30
/Sbin/halt 1
/Sbin/shutdown 1

 

Answer:

CAT/etc/passwd | awk-F: '{if ($7! = "") Print $7} '| sort | uniq-C

CAT/etc/passwd | awk-F: '{if ($7! = "") Print $7} '| sort | uniq-c | awk' {print $2, $1 }'

 

 

File Sorting

Employee ID and name are recorded in the employee file
Employee.txt:
100 Jason Smith
200 john doe
300 Sanjay Gupta
400 Ashok Sharma
Bonus file records employee ID and salary
Bonus.txt:
100 $5,000
200 $500
300 $3,000
400 $1,250
Merge the two files and output them as follows:
Processing result:
400 Ashok Sharma $1,250
100 Jason Smith $5,000
200 John Doe $500
300 Sanjay Gupta $3,000

 

Answer: Join employee bonus | sort-K 2

 

 

Print the swap partition size of the Local Machine

Processing result:
Swap: 1024 m

 

Free-M | sed-n'/SWAP/P' | awk '{print $2 }'

Free-M | sed-n's/swap: \ * \ ([0-9] * \). */\ 1/P'

 

Output the time used to create 20000 directories on the local machine

Processing result:
Real 0m3. 367 s
User 0m0. 066 s
Sys 0m1. 925 s

 

Answer:

# Time For I in {1 .. 2000}; do mkdir/root/Neil $ I; done

 

Real 0m6. 200 s

User 0m1. 128 S

Sys 0m4. 710 s

 

Print the current sshd port and process ID

Processing result:
Sshd Port & amp; PID: 22 5412

 

Answer: netstat-anp | grep sshd | sed-n's /. *: \ ([0-9] * \) \. * \ ([0-9] * \) \/sshd/\ 1 \ 2/P'

 

Print the number of executable files that can be used by the root user

Processing result:
Root's bins: 2306

 

Echo "Root's bins: $ (find./-type F | xargs LS-L | sed '/-. X/P' | WC-l )"

Root's bins: 3664

 

Compile all. c files in the current directory:

 

For file in *. C; do echo $ file; gcc-o $ (basename $ file. c) $ file; sleep 2; done> compile 2> & 1

 

Change the extension of all files in a directory to Bak.

For I in *. *; Do MV $ I $ {I %. *}. bak; done

From: http://blog.chinaunix.net/space.php? Uid = 20393955 & Do = Blog & id = 345320

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.