[英]How to create a bash script in Linux that does multiple tasks at once
我想創建一個包含以下內容的bash腳本:
我知道如何執行命令以分別創建每個命令,但是我無法弄清楚如何創建可以一次執行所有命令的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
昨天我了解了一個名為parallel
的工具: https : //www.gnu.org/software/parallel/
它使得並行運行任意數量的任務以及對它們的輸出進行排序非常容易。 花一些時間瀏覽本教程,這將是您度過的美好時光!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.