


[Linux Learning] Shell Scripting Language
shell 是操作系统中“提供使用者使用界面”的软件,它包在 linux 内核的外面,为用户和内核之间的交互提供了一个接口,系统中的命令用 shell 去解释,shell 接收系统回应的输出并显示其到屏幕中。
1.shell简介
- 解释性语言——shell脚本、python,运行效率低,基本只适用企业内部
shell——脚本,记录系统命令及命令执行的系统关系,充当解释器
gcc ——编译器
vim ——编辑器
#!/bin/bash 幻数,指定解释器#!/usr/bin/env bash 自动匹配解释器
描述性语言——C语言、java,执行效率高
1.脚本的调用
脚本(一般以.sh结尾):
[root@desktop5 mnt]# vim westos.sh#!/bin/bash echo hello westos
方法一:无执行权限,用sh调用
[root@desktop5 mnt]# sh westos.sh
方法二:有执行权限,用绝对路径调用
[root@desktop5 mnt]# chmod +x westos.sh [root@desktop5 mnt]# /mnt/westos.sh
2.脚本的检查
+ 表示:执行动作
无+表示:动作输出
方法一:
[root@desktop5 mnt]# sh -x /mnt/westos.sh
方法二:
[root@desktop5 mnt]# vim westos.sh#!/bin/bash -xecho hello westos
实验一:快捷键F4执行填充
方法一:
[root@desktop5 mnt]# vim /etc/vimrc map <F4> ms:call WESTOS()<cr>'s ##ms:执行命令时,不提示报错function WESTOS() call append(0,"#################################") call append(1,"# Author : Hao #") call append(2,"# Mail : Hao@westos.com #") call append(3,"# Version : 1.0 #") call append(4,"# Create_Time: ".strftime("%Y-%m-%d")." #") ##时间更新 call append(5,"# Description: #") call append(6,"#################################") endfunction
方法二:利用.来承接后面的#
map <F4> ms:call WESTOS()<cr>'sfunction WESTOS() call append(0,"#################################") call append(1,"# Author : Hao".(" #")) call append(2,"# Mail : Hao@westos.com".(" #")) call append(3,"# Version : 1.0 ".(" #")) call append(4,"# Create_Time: ".strftime("%Y-%m-%d").(" #")) call append(5,"# Description: ".(" #")) call append(6,"#################################") endfunction
测试:
[root@desktop5 mnt]# vim westos.sh ##按‘F4’执行填充
实验二:执行新建以.sh结尾的vim文件时,自动填充
注意:旧文件不自动填充
方法一:
[root@desktop5 mnt]# vim /etc/vimrc autocmd BufNewFile *.sh exec ":call WESTOS()" ##新文件,以.sh结尾,执行,调用文件"map <F4> ms:call WESTOS()<cr>'s ##此行注释,在此"表注释function WESTOS() call append(0,"#################################") call append(1,"# Author : Hao #") call append(2,"# Mail : Hao@westos.com #") call append(3,"# Version : 1.0 #") call append(4,"# Create_Time: ".strftime("%Y-%m-%d")." #") call append(5,"# Description: #") call append(6,"#################################") call append(7,"") call append(8,"#!/bin/bash")endfunction
方法二:
[root@desktop5 mnt]# vim /etc/vimrc autocmd BufNewFile *.sh exec ":call WESTOS()""map <F4> ms:call WESTOS()<cr>'s function WESTOS() call append(0,"#################################") call append(1,"# Author : Hao".(" #")) call append(2,"# Mail : Hao@westos.com".(" #")) call append(3,"# Version : 1.0 ".(" #")) call append(4,"# Create_Time: ".strftime("%Y-%m-%d").(" #")) call append(5,"# Description: ".(" #")) call append(6,"#################################") call append(7,"") call append(8,"#!/bin/bash") endfunction
测试:
[root@desktop5 mnt]# vim file1.sh ##新建以.sh结尾的文件,自动填充
2.shell脚本练习
练习一:显示当前主机ip地址
[root@desktop5 mnt]# vim ip_show.sh#!/bin/bashifconfig eth0 | awk -F " " '/inet /{print $2}' ##inet所在行,以空格间隔,第二个字符
测试:
[root@desktop5 mnt]# sh ip_show.sh
练习二:显示当前主机中能登陆系统的用户
[root@desktop5 mnt]# vim user_show.sh#!/bin/bashawk -F : '/bash$/{print $1}' /etc/passwd ##以bash结尾,打印出第一个字符
测试:
练习三:执行命令后可清空日至
方法一:
[root@desktop5 mnt]# vim clear_log.sh#!/bin/bash> /var/log/messages
方法二:
[root@desktop5 mnt]# vim clear_log.sh#!/bin/bashecho "" > /var/log/messages
测试:
[root@desktop5 mnt]# chmod +x clear_log.sh [root@desktop5 mnt]# /mnt/clear_log.sh
【推荐课程:linux视频教程】
The above is the detailed content of [Linux Learning] Shell Scripting Language. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →
