Linux shell script sharing

Aug 20, 2019 pm 03:57 PM
linux

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
Copy after login

我们知道计算在执行指令的时候,是一条一条执行的。那么很好理解以上这 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
Copy after login

这个 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
Copy after login

这样就很好的解决了一个复杂的操作

有时候我们在管理文件的时候需要将空文件,没用的文件全删掉

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
Copy after login

针对于 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

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)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

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.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

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.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

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.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

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.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

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.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

See all articles