also used "." When invoking the system function library under/etc/init.d/. Point number and source, let's look at the system function library/etc/init.d/functionsYou can also call the library with source, and the action prints out very flashy results, such asThe action in/etc/init.d/functions is a function defined in theWe can also define the functions ourselves in the functions, and then we can invoke the custom function. Such asYou can also send a reference, such asFour Basic specifications a
, and the shell interprets the execution.• Batch processing (Batch): The user writes a shell script in advance, with many commands that allow the shell to execute the commands one at a time without having to hit the command one by one.
Shell scripts and programming languages are similar, there are variables and proces
, in the shell script of the usual term loop.At this point, you press CTRL +z to pause it, and then enter BG to go back into the background.In the case of multitasking, if you want to move the task to the foreground, FG is followed by the task number and the task number can be obtained using the jobs command.9. >>, 2>, 2>> : The above-mentioned directional symbols > and >> denote the meaning of substitution and addition, then there are two symbols whi
detecting a file name provided by the user, we perform a 9 to 1 loop. File 9 is named 10, file 8 is renamed to 9, and so on. After the loop is complete, we name the original file 1 and create an empty file with the same name as the original file.DebugThe simplest debug command, of course, is to use the echo command. You can use Echo to print any variable value in any place where you suspect it is wrong. That's why most shell programmers spend 80% of
Basics of Linux Shell scripting here we first talk about the shell of the basic syntax, the beginning, comments, variables and environment variables, to do a basic introduction, although not related to specific things, but lay the foundation is to learn easily after the premise.
1. Linux
languages.
This begins with what we call the shebang mechanism, and his main function is to tell the system that the file should be executed by the Bash interpreter, followed by the body of the program.The content of the text is what you want to complete, then how to write the text content? The main ideas are as follows:
Demand analysis
Program Logic Analysis
Writing Program
Debugging bugs
First, let's take a simple script topic as an example:
$#
The number of arguments passed to the script or function
$*/[email protected]
All parameters passed to the script or function. When enclosed by double quotation marks (""),[email protected] is slightly different from $*
$$
The current shell process ID, which is the process ID for the shell script, which is where these scripts are located
command. You can use Echo to print any variable value in any place where you suspect it is wrong. That's why most shell programmers spend 80% of their time debugging programs. of the Shell programThe advantage is that there is no need to recompile, and it does not take much time to insert an echo command.The shell also has a real debug mode. If there is an error
results of "mail.jpg tux.jpg".quotation marks (single and double quotation marks) prevent this wildcard extension:
#!/bin/shecho "*.jpg" Echo ' *.jpg '
This will print "*.jpg" two times.Single quotes are more restrictive. It prevents any variable expansion. Double quotes prevent wildcard expansion but allow variable expansion.
#!/bin/shecho $SHELLecho "$SHELL" Echo ' $SHELL '
The result of the operation is
Http://classroom/pub/materials/userlist[Email protected]/]# vim/root/batchusers#!/bin/bashIf [$#-eq 0];then//$ #位置变量的数量, when no arguments are provided, the $ #为0, the expression is setEcho ' Usage:/root/batchusers ' >2//output Usage:/root/batchusers >2 into error outputExit 1//Return value 1, prompt for wrong input parameterselif [!-F $];thenEcho ' Input file not found ' >2Exit 2FiFor a in $ (cat $)DoUseradd-s/bin/false $aEcho $a created successfullyDone########################################
script requires a user name list file as a parameter2) If no parameters are provided, this script should give a hintUsage:/root/batchusers, exit and return the corresponding value3) If a nonexistent file is provided, this script should give aShow Input file not found, exit and return the corresponding value4) New User login Shell is/bin/false, no need to set password5) User list test file:Http://classroom/pub/materials/userlist# wgethttp://classroom/
1. Linux Scripting Basics1.1 Basic Introduction to grammar1.1.1 StartThe program must start with the following line (must be placed on the first line of the file):#!/bin/shThe symbolic #! is used to tell the system that the parameters behind it are the programs used to execute the file. In this example we use/BIN/SH to execute the program.When you edit a good script, you must also make it executable if you want to execute the script.To make the script
listDoCommand sequenceDoneFor a in Zhangsan Lisi Wangwu DCDoUseradd $aDoneVariable a participates in the loop, and the variable a value affects the result of the loop[Email protected]/]# vim/root/for.sh#!/bin/bashFor a in Nsd11 nsd12 nsd13 nsd15DoUseradd $aEcho $a created successfullyDoneVariable A does not participate in the loop, and the variable a value does not affect the result of the loop#!/bin/bashFor a in 1 2 3Do echo hello worlddone[Email protected]/]# cat/root/num.sh#!/bin/bashnum
whether the VSFTPD service is running, if it has been run to list its listening address, PID number, otherwise the output hint "VSFTPD service is not available!" ”First install the VSFTPD software, turn on the VSFTPD service (software CD)Then write the script vim chkftpd.sh[Email protected] ~]# chmod +x chkftpd.sh[Email protected] ~]#./chkftpd.shJINGGAO:VSFTPD fuwubukeyong!Set permissions and execute to prompt vsftpd service not availableExecute script again after opening VSFTPD server3. Multi-
.txt[[emailprotected] sbin]# ./05.expect 192.168.242.129 /tmp/123.txtspawn rsync -av /tmp/123.txt [emailprotected]:/tmp/123.txt[emailprotected]‘s password: sending incremental file list123.txtsent 71 bytes received 31 bytes 204.00 bytes/sectotal size is 0 speedup is 0.00Iii. building a file distribution system
Requirements background: For large companies, there must be a site or configuration file updates from time to time, and the use of the machine is certainly a lot of units, fewe
seconds, in the shell script of the usual term loop.At this point, you press CTRL +z to pause it, and then enter BG to go back into the background.In the case of multitasking, if you want to move the task to the foreground, FG is followed by the task number and the task number can be obtained using the jobs command.9. >>, 2>, 2>> : The above-mentioned directional symbols > and >> denote the meaning of substitution and addition, then there are two sym
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.