0

I would like to create a bash script that would contain the following:

  1. I/O redirection
  2. Pipes
  3. Symbolic Link
  4. Wild cards
  5. Add a user to multiple groups.

I know how to execute the commands to create each of them individually, but I can't figure it out how I could create a bash script that would execute all of them at once. Can someone help me please? I would greatly appreciate it.

    #/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

2 Answers 2

3

Put each thing you want to do in a function, and then background the execution of each:

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

Update (code was added to OP's question)

It seems like you're confused about how shell redirection is used. In the code you provided, you should just serially execute each command. Likewise, none of these commands are likely to block for very long, so executing them in parallel is overkill.

#!/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
Sign up to request clarification or add additional context in comments.

4 Comments

Jameson, thank you for your example. But I'm still having difficulties to understand the whole process! Would it be there an easier approach to accomplish this task?
@shabam you should post the code you have so far so there can be a concrete discussion.
I have added to my question what I did so far. Your help is very much appreciated.
At the end of the script I was trying to create an alias. So,what I did is: echo "alieas test='cd && ./myscript.sh'" >> ~/.bashrc . ~/.bashrc It does add the line to the .bashrc but it seems that the . ~/.bashrc it's not working. Can you please help. I don't know what I'm doing wrong!
0

I learned about a tool yesterday called parallel: https://www.gnu.org/software/parallel/

It makes it really easy to run any number of tasks in parallel, and sorts their output too. Take some time to look through the tutorial, it'll be time well spent!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.