Beginner LINUX Notes (10) Shell Scripts

simple scripting

Conversational script

Through the read command mentioned above, the input of the keyboard can be read, and the dialogue script can be implemented with this command.
insert image description here

date related script

insert image description here

Simple data addition, subtraction, multiplication and division

insert image description here

Differences in how scripts are executed

Different script execution methods will lead to different results, especially the environmental impact of bash.

direct execution

Use the sh script name to execute the script. It was also mentioned before that the variables defined in the script cannot be used in the bash environment.
Because the script is processed by direct execution, the system will give a new bash to run the script. After the script is executed, all data in the subroutine bash will be removed, so we cannot call the statement again in the bash environment. Variables.

source

Use source to make the script run in the parent program.

Make good use of judgment

test

insert image description here
The contents of the following table can also be obtained with the help command.

test sign meaning
Judgment of file name type
-e Does the filename exist?
-f Does the filename exist and is a file
-d Whether the filename exists and is a directory
-b Whether the filename exists and is a block device
File permission judgment
-r Does the filename exist and have "read" permission?
-w Does the filename exist and have "writable" permissions?
-x Does the filename exist and have "executable" permissions?
-u Does the filename exist and have the attribute "SUID"?
-g Does the filename exist and have the attribute "SGID"?
-k Does the filename exist and have the attribute of "Sticky bit"?
-s Whether the file name exists and is a "non-blank file"
Compare between two files
-nt (newer than) Determine whether file1 is newer than file2
-ot (older than) Determine whether file1 is older than file2

[ ]

The following points should be paid attention to when using:

  • Each component within square brackets [] needs to be separated by a space bar;
  • Variables in square brackets should preferably be enclosed in double quotation marks;
  • Constants in square brackets should preferably be enclosed in single or double quotation marks.

Default variables for shell scripts

  • $# : represents the "number" of the following parameters, the above table is shown as "4" here as an example;
  • $@ : means ""$1" "$2" "$3" "$4" ", each variable is independent (enclosed in double quotes);
  • $ * : Represents ""$1c$2c$3c$4", where c is the separator character, and the default is the space bar, so in this example it means ""$1 $2 $3 $4".
  • $0 : Represents the file name of the script to execute, followed by the rest of the numbers for the parameters when executing the script

conditional judgment

if then

here is similar to c

Single-level, simple conditional judgment statement

if [ 条件判断式 ]; then

fi

The fi here is not reversed, which means to end the conditional judgment of if.

Multiple and complex conditional judgments

similar

if [ 条件判断式一 ]; then

elif [ 条件判断式二 ]; then

else

fi

case esac

case $变量名称 in
	"第一个变量内容") <==每个变量内容建议用双引号括起来,关键词则为小括号 )
		程序段
		;; <==每个类别结尾使用两个连续的分号来处理!
	"第二个变量内容")
		程序段
		;;
	*) <==最后一个变量内容都会用 * 来代表所有其他值
		程序段
		exit 1
		;;
esac

function function

Similar to the function in C language, the difference is:

  • The execution mode of shell script is from top to bottom, from left to right, so the function setting in shell script must be at the front of the program. (C doesn't care about position as long as it is declared)

Of course, functions can also have parameters. The usage is similar to the parameter passing of shell scripts. Here is an example from Brother Bird's book:

function printit()
{
    
    
	echo "Your choice is ${1}" # 这个 $1 必须要参考底下指令的下达
}
echo "This program will print your selection !"
case ${
    
    1} in
	"one")
		printit 1 # 请注意, printit 指令后面还有接参数!
		;;
	"two")
		printit 2
		;;
	"three")
		printit 3
		;;
	*)
		echo "Usage ${0} {one|two|three}"
		;;
esac

Brother Bird's book puts function in conditional judgment. Personally, I don't feel that it is so rigorous, but it is not a big problem.

loop statement

while do done, until do done

One is while do done, which is the same as while in C.

while [ condition ]
do
	程序段落
done

The other is until do done, which is just the opposite of while.

until [ condition ]
do
	程序段落
done

for do done

for var in con1 con2 con3 ...
do
	程序段
done

Each time through the loop the value of var is replaced with the next state (con1 to conN)

for do done

This one is more like C than the previous one

for (( 初始值; 限制值; 执行步阶 ))
do
	程序段
done

Tracking and debugging of shell scripts

sh [-nvx] scripts.sh
Options effect
-n Don't execute the script, just query the syntax
-v Before executing script, output the content of scripts to the screen
-x Display the used script content to the screen, this is a useful parameter

Guess you like

Origin blog.csdn.net/qq_43550173/article/details/108940839