Shell script learning and shell script1111111111111 clear the simple command script of the messages log file under/var/log/Var/log/the system file under the log file must have the root permission:$ UID is the global variable of the system:Make make install indicates that the previous step is successful.Make | make install indicates that the previous operation is
,/sbin,/usr/ Bin,/usr/sbin wait in path, your current directory is usually not in path, so write mayuan.sh will not find the command, to use. /mayuan.sh told the system that it was looking in the current directory. As shown in the following:2. As an interpreter parameterThis works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:/bin/bash mayuan.shThe sc
Linux learning-shell simple writing-shell
It is recommended that all scripts be stored in/usr/local/sbin.
Run the sh-x script. sh-x to view the execution process.
1. When using a variable in a script, use the $ symbol:
#! /Bin/bash
# Assign a value to a variable using reverse quotation marks
D = 'date + "% H: % M: % S
bash_rematch[] array #re1.sh#!/bin/bash# manage to know your nameread-p "Please input your fullname:" Nameif [[$NAME =~ (. *) [[: Space:]] (. *)]];thenecho "Your last name ${bash_rematch[2]}" echo "Your name is ${bash_rematch[1]}" echo "Full name ${bash_rematch[0 ]} "fi execution results [[email protected] shell]#./re1.sh Please input your Fullname:liancao Liu your surname Liu you call liancao full name Li
NOTES: # Note: The # in #!/bin/bash at the beginning of the shell file is not a comment run: 1, bash shell.sh 2, chmod +x shell.sh./shell.sh 3, add to Path change Amount of 4,. ./shell.sh Troubleshooting: 1, syntax error when running with prompt 2, logic error using echo printing, Sleep Number: Sleeping number seconds, reduce the cycle speed using
load bash-related configuration files (typically the Non-login Shell's ~/.BASHRC) and execute bash to enable our commands to execute. Otherwise the program will not know what shell to use.The environment variable is set here so that the program can execute some external commands directly while it is in progress, without having to write absolute paths.The last li
:
#!/bin/bash
# Calling one function from another
Number_one () {
Echo "Url_1 is http://see.xidian.edu.cn/cpp/shell/"
Number_two
}
Number_two () {
Echo "url_2 is http://see.xidian.edu.cn/cpp/u/xitong/"
}
Number_one
Operation Result:Url_1 is http://see.xidian.edu.cn/cpp/shell/Url_2 is http://see.xidi
the function return value.Let's look at an example:#!/bin/bash# Define your function Herehello () {echo "Url is http://see.xidian.edu.cn/cpp/shell/"}# Invoke your Functionhe LloOperation Result:$./test.shhello world$The calling function only needs to give the function name, and no parentheses are required.Then look at a function with a return statement:#!/bin/bashfunwithreturn () {echo "The function is to
command line parameters, we can use the following way to traverse it, but usually we prefer to use "[email protected]", because it can be in the command line parameters within the space when it distinguishes it6. Variable $ #用于统计命令行参数的个数:7, the default value of the variable: 1) b=${a:-"xxx"} means if the variable a is not set or the value is empty, then b= "xxx", 2) with:-The symbol is different ${a:= "xxx"} means if a is not set or is empty, then a is set to ' xxx ' and return the value "XXX",
#ch #!/bin/sh mod +x filename
Cho *.jpg∪ slow Teman tip Wine together Ltd U Chung Yuen oldest? /filename to execute your script.
This will print out the results of "mail.jpg tux.jpg".
quotation marks (single and double quotation marks) prevent this wildcard extension:
#!/bin/sh
echo "*.jpg"
Echo ' *.jpg '
This will print "*.jpg" two times.
Single quotes are more restrictive. It prevents any variab
Open a text editor, create a new file with the extension sh (sh for Shell), and the extension does not affect script execution.Enter some code:
#!/bin/bash
echo "Hello world!"
"#!" is a contract tag that tells the system what interpreter the script needs to execute, even if it uses a shell. The echo command is used to output text to a window.The
2 3
Do
For var2 in 0 5
Do
if [ $var 1 -eq 2-a $var 2 -eq 0 ] /c0>
Then
break 2
Else
Echo "$var 1 $var 2"
fi
Done
Done
As above, break 2 means to jump out of the outer loop directly. Operation Result:1 01 5Continue commandThe continue command is similar to the break command, with only a little difference, and it does not jump out of all loops and just jumps out of the current loop.To modify the above example:
#!/bin/bash
control (%), format-string can be reused and all parameters can be converted.
Arguments use spaces separated by commas.
Take a look at the following example:
# format-string is double quotes
$ printf "%d%s\ n" 1 "abc"
1 ABC
# single quotes are the same as double quotes
$ printf '%d%s\ n' 1 "abc"
1 ABC
# No quotation marks can also be output
$ printf %s abcdef
ABCdef
# The format specifies only one parameter, but the extra parameter is still output in th
symbolic link when installing the cross-compilation toolchain in arm bare metal)2. Shell is a kind of programming language(1) The language used to write shell scripts is the shell language, also called the scripting language.(2) Shell scripts are actually a class of languages rather than a language.3. Common
In the shell, arguments can be passed to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of a $n, for example, $ $ for the first argument, and $ = for the second argument ...Examples of functions with parameters:
#!/bin/bash
Funwithparam () {
Echo "The value of the first parameter is $!"
Echo "The value of the second parameter is $!"
Ech
[email protected] treats each parameter differently#!/bin/bash"[email protected]"echo $iDone#运行: Test. 3#输出:1 2 3 #解释: Each parameter in [email protected] is considered to be independent, so this for loop loops multiple times4. $#This variable represents the number of all parameters in the command line#!/bin/bashecho $# #运行: Test. SH 1 2 3 #输出: 3Six, the predefined variable 1. $?The return status of the last command executed. If this value is 0,
In daily linux system management, shell scripts are indispensable. If you do not write shell scripts, you are not a qualified administrator. At present, many organizations are required to write shell scripts when recruiting linux system administrators. Some organizations even use the ability to write shell scripts to m
A matter of note1. The beginning of a script behavior:#!/bin/bashdeclare the name of the shell that this script requires2. "#!" is out of the program Other than the one that starts with # is the comment content3. For the use of the system command, you can set the primary environment variables such as path at the beginning of the program4. How to run the script:
SH example.sh or similar bash example
Shell Scripting Learning GuideThe following eight points can not be said to be your shell Scripting Learning Guide de all, at least let you write a reliable shell script.1. Specify bashThe first line of the shell script, what shou
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.