Linux Shell学习笔记(一)shell的编写与执行

echo命令

echo是用于终端打印的最基本命令
默认情况下 echo在每次调用后会添加一个换行符
命令格式:

echo [选项] [输出内容]

选项:

  • -e:支持反斜线控制的字符转换(见下表)
  • -n:取消输出后 行末的换行符号(内容输出后不换行)
    在这里插入图片描述
    echo输出时输出内容可以使用双引号 单引号 或者不用引号
    当字符串中包含特殊字符时双引号会解释特殊字符,单引号不会解释
    如 当字符串中有!号:
[root@linux01 ~]# echo "Hello World !" 
-bash: !": event not found
[root@linux01 ~]# echo 'Hello World !'
Hello World !
[root@linux01 ~]# echo Hello World ! 
Hello World !

echo -e演示:

[root@linux01 ~]# echo "Hello\tworld"
Hello\tworld
[root@linux01 ~]# echo -e  "Hello\tworld"  
Hello   world

echo -n演示:

[root@linux01 ~]# echo -n 'hello,word!'
hello,word![root@linux01 ~]# 

编写shell程序

shell脚本通常以shebang起始:#!/bin/bash
shebang这个词其实是两个字符名称(sharp-bang)的简写。在Unix的行话里,用sharp或hash(有时候是mesh)来
称呼字符“ # ”,用bang来称呼惊叹号“ ! ”,因而shebang合起来就代表了这两个字符。

shebang是一个文本行,其中 #! 位于解释器路径之前。/bin/bash是Bash的解释器命令路径。bash
将以 # 符号开头的行视为注释。脚本中只有第一行可以使用shebang来定义解释该脚本所使用的解
释器。
先写一个简单的Hello world

[root@linux01 sh]# vim sh1.sh
  1 #!/bin/bash
  2 echo Hello World!     
[root@linux01 sh]# sh sh1.sh 
Hello World!

运行shell脚本主要有两种方法

  1. 赋予执行权限,直接运行
 [root@linux01 sh]# ll
总用量 4
-rw-r--r--. 1 root root 30 5月  10 12:58 sh1.sh
[root@linux01 sh]# chmod 700 sh1.sh 
[root@linux01 sh]# ll
总用量 4
-rwx------. 1 root root 30 5月  10 12:58 sh1.sh
[root@linux01 sh]# ./sh1.sh 
Hello World!
  1. 通过Bash调用运行脚本,命令如下:
[root@linux01 sh]# bash sh1.sh 
Hello World!
[root@linux01 sh]# sh sh1.sh 
Hello World!

这种方法的意思是直接使用 Bash 去解释脚本中的内容,所以这个脚本也可以正常运行。使用这种方法运行脚本,甚至不需要脚本文件有"执行"权限,只要拥有"读"权限就可以运行了。
sh是一个shell。运行sh sh1.sh,表示使用sh来解释这个脚本;
如果直接运行./sh1.sh,首先你会查找脚本第一行是否指定了解释器,如果没指定,那么就用当前系统默认的shell(大多数linux默认是bash),如果指定了解释器,那么就将该脚本交给指定的解释器;

shell多命令执行

在 Bash 中,如果需要让多条命令顺序执行,则有这样方法,如表所示。
在这里插入图片描述

";“多命令顺序执行如果使用”;"连接多条命令,那么这些命令会一次执行,但是各命令之间没有任何逻辑关系,也就是说,不论哪条命令报错了,后面的命令仍会依次执行。举个例子:

 [root@linux01 sh]# ls ; date ; cd /user ; pwd
sh1.sh
2020年 05月 10日 星期日 13:12:00 CST
-bash: cd: /user: 没有那个文件或目录
/root/sh

&&逻辑与

如果使用"&&“连接多条命令,那么这些命令之间就有逻辑关系了。只有第一条命令正确执行了,”&&"连接的第二条命令才会执行。那么,命令 2 是如何知道命令 1 正确执行了呢?
这就需要 Bash 的预定义变量 $? 的支持了,如果 $? 返回值是 0,则证明上一条命令正确执行;如果 $? 返回值是非 0,则证明上一条命令执行错误。

[root@linux01 sh]# cp ./test1 /tmp/test1 && rm -rf ./test1 && echo 'ok'!
cp: 无法获取"./test1" 的文件状态(stat): 没有那个文件或目录
[root@linux01 sh]# touch test1 && echo "hello,java" > test1 && cat test1 && rm -f test1
hello,java
[root@linux01 sh]# ll
总用量 4
-rwx------. 1 root root 30 5月  10 12:58 sh1.sh

||逻辑或

如果使用"||"连接多条命令,则只有前一条命令执行错误,后一条命令才能执行。举个例子:

[root@linux01 sh]# cat file3 || touch file3
cat: file3: 没有那个文件或目录
[root@linux01 sh]# ll
总用量 4
-rw-r--r--. 1 root root  0 5月  10 13:20 file3
-rwx------. 1 root root 30 5月  10 12:58 sh1.sh

在Java中有三目运算符(): ()?()
在shell中我们可以用&&和||来实现这种三目运算:()&&()||()
判断某条命令是否成功执行:

[root@linux01 sh]# ls d2 && echo "ok" || echo "err"
ls: 无法访问d2: 没有那个文件或目录
err
[root@linux01 sh]# ll  && echo "ok" || echo "err"
总用量 4
-rw-r--r--. 1 root root  0 5月  10 13:20 file3
-rwx------. 1 root root 30 5月  10 12:58 sh1.sh
ok
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值