Basic shell script commands
How to write a shell script
First open the programming interface and look at the permissions information for the files in the dog directory now. Now b.txt has user permissions RW, group permissions RW, and other permissions R. Now I add X run permissions to the user permissions.
Create a new file shell script generally use x.sh as a suffix of course courage his can also. Open a terminal and type touchfirst.sh to create a new shell script named first.
There are three types of commands that can be used in shell scripts: 1) Unix commands: Although you can use any unix command in a shell script, there are some relatively more commonly used commands. These commands are usually used for file and text operations.
Writing a SHELL script program under UNIX/Linux is not difficult, as long as the full-screen editing program vi is used proficiently.
How to determine the success of a basic command in a shell script
Symbol shows the return value of the previous command, if it is 0, it means that the execution was successful, and the other means that it failed.
Use the special variable: $? Judge whether the execution of the previous command was successful (or return code) The value of the successful execution variable is 0, not zero means that the execution was not successful Example: Note: Be sure to judge the command immediately after its execution, otherwise the code that follows will flush out the value of the $? variable’s value.
Shell scripts to determine whether the last command was executed successfully use the symbol “$?” in the shell to display the return value of the last command executed. to show the return value of the last command executed, if it is 0, it means the execution was successful, other means failure. Combined with the if-else statement to determine the success of the last command.
The first step is to drag the written bat batch file to the cmd command window in order to clear the old data periodically, see the following figure, go to the following steps.
, you can get all the processes by ps command, and then extract the process name by awk command, and then grep to extract the corresponding process name can be.
LINUX Quick Start Chapter 8: Shell Basics
1, the first type: BourneshellBourneshell Bourneshell also includes Bourneshell (sh), Kornshell (ksh), BourneAgainShell (bash) three types. Bourneshell(sh), developed by Steve Bourne of AT&T, is the standard UNIXShell, and many UNIX systems come with sh.
2. The opening program must begin with the following line (which must be squarely on the first line of the file):! /bin/sh symbol #! is used to tell the system that the argument that follows it is the program used to execute the file. In this example we use /bin/sh to execute the program.
3. Following the previous track, the next step is the study of shell scripting in Linux. This is very, very important for raw information engineers to subsequently deal with large amounts (massive would be more appropriate) of data, but again, as a bit of a curmudgeon, I’ve been dead on my feet for quite some time as to what scripting means.
4, first of all, read “Bird’s Linux Private Kitchen – Basic Learning”, this is recommended to buy a book, ready to turn.
shell script read command usage
) while the outer read reads each line in the file pbook in turn, while the inner read is what is read from the console. 2) To achieve whileread nesting, you can use the exec command, which will redirect I/O. Note the use of exec, and the use of parentheses.
In shell scripts, the command used to read the contents of individual fields within a file and assign them to shell variables is: read-pinputaval:valecho$val. read-reads values from standard input. This read internal command is used to read a single line of data from standard input.
while condition do… .done should be preceded by a do. This is a fixed structure that loops all the way through when the condition is met. This is because there are input redirection symbols at the end that specify the input for the entire while loop condition to the file defined by the config variable.
How to use variables in shell script
1, linuxshell variables are generally defined in the format: var=value. where var is the variable name, = is the assignment, and value is the value assigned to the variable. For example, the following variable definition.
a=12;
b=”hello world”
2. Note that the first character of a variable name cannot be a number, but can be an underscore. The following example is an illegal variable name.
3. To use the value of a variable, put a $ sign in front of the variable. For example, the following example:
123=123 //this is the wrong variable name
_123=123//this is legal.
I’m writing a shell script under linux to execute a certain executable program test every day at 0:00. I’m just learning shell and would like to have a code reference from the masters!
Execute a program at a certain time, accurately referred to as “timed tasks”, I here to introduce two linux (ubuntu) under the timed execution of python scripts.
Method 1:
Prepare the script auto.py
Edit the following file with root privileges
Add the following commands at the end of the file
The above code means to execute the script every two minutes and print the log. script once every two minutes and print the log.
Crontab Writing Explained
Basic Format
Example
Method 2:
APScheler Timing Framework
APScheler is a Python timed task framework that is very easy to use. It provides date-based, fixed-interval, and crontab-type tasks, and can persist tasks and run applications as daemons.
Installation required to use APScheler
Example executed every morning at 6:30am
What is BlockingScheler in the code?
BlockingScheler is a scheduler in APScheler, there are two common schedulers in APScheler, BlockingScheler and BackgroundScheler, BlockingSchele is used when the scheduler is the only task to be run in the application, if the you want the scheduler to execute in the background, use BackgroundScheler.
What is cron in the code?
cron: triggered periodically at a specific time
How to write a shell script to find the first 10 terms and the sum of the Fibonacci series?
Shell script. First of all it is a script and does not serve as a formal programming language. It is called a shell script because it runs in a linux shell. To be clear, a shell script is a collection of commands. For example, I would like to do this:
1) Go to the /tmp/ directory;
2) List all the filenames in the current directory;
3) Copy all the current files to the /root/ directory;
4) Delete all the files in the current directory. The 4 simple steps in the shell window require you to hit command 4 times and press enter 4 times. Isn’t that a lot of work? Of course, this 4-step operation is very simple, if it is more complex command settings need dozens of operations? In that case, one at a time to hit the keyboard will be very troublesome. So it may be worthwhile to record all the operations to a document, and then go to the document to call the command, so that a step can be completed. In fact, this document is a shell script, but this shell script has its own special format.
Shell scripts can help us to manage the server very easily, because we can specify a task plan to execute a shell script regularly to achieve what we want. This is a matter of pride for linux system administrators. Now the 139 mailbox is very good to use, send e-mail at the same time you can also send an e-mail notification of the text message to the user to take advantage of this, we can deploy in our linux server monitoring shell scripts, such as network card traffic anomalies or the server web server stops you can send an e-mail to the administrator, and at the same time to send the administrator an alarm text message so that we can let us know in time the server problem. This allows us to know when something is wrong with the server.
There is a need to agree on a problem, where custom scripts are recommended to be placed in /usr/local/sbin/ directory, the purpose of doing so is that, first, you can better manage the documentation; secondly, to take over the future of your administrators know where the custom scripts are placed for easy maintenance.
How to write a shell script for linux to check if a 100 devices all have a certain service, such as crond(timed task
How to write a shell script
This article describes how to write a shell script with a lot of examples.
Why shell programming
While there are a wide variety of graphical interface tools available for Linux, the sell is still a very flexible tool. shell is not just a collection of commands, but it is a great programming language. You can automate a large number of tasks by using the shell. The shell is particularly good at system administration tasks, and is especially suited to tasks where ease of use, maintainability, and portability are more important than efficiency.
Here’s a quick look at how the shell works:
Creating a script
There are a number of different shells in Linux, but usually we use bash
(bourne
again
shell)
to program the shell. shell programming because bash is free and easy to use. So in this article the author has provided scripts that use bash (but in most cases these scripts can also be run in bash’s big sister, the bourne
shell).
As with any other language, our shell programs are written through our use of any one of a number of text editors, such as nedit, kedit, emacs, vi
, and so on.
The program must start with the following line (it must be square on the first line of the file):
#! /bin/sh
The symbol #! is used to tell the system that the argument that follows it is the program that will be used to execute the file. In this example we are using /bin/sh to execute the program.
When a script is edited, it must also be made executable if it is to be executed.
To make the script executable:
chmod
+x
filename
Then you can make it executable by typing:
. /filename
to execute your script.