Linux command line usage tips

Source: Internet
Author: User
Linux command line usage tips

2681421. jpg (16.24 KB, Downloads: 41)

Download attachment

Linux command line usage tips-wheat Institute

Upload

In the world of software development, Linux is the most popular operating system for web applications. For Linux systems, if you can master some tips, you will be able to improve the efficiency in program development and debugging. Below we will share with you three tips on user command line in Linux programming.


1. how to schedule tasks in Linux without using Cron

Cron refers to scheduling a task/command in Linux. We usually use Cron when scheduling a task, but in fact, it is also possible to schedule a task if it is not applicable to Cron.

Run a command every five seconds (data.txt ). To achieve this, we can directly run the following single-line script at the command prompt.

$ While true; dodate> date.txt; sleep 5; done &

Where:

While true: let the script enter a true loop, that is, to create an endless loop, and run the commands in it repeatedly.

Do: do is the keyword in the while Statement. its subsequent commands will be executed, and one or more commands can be placed behind it.

Date> date.txt: run the datecommand and write its output to the data.txt file. Note that we use> instead of>.

>>: Append the file (date.txt). after each command is run, the output content is appended to the file. If> is used, the previous content will be overwritten over and over again.

Sleep 5: put the script in sleep state for 5 seconds, and then run the following command. Note that the time unit can only be seconds. That is to say, if you want to run the command every 6 minutes, you should use sleep360.

Done: Mark of the end of the while loop statement block.

&: Run the entire process in the background.

Similarly, we can run any script like this. In the following example, a script named script_name.sh is executed every 100 seconds.

It is also worth mentioning that the script file mentioned above must be in the current directory; otherwise, the full path (/home/$ USER /... /Script_name.sh ). The single-line script that implements the preceding functions is as follows:

$ While true; do/bin/sh script_name.sh; sleep 100; done &

Summary: The preceding single-line script is not a substitute for Cron, because the Cron tool supports many options, which are more flexible and customizable. However, if we want to run some tests, such as I/O evaluation, the above single-line script also works.



2. how to clear terminal content without using the clear command

Generally, the clear command is used to clear the terminal content in Linux. However, if you use the ctrl + l shortcut key, we will save a lot of time. Because ctrl + l is a shortcut key, it cannot be used in the script, except that the clear command must be used to clear the screen content in the script, you can use this shortcut key for other clearing content.


3. run a command in another directory and then return to the current working directory automatically.

In programming, we often want to run a command in other directories and then return to the current directory, but this is often difficult to implement. To achieve this, you only need to put the command in a parentheses.

The following is an example:

Avi @ deb :~ $ (Cd/home/avi/Downloads /)

Sample output:

Avi @ deb :~

It will first cd to the Downloads directory, and then return to the previous home directory. Maybe you think that the command in it is not executed at all, or there is a certain error, because there is no change from the command prompt. Let's simply modify this command:

Avi @ deb :~ $ (Cd/home/avi/Downloads/& ls-l)

Sample output:

-Rw-r ----- 1 avi 54272 May 3 text1.txt

-Rw-r ----- 1 avi 54272 May 3 text2.txt

-Rw-r ----- 1 avi 54272 May 3 text3.txt

Avi @ deb :~ $

In the above command, it first enters the Downloads directory, then lists the file content, and finally returns to the current directory. It proves that the command is successfully executed. You can include any command in brackets. after the command is executed, it will be returned to the current directory smoothly.

The above are three super practical tips for Linux command line. Mastering and using them will make your software development more professional and get twice the result with half the effort.




Recommended Learning: php development practical tutorial http://www.maiziedu.com/course/php/

Source: code


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.