#!/bin/bash- function mytest () { echo "arg1 = $" If [$ = "1"]; then return 1 else return 0
fi}if MyTest 2; Then echo "Aaaaaaaaaa" fi
Execution Result:Change it a little bit.#!/bin/bash-function mytest
Tips for ShellThere'll is some case-based tips for Shell.1. Replace The sequence space with newlineInput Examplea b c dOutput ExampleabcdSummarysedis the best solution.Solutionsed -r -e ‘s/[[:space:]]/\n/g‘2. Sort the unordered word list (with
1. Time CalculationStarttime= 'Date+'%y-%m-%d%h:%m:%s''; Endtime=`Date+'%y-%m-%d%h:%m:%s''; Start_seconds=$(Date--Date="$starttime"+%s); End_seconds=$(Date--Date="$endtime"+%s);EchoEnd_seconds-start_seconds2. Get the status code returned by the
In the shell, if you want to get the return value of a command, if it is an integer, you can get its exit code directly with $.But if the return value of a command is not an integer, how is it implemented?Have to mention the command substitution
[ -a FILE ]
FILEtrue if it exists.
[ -b FILE ]
True if it FILE exists and is a block special file.
[ -c FILE ]
FILEtrue if a special file is present and is a word.
[ -d FILE ]
True if it
Suppose we define a variable as:File=/dir1/dir2/dir3/my.file.txtDifferent values can be replaced with ${}, respectively:${file#*/}: Remove the first one/And the string to its left:Dir1/dir2/dir3/my.file.txt${file##*/}: Erase the last one/And the
Order of 1.Shell Search commandsWhen the shell executes the command, it does not directly look in the path path, but instead looks for the command location in a fixed order. The search order is as follows.1. Aliases. Even if you create a command
Syntax 1:substring=${string:start:len}The subscript of string starts with 0, with start, but the Len character is truncated and assigned to substring1 #!/bin/bash2 #substr =${string: Start:len}3 str=" 123456789 " 4 substr=${str:3:3}5echo $substr6 7
1. Shell Variable TypeThe shell is a dynamic type language (not using explicit data declarations) and weakly typed languages (variable type operations vary according to requirements). The variables in the shell are non-typed (all string types), but
1. The concept of environmental variablesEnvironment variables are used to store shell sessions and work environments, allowing the shell to easily access the program in the set value, environment variables are divided into global variables and
Command history# Cat/root/.bash_history//place where historical commands are stored# History//View the number of command histories# echo $HISTSIZE//View the number of bars that can be saved# Vim/etc/profile//Change the value of variable histsize
variable names can only contain numbers, letters, and underscores, because some variables that contain other characters have special meanings, and such variables are called special variables.
List of special variables
#!/bin/bash # Check if the 192.168.1.1-192.168.1.254 host is aliveFor IP in 192.168.1. {1..254};DoIf ping-c 1 $ip >/dev/null; Thenecho "$ip OK."Elseecho "$ip no!"FiDone#!/bin/bash #检查多个域名是否可以访问Url= "www.baidu.com www.sina.com www.jd.com"for URL in
First, declaring variablesMyurl= "http://see.xidian.edu.cn/cpp/linux/"mynum=100Attention:There can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with.At the same
&& (and) with the || (OR) with the ; (and)A semicolon has just been mentioned above for the delimiter between multiple commands. There are also two special symbols that can be used in the middle of multiple commands, that is, "&&" and "| |" are all
Find FilesFind./-type FFind a DirectoryFind./-type DFind a file or directory with the name testFind./-name TestLook for a file whose name matches the regular expression, and note the preceding '. * ' (found file with directory)Find./-regex.
Advanced sorting is much faster than simple sorting, the time complexity of simple sorting is O (n^2), and Hill (shell) sorts O (n (logn) ^2), and quick Sort is O (N*logn).Note: The following is an example of a small to large sort of an int
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