Linux shell 脚本分享
Shell 是一个命令解释器,处于内核和用户之间,负责把用户的指令传递给内核并且把执行结果回显给用户,Shell 它是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁,同时,Shell 也可以作为一门强大的编程语言
推荐:【Linux视频教程】
理论
我们常说的 Shell 是命令行解释器的意思,也有内核上的一层壳的含义,Shell 常见的几种类型有 sh、csh、bash、zsh 等提供的脚本支持.
Shell 诞生于 Unix,是与 Unix/Linux 交互的脚本编程,单独地学习 Shell 是没有意义的,有 Linux 基础的同学应该知道,Linux 都默认集成了 Shell 环境,Shell 的功能点在于外部命令的使用.
其内部数据处理方面,侧重文本(或者说字符串,或字符流)处理,而对运算和其他基本数据结构的原生较弱(例如常用的 array、set、map 等等), 功能目标在于实现命令自动化.
但是呢,实现命令自动化的同时,Shell 需要依赖其他程序才能完成大部分的工作,这是它的缺陷也是一个优点吧,但它不容置疑的长处是:简洁的脚本语言标记方式,而且比 C 语言编写的程序执行更快、更有效率.
所以我们如果有做运维甚至是基础管理 Linux 服务器的朋友,只要学会了 Shell 编程,大大的可以节约我们的时间,更快捷的管理我们的服务器了.
Bash 是 Linux 标准默认的 Shell,是 BourneAgain Shell 的缩写,因为 bash 有很多特性,比如:可以使用类似 DOS 下面的 doskey 的功能,用方向键查阅和快速输入并修改命令。自动通过查找匹配的方式给出以某字符串开头的命令
还有就是 bash 包含了自身的帮助功能,你只要在提示符下面键入 help 就可以得到相关的帮助,在 Linux 系统中我们经常看见以:#!/bin/bash 开头的文件内容,这就是声明了该文件是一个基于 bash 的 Shell 编程文件
干货
有这么一个程序 :
#!/bin/bash cd /home/liwei/mongodb/bin ./mongod --dbpath=../liwei_db
我们知道计算在执行指令的时候,是一条一条执行的。那么很好理解以上这 3 行。依次操作的目的是:进入 mongodb/bin 目录,接着执行启动 mongodb 数据,保存为 m.sh, 这样,只要我们在 Linux 终端执行一条:sh m.sh,回车,计算就会依次执行指令,达到启动 mongodb 数据库的功能.
那么要执行更多的指令,可以么?回答肯定是可以,如果我们需要执行上百行指令,单独执行的话,我们要重复操作,那么把它们依次写入一个 Shell,这样来执行的时候就可以很方便了,执行 Shell 的过程中,我们可以通过 > 或者 >> 来记录下一些节点的结果报告.
比如这么一个 shell:
#!/bin/bash cd /lamp ls *.tar.gz > ls.list for TAR in `cat ls.list` do tar -zxf $TAR done
这个 Shell 要解决的实际问题是将 lamp 目录里面所有的压缩包,批量解压出来,这样我们可以看出 ls 的结果集可以拿出来参与下一个指令的执行
那么我们现在有个需求就是将某个文件夹下所有的文件名字里的大写字母改成小写字母,如果要人工一个个改的话,但是找文件就够眼花蛋疼了,这个时候 Shell 就发挥了强大的力量了
#!/bin/bash for file in `ls | grep '[A-Z]'` do str=`echo $file|tr 'A-Z' 'a-z'` mv $file $str
这样就很好的解决了一个复杂的操作
有时候我们在管理文件的时候需要将空文件,没用的文件全删掉
for filename in `ls` do if test -d $filename then b=0 else a=$(ls -l $filename | awk '{ print $5 }') if test $a -eq 0 then rm $filename fi fi done
针对于 Shell 编程,远远不止我给大家分享的这些,还有很多强大的地方,大家慢慢的挖掘吧。
The above is the detailed content of Linux shell script sharing. For more information, please follow other related articles on the PHP Chinese website!

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools