Study Notes(7)

1. If the command is executed incorrectly, the subsequent commands will continue to be executed. If the syntax is incorrect, the subsequent commands will not be executed.

2.bash -n only checks for syntax errors, not command errors

3. Use double quotes to keep the text format name=`cat /etc/fstab`, echo "$name"

4. name1=magename2=wang name3=$name1 echo $name3 name1=zhangsi echo $name3

5.type if can check whether it is a keyword

6. Ordinary variables are only valid in the current session, invalid in the child process, and invalid in other terminals. The variables defined by the subshell do not affect the variables of the same name defined by the parent shell.

7.echo $$ View the process ID of the current process echo $PPID View the parent process ID

8.pstree -p View process tree

9. Global variables that child processes cannot modify

10.export displays the environment variables of the system, you can also use declare to display the environment variables, declare -x name to create the environment variables, env can also display the environment variables

11.declare -x name=xixi You can also use declare -x to display system environment variables

12. The environment variables that come with the system are read in by reading the configuration file, so custom variables (including environment variables) can be canceled by exiting the current session, or canceled by unset

13.set displays all the variables of the system, and declare also displays all the variables of the system (environment variables and common variables)

14. Remember to unset the variables defined in the script so that the system can reclaim the occupied memory

15.lscpu display cpu information

16. Check the hard disk size lsblk, df can only query the partition size

17. /root/bin in $PATH can be used as the storage directory of the script, no need to use the path, this directory needs to be created manually

18.echo -e "\e[1:31m start backup ... \e[0m" output color font, echo -e "\e[1;31m start \e[0m"

#!/bin/bash

color_num=$[RANDOM%7+31]

echo "color_number :$color_num"

echo -e "\e[1;${color_num}mCOLOR \e[0m"

19.echo $SHLVL View shell nesting depth, which can be used to view the number of layers of the current shell (LVL: level)

20.echo $_ The last string of the previous command, you can use command $_ to execute the last parameter of the previous command

21. Read-only variables cannot be modified or deleted. The life cycle is the life cycle of the process. The environment variable is canceled after exiting the session.

22.declare -r displays the read-only variables of the system, declare -r name=zl(readonly name=zl)

23. To prevent modifying the system environment, you can use (umask 666; touch /data/f1), () is to open a subshell, which does not affect the umask value of the current system, and can also retain the original directory to execute

  (cd /data; rm -rf /data/*) The path does not switch after the execution is complete

24. You can view the usage instructions of () in man bash, note that you need to escape the symbol \ when searching for $ in bash

25.{ name=mage; echo $name; } Execute in the current shell environment, pay attention to spaces and semicolons, the commands in () can not use spaces, use {} must have spaces, such as { name=mge; echo $name }

26.x=1;echo $$;(echo $$;echo $x;x=2;echo $x);echo $x   

27. Multiple scripts pass parameters, and print the first parameter $*, $@ The difference is only when the "" sign is added

test2.sh

#!/bin/bash

echo "arg1 is $1"

test.sh

#!/bin/bash

echo "============================="

echo "arg1 is $1, all args is $*"

echo "============================="


echo "+++++++++++++++++++++++++++++"

/root/bin/test2.sh "$@"

echo "+++++++++++++++++++++++++++++"


# ./test.sh a b c

28.set -- clear all location variables, after the script is executed, clear the variables so that the system can reclaim memory

29. Create a soft connection for the executed script, which can be judged by $0 to perform different functions, the example given in the system: # ll /usr/sbin/pidof

30. The vim editor uses V to copy rows, Ctrl + v to copy columns

31.shift left shift position parameter, shift num can specify how many bits to shift left

32. Whether the execution of the script is successful is determined by the last command. If it is a syntax error, the subsequent commands cannot be executed. Even if it is correct, it will return non-zero.

33.exit can specify the exit code, use echo $? to view the return

34. The executed shell script is executed in the subprocess, which can be viewed using pstree

35.let z=x+y(let z=$x+$y),$[],declare -i The variables in the arithmetic operation do not need to add $

36.COLOR=$[RANDOM%7+31] ;color color font supplement

37.expr is the command expr 1+2 error, correct expr 1 + 2, expr 3 \* 2 multiplication symbols need to use \ escape

38.a=$[a^b]; b=$[a^b];a=$[a^b]; echo $a $b  a、b互换

39.help test You can view the types of conditional judgments, and it is also the help description of [ ], because test and [] are equivalent

40. It is recommended to use "" reference for the variables in [] to prevent one variable being empty and the other non-empty from causing an error

41.[ $# -ne 2 ] && echo "******" && exit 1 The last && Description

42.=~ Use extended expressions and use [[]], ! negate [[ $filename =~ .+\.sh ]]

43.-w Judging permissions is based on the actual permissions, not the file permissions settings, for the root (ll view) [ -w /etc/shadow ]

44. Use () or {} to change the priority in the comparison false ||{ cmd1; cmd2; },() opens a subshell, {} does not open a subshell, use {} to exit the script itself

45. [ ! $# -eq 2 ] Attention! s position

46. ​​Only when using regular expressions in the script, use [[]] to make judgments

47. The backspace key of the linux operating system is not easy to use, you can install the readline library # yum install readline-devel.*

48. The ls -d option will not recurse the subfolders under the directory when viewing all files in the current directory, such as: ls -d /etc/*/ displays all the folders under /etc, ls -d /etc/* displays All folders and files under the directory

49. The A option in ls excludes. and .. can be combined with grep to search for the number of subdirectories or files in the current directory (including hidden files), such as: ls -Al /etc/ |grep '^d'| wc -l

50.df -i View the inode utilization of the disk partition

51.if [[ $key =~ [Yy][Ee][Ss] ]] Be careful not to use double quotes when using regular expressions such as $key =~ "[Yy][Ee][Ss]", this species will report an error

52. Scan the newly added disk and identify it: echo '- - -' >/sys/class/scsi_host/host0/scan

53.-a File(-e File): Existence test, existence is true, otherwise false

53.-h File(-L File): exists and is a symbolic link file

54.-s File: exists and is not empty

55. The vi editor can save the specified line to another file: 3,8w /root/test1111, save the specified third line to the 8th line to the file test1111

   You can also read the command output, :r ls output the output of ls to the editor currently opened by vi, use: r /etc/fstab to read the contents of the file in /etc/fstab to the current editor

56. In the vim editor, ctrl + d flips half a screen to the end of the screen ctrl + u flips half a screen to the head of the screen

57. Use the D option in the vim editor to delete the cursor position to the end of the line


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324604363&siteId=291194637