Suppose I have this shell script call cpdir:
(cd $1 ; tar -cf - . ) | (cd $2 ; tar -xvf - )
When I ran it, the main shell should create two processes (subshells) to execute both groups of commands concurrently. However, how can the shell make sure that both processes change to appropriate directories, then first process package the content of the directory and send to the second process for unpacking?
Why is there no race condition? Is it a rule that every command of every process will execute in order, although processes can be parallel?
i.e. first process will run “cd $1”, and then second process will run “cd $2” (or it should be execute the same time as the first process? Not sure), then first process will package everything and finally send to second process.
Although, one little thing I don’t know about tar:
tar -cf - .
I know the dot (.) is the content of current directory. However, what’s the ‘-‘ in the command?
You don’t need to use
cdbecause tar has a-Coption which tells it to change to a directory. So you can simply use a command such as:-means stdin/stdout. The first hyphen tellstarto write to stdout. The second one tellstarto read from stdin.Since
-is the default, you don’t even need to specify it. You can shorten your command to: