Linux01-bash script programming for signal capture and mission planning 53

Source: Internet
Author: User
Tags variable scope

First, the script programming knowledge point

1. Get the length of the character in the variable: ${#VARNAME}

2. Assigning values to variables:

A) ${parameter:-word}: If the parameter is empty or undefined, the variable is expanded to "word"; otherwise, the value is expanded to parameter;

such as: a=3

Echo ${a:-30}

Unset A

Echo ${a:-30}

b) ${parameter:+word}: If the parameter is empty or undefined, do nothing; otherwise, expand to "word" value;

such as: Echo ${a:+30}

A=1

Echo ${a:+30}

c) ${parameter:=word}: If parameter is empty or undefined, the variable is expanded to "word" and the expanded value is assigned to parameter;

such as: unset A

echo {a:=30}

D) ${parameter:offset}:offest means skipping a few;

e) ${parameter:offset:length}: Intercept substring, starting from the next character at offset and taking lenth long substring;

such as: A= ' Hello world '

echo {A:2:3}

3. Script configuration file

/etc/rc.d/init.d/service script, Service Script Support profile:/etc/sysconfig/Service script configuration file with the same name

4, variable scope, locals: local var_name=

such as: Edit a script, note where the function definition plus local and no local difference

#!/bin/bash

#

A=1

Test () {

Local A=$[3+4]

}


Test

For I in ' seq $a 10 '; Do

Echo $I

Done

5. Command mktemp: create temporary files or directories to avoid duplication with other filenames

Mktemp/tmp/file. XX (TMP directory is cleared by default for 30 days)

-D: Create as Directory

Second, signal capture

1. Signal:

Kill-signal PID

1:hup

2:int

9:kill

15:term

2, the script can achieve the signal capture, but 9 and 15 can not capture

Ctrl+c:sigint

3. Signal Capture command: Trap

Trap ' COMMAND ' signal list

such as: Edit a script to capture CTRL + C signal;

#!/bin/bash

#

Trap ' echo ' You go ... "' Int

While:;d o

Date

Sleep 2

Done

4, a row to execute multiple statements, the statement separated by semicolons;


Script instance:

Create a script ping.sh to capture the CTRL + C signal. and clean up the temporary files that are generated during the script run.

#!/bin/bash

#

network=192.8.8.

File= ' Mktemp/tmp/file. XX '

Clearup () {

echo "Quit ..."

RM-RF $FILE

Exit 1

}

Trap ' Clearup ' INT

For I in ' seq ten ';d o

If ping-c 1-w 1 $NETWORK $i &>/dev/null;then

echo "$NETWORK $i is up." | Tee >> $FILE

Else

echo "$NETWORK $i is down."

Fi

Done


III. Mission Plan

1. Task Plan mode:

A) perform a task at a future point in time;

At

Batch

b) To perform a task periodically;

Cron


2. At related

A) Use the format:

At time

At> COMMAND

At> Ctrl+d

b) Specify the time format:

Absolute time: hh:mm, DD. Mm. YY Mm/dd/yy

Relative Time: now+#

Units: minutes, hours, days, weeks

Blur Time: Noon, midnight, teatime

c) Options:

At-l = ATQ: Show jobs

at-d at_job_id = ATRM at_job_id: Deleting a job

d)/etc/at.deny and/etc/at.allow files

Control who uses the AT command

e) Execution result of the command: it will be sent to the user who scheduled the task in the form of mail;

3. Cron Related:

Cron: itself is an uninterrupted service that runs on the online host.

Anacron:cron, can be implemented to enable cron for various reasons in the past time the execution of the task is not performed, the resumption of normal execution once;

A) Cron:

A1) System cron Task: defined in/etc/crontab

Format: minutes, hours, days, months, weeks, user tasks

A2) User cron Task: defined in/var/spool/cron/username

Format: Minutes Hours day Month Week task

b) Valid value of time:

B1) minutes: 0-59

B2) Hours: 0-23

B3) Days: 1-31

B4) Month: 1-12

b5) Week: 0-7,0 and 7 both represent Sunday

c) Time-pass indication:

C1) *: All valid values corresponding to:

For example: The third minute of every hour: 3 * * * *

Third minute of every hour of the week: 3 * * 7

Daily 12:13:13 12 * * *

6th per month 12:13:13 12 6 * *

every year, July 6, 12:13:13 12 6 7 *

C2),: Discrete time point value:

For example: 10 minutes and 40 minutes per hour: 10,40 * * * *

every Tuesday Friday, 10 minutes and 40 minutes: 10,40 * * * 2,5

every Tuesday Friday 2 points 10 minutes 40 points: 10,40 02 * * 2,5

C3)-: Continuous point in time:

For example: Every Monday to Friday 2:10:10 02 * 1-5

c4)/#: How often in the corresponding value range,

Example: Every 3 minutes: */3 * * * *

every 2 hours: 01 */2 * * *

Every 1 hours: * */2 * * *

every other day: 10 04 */2 * *

d) Execution results will be emailed to the administrator;

such as: Only the wrong execution results are sent to the administrator:

*/3 * * * */bin/cat/etc/fstab >/dev/null

e) Cron environment variables: All cron execution commands go down the path specified by the PATH environment variable and add it in the script:

#!/bin/bash

Export Path=/bin:/sbin:/usr/bin:/usr/sbin

f) User Task Management command: crontab

-L: Lists all cron tasks for the current user

-E: Edit cron Task

-r: Remove all cron tasks

-u USERNAME: Managing cron tasks for other users

g) Cron Service

Service Crond Status

Chkconfig--list Crond

4, Anacron related

A)/etc/anacrontab file field (minimum time unit is daily)

First paragraph: not implemented in the past few days

The second paragraph: the number of minutes after the boot starts running

The third paragraph: represents the comment information

Fourth: Indicates the task performed

b) The Anacron service is turned off by default:

Service Anacron status

Cron Task Exercise:

Configure the machine to back up the/etc directory at 3:20 A.M. every two days, and the backup file to the/BACKUP/ETC directory. file name type: etc-2012-06-20.tar.bz2

*/2 * * root/bin/tar-jcf/backup/etc/etc-' date +%f '. tar.bz2/etc/*

This article is from "Nick Liu's blog," Please make sure to keep this source http://zkhylt.blog.51cto.com/3638719/1426060

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.