[Linux study notes 29] shell script basics

1. What is a shell


Shell is a program written in C language, which is a bridge for users to use Linux. Shell is both a command language and a programming language. Shell refers to an application program that provides an interface through which users access the services of the operating system kernel. Ken Thompson's sh is the first Unix Shell, and Windows Explorer is a typical graphical shell


Shell script (shell script) is a script program written for the shell


2. The meaning of shell scripts


  1. Record the execution process and execution logic of the command for repeated execution in the future
  2. Script can process hosts in batch
  3. The script can process the host regularly

3. Create a shell script


#! : Is an agreed mark, which tells the system which interpreter the script needs to execute, that is, which kind of Shell is used.

#!/bin/bash: Magic number (this script uses /bin/bash to explain and execute)


Automatically add the script header :vim ~/.vimrc

autocmd FileType yaml setlocal ai ts=2 sw=2 et
autocmd FileType sh set ai ts=4
autocmd BufNewFile *.sh,*.script call WESTOS()
"map <F4> ms:call WESTOS()<cr>'s

func WESTOS()
		call append(0,"###########################")
		call append(1,"##	Author:	zy")
		call append(2,"##	Version:")
		call append(3,"##	Create_Time:	".strftime("%Y/%m/%d"))
		call append(4,"##	Mail:	[email protected]")
		call append(5,"##	Info:	")
		call append(6,"###########################")
		call append(7,"")
		call append(8,"#!/bin/bash")
endfunc

Test : Create a new file test.sh, with the extension sh (sh stands for shell), and automatically generate the header

Insert picture description here



4. Run the shell script


Operation method 1:

  1. Manually start the specified interpreter in the environment
  2. sh 脚本文件:run

Insert picture description here

Operation method 2:

Run the commands in the shell directly in the current environment (do not open a new shell)

source 脚本文件

. 脚本文件

Insert picture description here


Run method 3:


Open the shell specified in the script and use this shell environment to run the commands in the script

( No need to specify interpreter information in the first line )

  1. chmod +x 脚本文件名: Give execution permission

  2. /脚本文件名Or . /脚本文件名: execute

Insert picture description here
Insert picture description here



5. Debug shell script


sh command
-c string
The command reads from the string after -c
-i
Realize script interaction
-n
Check the syntax of the shell script
-x
Realize the tracking of shell script statement by statement

sh -x 脚本文件: Show the script execution process

+	#运行指令
不带+	#命令运行的输出

Insert picture description here



6. Script exercises


Exercise 1:
Run'ip_show.sh network card' to display the current IP


ifconfig $1 | awk '/inet\>/{print $2}'

Insert picture description here


Exercise 2:
host_messages.sh

  1. Display the current host name, ip login to the current host
  2. 格式:
    hostname: XXXX
    ipaddress: XXXX.XXXX.XXXX.XXXX
    username: root

echo "hostname:	$HOSTNAME"
echo "ipaddress:	$(ifconfig 网卡 | awk '/inet\>/{print $2}')"
echo "username:	$USER"

Insert picture description here

Exercise three:
clear_log.sh
can clear the log after executing this script


#检测是否是root用户执行此脚本
[ "$USER" = "root" ] || {
    
    
	echo -e "\033[31m Error: Please run script with root ! \033[0m"
	exit 1
	}
#清空日志
LOGS=`sed -n '/RULES/,$p' /etc/rsyslog.conf | awk '!/^#|^$/&&/var/{print $2}' | sed 's/-//g'`

for LOG in $LOGS
do
	> $LOG && echo -e "\033[32m $LOG is cleared ! \033[0m"
done

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46069582/article/details/111543159