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 ] ,切记[]两侧空格
相关文章
|
6天前
|
Shell Linux Perl
Linux|如何允许 awk 使用 Shell 变量
Linux|如何允许 awk 使用 Shell 变量
14 2
|
6天前
|
Linux 芯片 Ubuntu
Linux驱动入门 —— 利用引脚号操作GPIO进行LED点灯
Linux驱动入门 —— 利用引脚号操作GPIO进行LED点灯
|
6天前
|
Ubuntu Linux
Linux驱动入门 —— 利用寄存器操作GPIO进行LED点灯-2
Linux驱动入门 —— 利用寄存器操作GPIO进行LED点灯
Linux驱动入门 —— 利用寄存器操作GPIO进行LED点灯-2
|
6天前
|
Linux 芯片
Linux驱动入门 —— 利用寄存器操作GPIO进行LED点灯-1
Linux驱动入门 —— 利用寄存器操作GPIO进行LED点灯
Linux驱动入门 —— 利用寄存器操作GPIO进行LED点灯-1
|
6天前
|
Linux C语言 Ubuntu
Linux驱动入门——编写第一个驱动
Linux驱动入门——编写第一个驱动
Linux驱动入门——编写第一个驱动
|
6天前
|
Linux C语言 调度
|
6天前
|
Linux API
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
|
6天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
12 0
|
6天前
|
缓存 安全 Linux
Linux入门基本指令(2)
Linux入门基本指令(2)
13 0
|
6天前
|
Linux Windows
Linux入门基本指令(1)-2
Linux入门基本指令(1)
12 1