-------------------------------------------------- --hello world---------------------------------------------
linux is created as follows File vim hello
#! /bin/bash //Tell shell which shell program to use
#Display a line //# indicates a comment
//Blank line is used to distinguish different functions without practical meaning
name="[email protected]"
echo " Hello World" //echo passes the following parameters to standard output
Output : Hello World
-------------------------------- --------------------hello world---------------------------- ---------------
----------------------------------- ------------------------About Variables----------------------- -------------------------
Create variables directly in the script as follows, and use the variable
log="[email protected]" // Note that there can be no spaces on either side of the equal sign
echo $name //Use $ to get the value in the variable result output [email protected]
echo " this is the \&name" //result output this is the $name
echo "this is the $name" //result output this is the [email protected]
echo "this is the ${name}email" //the output result this is the [email protected] here {} the user defines the start and end of a variable
source hello //here hello is above After execution, you can call the value of the name variable in the parent shell environment
echo $name
export count=5 //Indicates that the script affects the child Shell environment (that is, the current user's Shell), and outputs the variable count
bash // Start the child shell
echo $count //Display the value of the variable in the subshell
exit //Exit the subshell
unset count //Indicates to delete a variable
num1=1+2
num2=$[1+2] //Similarly 1 can be replaced with a variable $ num If $[] is not added, it will output 1 (here 1 is the value of $num) + 2, not 3
echo $num1 $num2 //Output 1+2 3 , so you need to use $[] for operations with variables
You can also use let num2 = $num2 +1
2#10: represents binary 10, 16#10: represents hexadecimal 10
expr 1 + 2 //The output result is 3, instead of $[], pay attention to the middle of 1 + 2 There must be spaces, otherwise it will be output as a string
//You can also refer to let
----------------------------------- ------------------------About Variables----------------------- -------------------------
------------------------- ----------------------------------About incoming array parameter variables---------- ---------------------------------------
$1 means the first parameter $2 means the second Parameter $3 indicates the third parameter
$0 indicates the name of the script
$* indicates the parameter list
$@ Same as above
$# Number of parameters
Example for loop and array parameter
for file in #@ //file is a variable, the array parameter
do passed from the loop
ls -l $file
done
-------------------------------------------- ---------------About incoming array parameter variables---------------------------- --------------------
------------------------------ -----------------------------About the quotation mark rule------------------- -------------------------------
echo " this is the $name" // double quotes explain $
echo 'this is the $name' //Single quotation marks do not interpret $, the output is this is the $name
echo "today is `date " //When the quotation marks will run the command in it, the date will be executed as a command
------ -------------------------------------------------- ---About the quotation mark rule-------------------------------------------- ---
----------------------------------------------- ------------Judgment statement------------------------------------ --------------------
#! /bin/bash
read password //The read command is used to request a user input and assign the value to password
if [ "$password" = "12333"] // Determine whether the value entered by the user is equal to 12333. Here [ is a command, so the equal sign , parameters, with spaces before and after
//Explanation of the judgment parameter of if: The judgment statement of if only accepts the program as a parameter. This is why [ is used to contain "$password" = "123333"
//Because [ is actually a command, you can replace it with test: if test $password = "12333"
//There is a problem here, because test and [ are commands, so each parameter and operator = need to be separated by spaces. This is also the reason why there can be no spaces when assigning variables
//The judgment parameter here can be a script because the script is also a program. Return 0 for true, non-0 for false
then
echo "you are in"
fi //
other formats of if statement indicating the end of the statement block
if judgment
then
Command
elif judgment
then
Command
else
command
fi
case multiple choice structure
case $1 in
start) //Determine whether $1 is equal to start
Order
;; //This means the end of the exit. And the command is a case must amount
stop)
Order
;;
*) //The wildcard is used, which is equivalent to executing if there is no match as above
Order
;;
esac //Indicates the end of the case
Now there are 3 types of judgments: A: String judgment B: File test C: Number comparison Each judgment provides a variety of attributes for reference p344
exit $@ //exit means exit and return A value can be either exit 0 or exit $@
---------------------------------------- -------------------Judgment statement----------------------------- ---------------------------
----------------------- ------------------------------------loop statement------------ ---------------------------------------------
while phantom statement
while test $number -le 100 // If the value of numbe is less than or equal to 100, continue to run
do
Order
done
until judgment statement //This judgment statement is to exit when it is true
do
Order
done
for i in 1 2 3 // after in is the parameter array separated by spaces,
do
echo $i
done
seq command detailed
seq 9: will generate a parameter array from 1-9
for i `seq 9` //Note the role of quotation marks here
do
echo $i
done
-------------------------------------------------------- -------------loop statement----------------------------------- ----------------------
---------------------------- -------------------------------Some commonly used commands--------------- -------------------------------
read: Receive a line of information from standard input, if no variable name is provided, the information will be Put it in the variable REPLY. When using Ctrl+D\
read, multiple input data can be stored in multiple variables, where the input is separated by spaces. For example: read first second
When a process runs successfully, it will always think of the parent process The return value is 0 (indicates normal), and other values indicate abnormal
exit: forcibly exit a script, and the process that wants to call the script returns an integer value
trap: used to capture signals, for example as follows
#! /bin/bash
trap `echo "type quite to exit"` INT //The meaning of this sentence is to print "type quite to exit" after capturing the INT signal (ie Ctrl+Z).
----------------- ------------------------------------------Some commonly used commands---- ------------------------------------------