shell-4.Bash shell编程的基本概念

Bash shell编程的基本概念

  • 标准输入以及标准输出
  • 输入输出重定向
  • 管道
  • 前台运行程序和后天运行任务

1.标准输入以及标准输出

[root@master ~]# ls -l /dev/std*
lrwxrwxrwx. 1 root root 15 7月  23 19:19 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx. 1 root root 15 7月  23 19:19 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx. 1 root root 15 7月  23 19:19 /dev/stdout -> /proc/self/fd/1

其中,这个为标准输入文件:

lrwxrwxrwx. 1 root root 15 7月  23 19:19 /dev/stdin -> /proc/self/fd/0

其中,这个为标准输出文件:

lrwxrwxrwx. 1 root root 15 7月  23 19:19 /dev/stdout -> /proc/self/fd/1

其中,这个为标准错误文件:

lrwxrwxrwx. 1 root root 15 7月  23 19:19 /dev/stderr -> /proc/self/fd/2

总结:系统默认开启的三个文件,标准 I/O

  • stdin标准输入,-》0(键盘)
  • stdout标准输出,-》1(屏幕)
  • stderr标准错误,-》2(屏幕)
    注意:0是代表输出,1是代表输出,2是代表错误
    在这里插入图片描述

输入输出重定向(接上0,1,2三代表)

使用:

[root@master ~]# echo abc 1>file1
[root@master ~]# cat file1
abc

清除文件内容:

[root@master ~]# >file1
[root@master ~]# cat file1
[root@master ~]# 

追加(>>)与替换(替换>是对首行而言)


[root@master ~]# echo begin > file1
[root@master ~]# cat file1
begin
[root@master ~]# echo enda > file1
[root@master ~]# cat file1
enda
[root@master ~]# echo begin > file1
[root@master ~]# cat file1
begin
[root@master ~]# echo end >>file1
[root@master ~]# cat file1
begin
end

管道

无名管道

  • “|” 符号,将一个程序的输出作为另一个程序的输入
[root@master ~]# grep root /etc/passwd | tee file
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@master ~]# cat file
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
  • 命令1 | 命令2
  • 命令1 | 命令2 |命令3…|命令n

有名管道

  • 使用mkfifo创建的管道文件(上节有)
  • 可以跨越终端将两个命令协同运行

前台运行程序和后天运行任务

  • 前台运行的程序
[root@master ~]#  while true; do sleep 1 ; done
^C
[root@master ~]# 
  • 后台运行任务(&是放在后台)
[root@master ~]# sleep 1 &
[1] 10528
[root@master ~]# jobs
[1]+  Done                    sleep 1
  • 前后台切换方法
    • &、nohup、 Ctrl +Z(把任务放到后台会停止)
    • fg(从后台拉回来继续运行) 、bg(从前台台继续运行程序)
      注意:+是拉回到后台的状态
[root@master ~]# sleep 100 & 
[1] 10540
[root@master ~]# sleep 100 & 
[2] 10541
[root@master ~]# sleep 100 & 
[3] 10542
[root@master ~]# jobs
[1]   Running                 sleep 100 &
[2]-  Running                 sleep 100 &
[3]+  Running                 sleep 100 &
[root@master ~]# bg 2
-bash: bg: job 2 already in background
  • jobs(查看后台任务)
  • kill(杀掉后台进程)
    • 使用 kill job_id
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值