Linux从入门到精通(十二)——shell编程 中

简介: Linux从入门到精通(十二)——shell编程 中

4. shell基本语法

在shell中使用read命令从标准输入读入数据, 并将该数据赋值给变量;使用echo命令实现换行标 准输出操作。

在进行计算整数变量值时,可以使用expr表达式,let命令,$()形式和$[]形式实现。

test是Shell程序中的一个表达式,通过和Shell 提供的if等条件语句相结合可以方便地测试字符串、 文件状态和数字。

4.1 read 命令

read命令可以通过键盘输入为变量赋值:

read[参数]变量名...

-p后面跟提示信息,即在输入前打印提示信息。

-n后跟一个数字,指定输入文本的长度,当输入的字符数目达到预定数目时,自动退出,并将输入的数据赋值给变量。

-S输入字符时不在屏幕上显示

read读入的变量可以有多个,第一个数据给第一个变量,第二个数据给第二个变量,如果输入数据个数过多,则最后所有的值都给最后一个变量。

[root@VM-24-17-centos shellstudy]# read a b c
1 2 3 4 5
[root@VM-24-17-centos shellstudy]# echo ${c}
3 4 5

【例子】

实例程序read.sh

[root@VM-24-17-centos shellstudy]# vim read.sh
#!/bin/bash
read -p 'please input three numbers:' first second third
echo "${first},${second},${third}"
read -n1 -p 'please input y or n:' ans
echo # 换行
echo ${ans}
unset first second third ans
[root@VM-24-17-centos shellstudy]# chmod u+x read.sh 
[root@VM-24-17-centos shellstudy]# ./read.sh 
please input three numbers:1 2 3
1,2,3
please input y or n:y
y

4.2 echo 显示命令

Shell使用echo命令实现标准输出,在屏幕上打印出指定的字符串:

echo [选项] [字符串]

-n:不在最后自动换行。

-e:启用反斜线控制字符的转换

-E:不处理转义字符。此为缺省(默认)选项;

echo命令的转义符(echotest.sh):

image.png

【例子】

[root@VM-24-17-centos shellstudy]# vim echotest.sh
#!/bin/bash
echo -e "this \nis \na \ntest"
echo -E "this \nis \na \ntest"
echo "this \nis \na \ntest"
echo -n "this is a test"
echo ",bye"
echo -e "this is a \ctest"
[root@VM-24-17-centos shellstudy]# chmod u+x echotest.sh 
[root@VM-24-17-centos shellstudy]# ./echotest.sh 
this 
is 
a 
test
this \nis \na \ntest
this \nis \na \ntest
this is a test,bye
this is a 

4.3 数值计算

对数字型变量计算可以使用如下方法:

  1. 用法 1:$((...))
A=$((3+4))

用法 2:$[..]

B=$[3+5]

用法3:expr表达式:

C=`expr 5 + 4` #  注意运算符两侧必须有空格,否则出错

用法4:let指令,计算并赋值:

let D=5+5

【例子】

[root@VM-24-17-centos shellstudy]# vim com.sh
#!/bin/bash
A=$((3+4))
B=$[3+5]
C=`expr 5 + 4`
let D=5+5
echo "${A},${B},${C},${D}"
E=$((3+$((4+$((5+5))))))
echo ${E}
let E++
echo ${E}
unset A B C D E
[root@VM-24-17-centos shellstudy]# chmod u+x com.sh 
[root@VM-24-17-centos shellstudy]# ./com.sh 
7,8,9,10
17
18

4.4 变量表达式测试

test命令在Shell脚本程序中主要用于测试一个表达式;如果条件为真,则返回一个0值。如果表达式不为真,则返回一个大于0的值——也可以将其称为假值。其语法如下:

test 表达式

表达式所代表的操作符有字符串操作符、数字操作符、逻辑操作符以及文件操作符。

4.4.1 字符串测试

image.png

【例子】

[root@VM-24-17-centos shellstudy]# vim strtest.sh
#!/bin/bash
echo -n "Enter your login name: "
read name
if test "${name}" == "root"; # 注意==两边的空格
then    
        echo "hello,${name},How are you today?"
else    
        echo "name error,so who are you?"
fi 
[root@VM-24-17-centos shellstudy]# chmod u+x strtest.sh
[root@VM-24-17-centos shellstudy]# ./strtest.sh 
Enter your login name: root
hello,root,How are you today?

上面这种test…形式写起来比较复杂,通常使用下面的方式:

if [ "${name}" = "root" ]; # 同样要注意[]两侧的空格

4.4.2 数值测试

image.png

【例子】

[root@VM-24-17-centos shellstudy]# vim inttest.sh
#!/bin/bash
if [ $# -ne 2 ]; then
        echo "not enough parameter"
        exit 0
fi      
if [ $1 -eq $2 ]; then
        echo "$1 equals $2"
elif [ $1 -lt $2 ]; then
        echo "$1 littler than $2"
elif [ $1 -gt $2 ]; then
        echo "$1 greater than $2"
fi   
[root@VM-24-17-centos shellstudy]# chmod u+x inttest.sh 
[root@VM-24-17-centos shellstudy]# ./inttest.sh
not enough parameter
[root@VM-24-17-centos shellstudy]# ./inttest.sh 2 3
2 littler than 3
[root@VM-24-17-centos shellstudy]# ./inttest.sh 2 2
2 equals 2
[root@VM-24-17-centos shellstudy]# ./inttest.sh 3 2
3 greater than 2

4.4.3 文件测试

image.png

【例子】

[root@VM-24-17-centos shellstudy]# vim filetest.sh
#!/bin/bash
echo "please input a file name:"
read file_name
if [ -d $file_name ]; then
        echo "${file_name} is a directory"
elif [ -f $file_name ]; then
        echo "${file_name} is a common file"
elif [ -c $file_name ];then
        echo "${file_name} is a device file"
else    
        echo "${file_name} is an unknown file"
fi 
[root@VM-24-17-centos shellstudy]# chmod u+x filetest.sh
[root@VM-24-17-centos shellstudy]# ./filetest.sh 
please input a file name:
/etc
/etc is a directory

4.4.4 逻辑测试

image.png

变量测试语句一般不单独使用,一般作为if语句的测试条件,如:

if test -d $1;then
  ...
fi
# 变量测试语句可用[]进行简化,如
test -d $1 等价于 [ -d $1 ] ,切记[]两侧空格
相关文章
|
算法 Linux Shell
Linux实用技能:打包压缩、热键、Shell与权限管理
本文详解Linux打包压缩技巧、常用命令与原理,涵盖.zip与.tgz格式操作、跨系统传文件方法、Shell运行机制及权限管理,助你高效使用Linux系统。
Linux实用技能:打包压缩、热键、Shell与权限管理
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
1215 9
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
1281 2
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
1647 1
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
2712 25
|
安全 算法 Ubuntu
Linux(openssl)环境:编程控制让证书自签的技巧。
总结:在Linux环境中,OpenSSL是一个非常实用的工具,可以帮助我们轻松地生成自签名证书。通过上述三个简单步骤,即可为内部网络、测试环境或开发环境创建自签名证书。但在公共访问场景下,建议购买经过权威认证机构签发的证书,以避免安全警告。
861 13
|
Linux Shell
shell_42:Linux参数移动
总的来说,参数移动是Linux shell脚本中的一个重要概念,掌握它可以帮助我们更好地处理和管理脚本中的参数。希望这个解释能帮助你理解和使用参数移动。
544 18
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
596 4
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
869 3
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。