linux - 如何在 Linux 中创建同时执行多项任务的 bash 脚本

标签 linux bash

我想创建一个包含以下内容的 bash 脚本:

  1. 输入输出重定向
  2. 管道
  3. 符号链接(symbolic link)
  4. 外卡
  5. 将用户添加到多个组。

我知道如何执行命令来单独创建每个命令,但我不知道如何创建一个 bash 脚本来一次执行所有命令。有人能帮助我吗?我将不胜感激。

    #/bin/bash

    cd /home

    mkdir testdir &&

    cd testdir

    touch testfile.txt | ln -s /etc/passwd symboliclink && cd .. | mkdir things && cp -rf /testdir /things | usermod -aG admins,hr,payroll test

最佳答案

把你想做的每件事都放在一个函数中,然后在后台执行每一个:

do_redirection_stuff() {
    # your stuff here
}

do_wild_card_stuff() {
    # your stuff here
}

do_symbolic_link_stuff() {
    # kool stuff
}

do_parallel_tasks() {
    do_redirection_stuff &
    do_wild_card_stuff &
    do_symbolic_link_stuff &

    wait # For all of them to finish
}

do_parallel_tasks

更新(代码已添加到 OP 的问题中)

您似乎对如何使用 shell 重定向感到困惑。在您提供的代码中,您应该连续执行每个命令。同样,这些命令都不可能阻塞很长时间,因此并行执行它们是多余的。

#!/bin/bash
cd /home
mkdir testdir
cd testdir
touch testfile.txt
ln -s /etc/passwd symboliclink
cd ..
mkdir things
cp -rf /testdir /things
usermod -aG admins,hr,payroll test

关于linux - 如何在 Linux 中创建同时执行多项任务的 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42869510/

相关文章:

linux - 如何在单板计算机上编程

linux - 如何使用 telnet 获取外部连接的 IP,或者可能是 memcached 允许的 ip 的配置错误

c - 为什么 sleep 语句会为我们的嵌入式 ruby​​ 产生这个结果?

python - 将 Bash 脚本的剩余 `n-2` 个参数提供给调用的命令

c - 涉及for循环的fork函数

bash - "/bin/sh: 1: [“apache2ctl” , : not found"in docker

linux - Linux 中的输出格式

bash - 来自 bash 脚本的电子邮件

bash - consul-template 在模板中使用地址而不是服务地址

linux - 自动执行命令的本地Linux用户无法关闭