shell编程基础(-)

频道:行业资讯 日期: 浏览:704

认识Shell

1.1概述

我们知道计算机是无法直接识别我们键入的指令的,在计算机语言里只有二进制的0和1,所有信息都将转化为由0和1组成的代码进行存储和传输。那么我们和计算机之间进行交互就需要有一个翻译官来翻译我们键入的指令以便计算机能够识别,在Linux系统中这个翻译官正是由Shell来担任的。简单来说,Shell就是用户与Linux内核之间的解释器,负责将用户的指令翻译为内核可以识别的指令。

1.2常见Shell解释器

常见的Shell解释器有:

/bin/bash

/bin/sh

/bin/csh

/bin/tcsh等等。

1.3查看Shell解释器

如何查看本机所支持的Shell解释器有哪些呢?

可以使用命令:cat /etc/shells

shell编程基础(-)

1.4修改Shell解释器

Linux系统中新创建的用户默认的登录Shell为/bin/bash,可以使用usermod或chsh命令修改用户的登录shell:

shell编程基础(-)

本系列文章使用的操作系统为CentOS7.5,Shell解释器为/bin/bash。

2. Shell脚本基础

2.1什么是Shell脚本?

简单的说就是将Linux或类UNIX系统的命令写入一个文件中,这个文件就是一个Shell脚本文件。

2.2 Shell脚本的书写格式

一个合格规范的脚本应该包含以下这些内容:

#!脚本声明:标明使用哪种解释器来解释该脚本的代码注释信息:标明该脚本编写的步骤、思路、用途等等其他信息,方便阅读脚本正式可执行指令语句:具体的Shell代码语句块

2.3编写第一个Shell脚本

#!/bin/bash#第一个Shell脚本,输入Hello Worldecho "Hello World"

2.4 Shell脚本的注释

注释的内容计算机将自动忽略,这部分内容是给阅读者看的。

单行注释:#第一行的#!为标识所使用的解释器

单行或多行注释:<<字符

字符

3.脚本文件的执行方式

3.1脚本文件没有可执行权限时

新创建的脚本文件,默认是没有可执行权限的,不可以直接执行,此时可以使用bash、sh、source、.来执行脚本文件。

[root@localhost shell]# chmod -x first.sh[root@localhost shell]# ls -l first.sh-rw-r--r--1 root root 122 Aug 3011:55 first.sh[root@localhost shell]# bash first.shHello World[root@localhost shell]# sh first.shHello World[root@localhost shell]# source first.shHello World[root@localhost shell]#. first.shHello World

上述各命令执行脚本的区别:

bash/sh:执行脚本时将会开启一个shhd子进程进行执行脚本命令

source/.:执行脚本时是直接在bash终端下执行的,没有开启子进程。

3.2脚本文件有可执行权限时

脚本文件分配了可执行权限后,可以直接使用绝对路径或相对路径来执行脚本文件。

[root@localhost shell]#./first.shHello World[root@localhost shell]#/opt/shell/first.shHello World

4.数据的输入与输出

4.1数据输出

4.1.1 echo 命令

功能描述:echo命令主要用来输出显示字符串信息。

语法格式:echo [选项]字符串

常用选项:

-n:不换行输出

-e:扩展输出,可以让echo命令识别\后面的转义符号含义。

[root@localhost shell]# echo "Hello World"#echo默认换行输出Hello World[root@localhost shell]# echo -n "Hello World"#添加-n选项不换行Hello World[root@localhost shell]# echo -e "\033[031mHello World\033[0m"# 使用-e选项输出带颜色的字符串Hello World#在第3行,第10列输出红色字体的Hello World[root@localhost shell]# echo -e "\033[31m\033[3;10H Hello World\033[0m"Hello World

4.1.2 printf 命令

功能描述:格式化输出数据,printf默认不换行输出

语法格式:printf [格式]参数(一般参数就是要输出的内容)

常用的格式字符串及功能描述如下表:

shell编程基础(-)

[root@localhost shell]# printf "%d"23#不换行输出数字23[root@localhost shell]# printf "%d\n"23# 换行输出数字23[root@localhost shell]# printf "%5d\n"23#换行输出数字并且宽度为5右对齐23[root@localhost shell]# printf "%-5d\n"23#换行输出数字并且宽度为5左对齐23[root@localhost shell]# printf "%-5s\n" aaa #左对齐输出字符串aaa [root@localhost shell]# printf "%5s\n" aaa #右对齐输出字符串 aaa[root@localhost shell]# printf "%3.2f\n"626.327237#输出浮点数,保留两位小数626.33[root@localhost shell]# printf "%.3f\n"626.327237#输出浮点数,保留三位小数626.327

