shell基本教程

  1. 框架
第一行指定shell的类型,一般为Bourne shell,具体为#!/bin/bash,也就是是shebang
剩下的为具体的命令,顺序执行这些命令
  • 参考自:https://www.w3cschool.cn/shell_tutorial/what_is_she.html
  • https://www.lxlinux.net/4242.html
  1. 三个符号< << <<<
  • 参考自:https://blog.csdn.net/weixin_43025343/article/details/132537234
  • < 输入重定向,箭头指向的是输入的目的地
    1. 将文件的内容传递给cat命令
    cat < data.txt
    
    1. 将文件的内容传递给wc命令
    wc < data.txt
    
  • << 用于支持多行输入
    $ cat << EOF > message.txt
    > Hello, this is a custom message!
    > It spans multiple lines.
    > Have a great day!
    > EOF
    
    cat > message.txt // cat读取的内容输入到message.txt文件中,再在中间插入EOF作为输入的结束就变成了
    cat << EOF > message.txt
    
  • <<< 单行字符串输入
    $ grep "keyword" <<< "This is an example text containing the keyword."
    
  1. /dev/null 与标准输出和标准错误输出
  • 参考自:https://baijiahao.baidu.com/s?id=1730629989727646104&wfr=spider&for=pc
  • linux中一切皆文件,设备也是被当做文件,进行访问,/dev就是包含设备的目录
/dev下有null这个文件
[root()@xxxx dev]# ls
autofs         console          dm-1   dm-16  dm-8  hpet       log           memory_bandwidth    port    rtc0  sg0       stdin   tty12  tty19  tty25  tty31  tty38  tty44  tty50  tty57  tty63  ttyS3    vcs1  vcsa1  vga_arbiter
block          core             dm-10  dm-2   dm-9  hugepages  loop-control  mqueue              ppp     sda   sg1       stdout  tty13  tty2   tty26  tty32  tty39  tty45  tty51  tty58  tty7   uhid     vcs2  vcsa2  vhci
bsg            cpu              dm-11  dm-3   dri   hwrng      loop0         net                 ptmx    sda1  shm       tty     tty14  tty20  tty27  tty33  tty4   tty46  tty52  tty59  tty8   uinput   vcs3  vcsa3  vhost-net
btrfs-control  cpu_dma_latency  dm-12  dm-4   fb0   initctl    loop1         network_latency     pts     sda2  snapshot  tty0    tty15  tty21  tty28  tty34  tty40  tty47  tty53  tty6   tty9   urandom  vcs4  vcsa4  vhost-vsock
bus            cuse             dm-13  dm-5   fd    input      mapper        network_throughput  random  sda3  snd       tty1    tty16  tty22  tty29  tty35  tty41  tty48  tty54  tty60  ttyS0  usbmon0  vcs5  vcsa5  zero
cdrom          disk             dm-14  dm-6   full  kmsg       mcelog        null                raw     sda4  sr0       tty10   tty17  tty23  tty3   tty36  tty42  tty49  tty55  tty61  ttyS1  usbmon1  vcs6  vcsa6
char           dm-0             dm-15  dm-7   fuse  lightnvm   mem           nvram               rtc     sda5  stderr    tty11   tty18  tty24  tty30  tty37  tty43  tty5   tty56  tty62  ttyS2  vcs      vcsa  vfio
  • /dev/null,它是一种特殊的虚拟设备,用于写入而不是读取。写入 /dev/null 的任何内容都会从操作系统中消失
echo "hello world" > /dev/null
  • 在Linux中运行任何命令时,都会生成两个输出流,即标准输出(stdout)和标准错误(stderr)。
  • stdout 和 stderr 流可以通过它们的文件描述符(分别为 1 和 2)进行访问
$ ping a.com 1 > data.txt
$ cat data.txt // 会发现没有任何的数据

ping a.com 2>a.txt
  • 丢弃错误信息到/dev/null
ping v.com 2>/dev/null
  • 丢弃正确信息到/dev/null
ping v.com >/dev/null
  • 测试,参考自:https://www.cnblogs.com/zhenghongxin/p/7029173.html
& 表示后台执行的意思
ls 2>1测试一下,不会报没有2文件的错误,但会输出一个空的文件1;
ls xxx 2>1测试,没有xxx这个文件的错误输出到了1中;
ls xxx 2>&1测试,不会生成1这个文件了,不过错误跑到标准输出了;
ls xxx >out.txt 2>&1, 实际上可换成 ls xxx 1>out.txt 2>&1;重定向符号>默认是1,错误和输出都传到out.txt了。
  1. 以下内容参考自:https://zhuanlan.zhihu.com/p/418357041?utm_id=0
  2. 字符串
  • 可以使用单引号或者双引号,或者不使用引号
  1. 变量
  2. 系统命令 $REPLY https://blog.csdn.net/tingyu_521/article/details/84822188
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值