Shell echo\test\输入输出重定向\文件包含

该文详细介绍了LinuxShell脚本中的输入输出重定向,包括如何将命令输出重定向到文件以及追加内容,如何使用变量、转义字符以及字符串和数值的测试。此外,还讲解了文件的权限设置和文件包含的概念,便于模块化编程。
摘要由CSDN通过智能技术生成

Shell echo\test\输入输出重定向\文件包含

echo

进入测试目录,新建一个文件,写入内容,注意要赋予文件可执行的权限。

显示普通字符串

[root@localhost test]# cat t.sh
echo "it is a test"
echo it is a test
[root@localhost test]# ./t.sh
it is a test
it is a test

双引号可以省略

显示转义字符

[root@localhost test]# cat t.sh
echo "\"it is a test\""
echo it is a test
[root@localhost test]# ./t.sh
"it is a test"
it is a test

同样双引号可以省略

显示变量

[root@localhost test]# cat t.sh
#read从标准输入读取一行并把读取输入行的每个字段制定给shell变量
read name
echo "hey ${name}"
[root@localhost test]# ./t.sh
jojo
hey jojo

显示换行\不换行

[root@localhost test]# cat t.sh
#换行
echo -e "jojo \n dio"
#不换行
echo -e "jojo \c"
echo "dio"
[root@localhost test]# ./t.sh
jojo 
 dio
jojo dio

-e开启转义

将显示结果重定向至文件

[root@localhost test]# touch f.txt #新建文件
[root@localhost test]# echo "hello shell" > f.txt
[root@localhost test]# cat f.txt
hello shell

原样输出字符,不进行转义或取变量(单引号)

[root@localhost test]# cat t.sh
name="jojo"
echo '$name'

[root@localhost test]# ./t.sh
$name

显示命令执行结果

[root@localhost test]# echo `date`
2023年 04月 05日 星期三 09:59:22 CST

test

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

数值测试

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

[root@localhost test]# cat t.sh
a=100
b=120
if test $[a] -eq $[b]
then 
  echo '两数相等'
else
  echo '两数不等'
fi

[root@localhost test]# ./t.sh
两数不等

字符串测试

=    等于则为真
!=    不相等则为真
-z 字符串    字符串的长度为零则为真
-n 字符串    字符串的长度不为零则为真
[root@localhost test]# cat t.sh
a="test"
b="test"
if test $[a] = $[b]
then 
  echo '字符串相等'
else
  echo '字符串不等'
fi

[root@localhost test]# ./t.sh
字符串相等

文件测试

-e 文件名	如果文件存在则为真
-r 文件名	如果文件存在且可读则为真
-w 文件名	如果文件存在且可写则为真
-x 文件名	如果文件存在且可执行则为真
-s 文件名	如果文件存在且至少有一个字符则为真
-d 文件名	如果文件存在且为目录则为真
-f 文件名	如果文件存在且为普通文件则为真
-c 文件名	如果文件存在且为字符型特殊文件则为真
-b 文件名	如果文件存在且为块特殊文件则为真
[root@localhost test]# vi t.sh
[root@localhost test]# cat t.sh

if test -e ./bash
then 
  echo '文件存在'
else
  echo '文件不存在'
fi

[root@localhost test]# ./t.sh
文件不存在
[root@localhost test]# ll
总用量 8
-rw-r--r--. 1 root root 12 45 09:54 f.txt
-rwxr-xr-x. 1 root root 81 45 18:16 t.sh

输入输出重定向

  • command > file 将输出重定向到 file。

  • command < file 将输入重定向到 file。

  • command >> file 将输出以追加的方式重定向到 file。

  • n > file 将文件描述符为 n 的文件重定向到 file。

  • n >> file 将文件描述符为 n 的文件以追加的方式重定向到 file。

  • n >& m 将输出文件 m 和 n 合并。

  • n <& m 将输入文件 m 和 n 合并。

  • << tag 将开始标记 tag 和结束标记 tag 之间的内容作为输入。

输出重定向

[root@localhost test]# ll > f.txt
[root@localhost test]# cat f.txt
总用量 4
-rw-r--r--. 1 root root  0 45 19:02 f.txt
-rwxr-xr-x. 1 root root 81 45 18:16 t.sh

将ll命令输出的内容重定向到f.txt文件中,重定向输出会覆盖内容,如果你不希望被覆盖,请使用>>符号追加到文件末尾

[root@localhost test]# echo hello shell >> f.txt
[root@localhost test]# cat f.txt
总用量 4
-rw-r--r--. 1 root root  0 45 19:02 f.txt
-rwxr-xr-x. 1 root root 81 45 18:16 t.sh
hello shell

输入重定向

[root@localhost test]# wc -l f.txt
4 f.txt
[root@localhost test]# wc -l < f.txt
4
[root@localhost test]# cat f.txt
总用量 4
-rw-r--r--. 1 root root  0 45 19:02 f.txt
-rwxr-xr-x. 1 root root 81 45 18:16 t.sh
hello shell

这里,统计f.txt文件的行数 输入重定向到f.txt文件

文件包含

shell语言也可以包含外部脚本,分模块化编程,很方便的封装一些公用代码作为一个独立的文件。

创建两个.sh文件:

[root@localhost test]# touch t1.sh
[root@localhost test]# touch t2.sh

t1和t2内容:

[root@localhost test]# cat t1.sh
url="www.baidu.com"

[root@localhost test]# cat t2.sh
. ./t1.sh  #引用t1文件
echo "全球最大的中文搜索引擎网站 : $url"

赋予t2.sh执行的权限,并执行:

[root@localhost test]# chmod +x t2.sh
[root@localhost test]# ll t2.sh
-rwxr-xr-x. 1 root root 82 45 19:25 t2.sh
[root@localhost test]# ./t2.sh
全球最大的中文搜索引擎网站 : www.baidu.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值