Shell入门(三)之Shell echo、printf、test命令

Shell入门(三)之Shell echo、printf、test命令

Shell echo命令

显示普通字符串

echo "hello"

输出:hello

这里的双引号完全可以省略,如下:

echo hello

输出:hello

显示转义字符

echo "\"hello\""
或者
echo \"hello\"

输出:”hello”

显示变量

name="tom"
echo "$name is a child"

输出:tom is a child

显示换行

# -e:开启转义
echo -e "hello \nworld"

输出:

hello
world

显示结果定向至文件

echo "hello world" > myfile.txt

执行cat myfile.txt命令,输出

hello world

显示日期

echo `date`

输出:Tue Jan 16 06:30:53 PST 2018


Shell printf 命令

Shell printf命令模仿C程序库(library)里面的printf()程序。

printfPOSIX标准所定义,因此使用printf的脚本比较使用echo移植性好。

printf的语法:

printf format-string [arguments...]
  • format-string:为格式控制字符串
  • arguments:为参数列表

eg:

[zhang@localhost ~]$ echo "hello,shell"
hello,shell

# 如果没有换行符,不换行
[zhang@localhost ~]$ printf "hello world"
hello world[zhang@localhost ~]$ 

# 使用换行符
[zhang@localhost ~]$ printf "hello,shell\n"
hello,shell

printf占位功能

eg:

#!/bin/bash

printf "%-10s %-8s %-4s\n" name sex weight
printf "%-10s %-8s %-4.2f\n" zhangsan male 68.22
printf "%-10s %-8s %-4.2f\n" lisi male 70.02
printf "%-10s %-8s %-.2f\n" xiaolongnv female 48.12

输出:

name       sex      weight
zhangsan   male     68.22
lisi       male     70.02
xiaolongnv female   48.12

占位符有:%s,%d,%c,%f。

%-10s:表示宽度为10个字符,-表示左对齐,没有表示右对齐。不足自动填充空格。超过也会将内容全部显示出来。

%-4.2f:表示格式化小数,其中.2表示保留2位小数。

%d:格式化数字


Shell test 命令

Shell中的test命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

数值测试

参数说明
-eq等于则为真
-ne不等于则为真
-gt大于则为真
-ge大于等于则为真
-lt小于则为真
-le小于等于则为真

eg:

#!/bin/bash

num1=100
num2=200

if test $num1 -eq $num2
then
    echo "$num1 equal $num2"
else
    echo "$num1 not equal $num2"
fi

结果:

100 not equal 200

字符串测试

参数说明
=等于则为真
!=不相等则为真
-z字符串长度为0,则为真
-n字符串的长度不为0,则为真

eg:

#!/bin/bash

str1="hello"
str2="world"

if test $str1 = $str2
then
    echo "$str1 equal $str2"
else
    echo "$str1 not equal $str2"
fi


if test -z $str1
then
    echo "$str1 is empty"
else
    echo "$str1 is not empty"
fi

输出结果:

hello not equal world
hello is not empty

文件测试

参数说明
-e 如果文件存在,则为真
-r 如果文件存在且可读,则为真
-w 如果文件存在且可写,则为真
-x 如果文件存在且可执行,则为真
-s 如果文件存在且至少有一个字符,则为真
-d 如果文件存在且为目录,则为真
-f 如果文件存在且为文件,则为真
-c 如果文件存在且为字符串特殊文件,则为真
-b 如果文件存在且为块特殊文件,则为真

eg:

在zhang用户目录下,执行ls -l命令:

image

执行test -e <filename>命令:

[zhang@localhost ~]$ test -e 1.sh
[zhang@localhost ~]$ if test -e 1.sh
> then
>     echo "1.sh file exist"
> else
>     echo "1.sh file not exist"
> fi
1.sh file exist

执行test -r <filename>命令:

[zhang@localhost ~]$ if test -r 1.sh
> then
>     echo "1.sh read ok"
> else
>     echo "1.sh read not ok"
> fi
1.sh read ok

执行test -x <filename>命令:

[zhang@localhost ~]$ if test -x myfile.txt
> then 
>     echo "myfile.txt is execute file"
> else
>     echo "myfile.txt is not execute file"
> fi
myfile.txt is not execute file

执行test -d <filename>命令:

[zhang@localhost ~]$ if test -d test
> then
>     echo "test is a dir"
> else
>     echo "test is not a dir"
> fi
test is a dir
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值