4.2输入数据

4.2.1 read 命令

功能描述:可以从标准输入中读取一行数据

语法格式:read [选项][变量名]

默认变量名为REPLY。

常用选项:

-p:显示提示信息

-t:设置读入数据的超时时间

-n:设置读取n个字符后结束,默认会读取标准输入的一整行内容

-r:支持读取\,默认read命令理解\为特殊符号(转义字符)

-s:静默模式,不显示标准输入的内容

[root@localhost shell]# read key1aaa[root@localhost shell]# echo $key1aaa[root@localhost shell]# read key1 key2 key3112233[root@localhost shell]# echo $key111[root@localhost shell]# echo $key222[root@localhost shell]# echo $key333[root@localhost shell]# read -p "请输入账号:" uname请输入账号:test[root@localhost shell]# echo $unametest[root@localhost shell]# read -s -p "请输入密码:" passwd请输入密码:[root@localhost shell]# echo $passwd123456

5.输入与输出重定向

在Linux系统中输出可以分为标准输出和标准错误输出。标准输出的文件描述符为1,标准错误输出的描述符为2。而标准输入的文件描述符为0。

如果希望改变输出信息的方向,可以使用>或>>符号将输出信息重定向到文件中。

重定向符号与功能描述表

shell编程基础(-)

5.1输出重定向

[root@localhost shell]# echo "Hello the world"> test.txt[root@localhost shell]# cat test.txtHello the world[root@localhost shell]# echo "My Shell Scripts"> test.txt[root@localhost shell]# cat test.txtMy Shell Scripts[root@localhost shell]# echo "test file">> test.txt[root@localhost shell]# cat test.txtMy Shell Scriptstest file[root@localhost shell]# ls /etc/hosts /abc > test.txtls: cannot access /abc: No such file or directory[root@localhost shell]# cat test.txt/etc/hosts[root@localhost shell]# ls /etc/hosts /abc 2> test.txt/etc/hosts[root@localhost shell]# cat test.txtls: cannot access /abc: No such file or directory[root@localhost shell]# ls /etc/hosts /abc &> test.txt[root@localhost shell]# cat test.txtls: cannot access /abc: No such file or directory/etc/hosts[root@localhost shell]# ls /etc/hosts /abc > ok.txt 2> error.txt[root@localhost shell]# cat ok.txt/etc/hosts[root@localhost shell]# cat error.txtls: cannot access /abc: No such file or directory

另外,可以使用2>&1将错误输出重定向到标准输出,也可以使用1>&2将标准输出重定向到错误输出。

[root@localhost shell]# ls /abc > ok.txt 2>&1[root@localhost shell]# cat okcat: ok: No such file or directory[root@localhost shell]# echo "hello"2> error.txt 1>&2[root@localhost shell]# cat error.txtHello

Linux系统中有一个特殊的设备文件/dev/null,这是一个黑洞。无论往该文件中写入多少数据,都会被系统吞噬、丢弃。

[root@localhost shell]# echo "hello">/dev/null[root@localhost shell]# ls /abc 2>/dev/null[root@localhost shell]# ls /etc/hosts /abc &>/dev/null

5.2输入重定向

5.2.1< file 输入重定向

例:将/etc/hosts文件中的内容输入重定向到邮件内容中,发送给root用户

[root@localhost shell]# mail -s warning root@localhost N 1 root Mon Aug 3015:4719/769"warning"&1Message 1:From Mon Aug 3015:47:162021Return-Path:<>X-Original-To: root@localhostDelivered-To:: Mon,30 Aug 202115:47:16+0800To:: warningUser-Agent: Heirloom mailx 12.57/5/10Content-Type: text/plain; charset=us-asciiFrom:(root)Status: R127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6& exit

5.2.2<<输入重定向

<<符号(也称为Here Document)代表你需要的内容在这里。

[root@localhost shell]# vim second.sh#!/bin/bash# cat 通过Here Document读取数据,再通过输出重定向将数据导入文件cat >/opt/shell/test.txt << HERE该文件为测试文件。<<输入重定向测试。HERE[root@localhost shell]# chmod +x second.sh[root@localhost shell]#./second.sh[root@localhost shell]# cat test.txt该文件为测试文件。<<输入重定向测试。

shell编程基础(-)

0 留言

评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
验证码