bash中的作业控制jobs & 花括号{}的使用,括号可以包含连续的序


1、作业控制技巧

     Bash环境中通过命令运行一个进程的时候,使【&】 符可以使改进程进入后台

(base) [root@localhost ~]# sh test.sh &
[1] 46963
(base) [root@localhost ~]#

     将该进程放入后台并暂停执行      Ctrl+z

(base) [root@localhost ~]# sh test.sh 
^Z                                       ---(Ctrl + Z)
[2]+  Stopped                 sh test.sh
(base) [root@localhost ~]# jobs
[1]-  Stopped                 sh test.sh
[2]+  Stopped                 sh test.sh
(base) [root@localhost ~]# 

     查看后台进程命令      jobs

(base) [root@localhost ~]# jobs
[1]+  Running                 sh test.sh &
(base) [root@localhost ~]#

     通过fg(编号) 的形式可以将这些后台进程再次调入前台执行   

(base) [root@localhost ~]# fg 1
sh test.sh

2、花括号{}的使用

      可以通过括号 扩展 生成 命令行和脚本需要是字符串。

      括号可以包含连续的序列或使用逗号分割的多个项目。连续的序列包含一个起点和一个终点,并使用“”..“”分割。

     例子如下:

(base) [root@localhost ~]# echo {a,b,c}
a b c
(base) [root@localhost ~]# echo user{1,5,6}
user1 user5 user6
(base) [root@localhost ~]# echo {0..10}
0 1 2 3 4 5 6 7 8 9 10
(base) [root@localhost ~]# echo a{2..-1}
a2 a1 a0 a-1
(base) [root@localhost ~]# mkdir /tmp/{dir1,dir2,dir3}
(base) [root@localhost ~]# ls -ld /tmp/dir{1,2,3}
drwxr-xr-x. 2 root root 6 May 23 23:31 /tmp/dir1
drwxr-xr-x. 2 root root 6 May 23 23:31 /tmp/dir2
drwxr-xr-x. 2 root root 6 May 23 23:31 /tmp/dir3
(base) [root@localhost ~]# chmod 777 /tmp/dir{1,2,3}
(base) [root@localhost ~]# ls -ld /tmp/dir{1,2,3}   
drwxrwxrwx. 2 root root 6 May 23 23:31 /tmp/dir1
drwxrwxrwx. 2 root root 6 May 23 23:31 /tmp/dir2
drwxrwxrwx. 2 root root 6 May 23 23:31 /tmp/dir3
(base) [root@localhost ~]# 

 

相关内容