About Eval and crontab commands in the Shell _unix Linux

Source: Internet
Author: User
Tags eval

Eval

Eval performs a two-pass scan of the following commands, executes this command if the command is a normal command after the first scan, and guarantees the semantics of the indirect reference if the command contains an indirect reference to the variable. That is, the eval command will first scan the command line for all permutations and then execute the command. Therefore, the eval command applies to variables whose functionality cannot be implemented by one scan.

Eval performs the following two steps

For the first time, perform variable substitution, similar to the C language macro substitution

The second time, execute the replaced command string

Chestnuts:

1. There is a file named Test with the contents "please move on!" , define a string variable that is a content cat test , and use the command to refer to echo the variable, print the string directly, and then use eval to output the contents of the file test!

This is because the first scan of the eval command performed a variable permutation, and the second scan executed the command contained in the string cat test .

2. Get the last argument passed to the shell script

If we know the number of arguments passed to the shell and want to output the last parameter, you can use the echo direct display, for example, in this case, the parameters entered in this example have 10, 11, 12 altogether three parameters, you can use the command: Echo $

But if we don't know that we have a few parameters and we want to output the last argument, you might think of using the $$# ( $# number of arguments to pass to the shell script to output the last argument.

If you use the command: echo "\$$#"  The result is $12, not what we want.

Use the eval command to achieve our expectations: The eval echo "\$$#" result is 12.

Shell script content:

Execute script file:

3. Another example:

[Admin@localhost test]$ g= ' | '
[admin@localhost test]$ eval ls $G wc-l

The first step is replaced by the eval ls | wc -l second part, then the command result is executed.

Attention:

1. Eval cannot obtain function processing results

2. Eval nesting is meaningless and can be performed in other languages eval(eval("code")) (executing the return of dynamically generated code), and because the eval in the shell executes the subsequent eval command simply as a command string, it loses its nesting and the nesting is replaced by a command substitution.

Crontab

Crond is a daemon that is used by Linux to periodically perform certain tasks or wait to handle certain events, and the system automatically starts the Crond process, and the Crond process periodically checks to see if there are any tasks to perform, and automatically executes the task if there are tasks to perform.

The crontab file contains a series of jobs and instructions sent to the cron daemon. Each user can have his or her own crontab file, while the operating system holds a crontab file for the entire system, which is usually stored in subdirectories under/ETC or/ETC, which can only be modified by the system administrator.

The crontab command is used to set instructions that are periodically executed by the system. This command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The/etc/crontab file is the configuration file for system task scheduling.

Parameters:

-L Displays the current crontab on the standard output.

-R Deletes the current crontab file.

-e Edit the current crontab file using the editor that the visual or editor environment variable refers to

Use format:

* * * * command

Time-sharing and Lunar Week command

The 1th column represents the minute 0~59 (per minute * or */1)

The 2nd column indicates the hour 0~23

The 3rd column represents the date 1~31

The 4th column represents the month 1~12

The 5th list of the week 0~6 (0 for Sunday)

The 6th column indicates the command that needs to be executed

In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values in the range of numbers. "/" stands for each meaning, "/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers.

Some examples of the use of crontab files:

#每晚的21:30 Restart Apache * * * *
/usr/local/etc/rc.d/lighttpd restart
#每月1, 10, 22nd 4:45 restart Apache
45 4 1,10,22 * */usr/local/etc/rc.d/lighttpd Restart
#每周六, Sunday 1:10 restart Apache
1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart< c6/> #每天18:00 to 23:00 restart Apache 0,30 18-23 * * * *
/usr/local/etc/rc.d/lighttpd restart
#每星期六的11: PM Restart APA Che
0 * * * * 6/usr/local/etc/rc.d/lighttpd restart
#晚上11点到早上7点之间, restart Apache
* 23-7/1 * * * * * * * * * * */usr/local/e every hour TC/RC.D/LIGHTTPD Restart
#每一小时重启apache * */1 * * * *
/usr/local/etc/rc.d/lighttpd Restart
# 4th per month with 11 points per Monday to Wednesday restart Apache
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
#一月一号的4点重启apache
0 4 1 /USR/LOCAL/ETC/RC.D/LIGHTTPD Restart #每半小时同步一下时间 */30 * * * * *
/usr/sbin/ntpdate 210.72.145.44

The above is about the shell in the eval and crontab command of the entire content, I hope this article can be helpful to everyone, if there are questions welcome everyone to discuss the message.

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.