LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Interesting Behavior (https://www.linuxquestions.org/questions/linux-newbie-8/interesting-behavior-4175521299/)

sumncguy 10-07-2014 07:45 AM

Interesting Behavior
 
Im taking a Linux Essentials class at a local community college. Ive been using the command line for some time in one nix environment or another. Im ok .. but I want to fill in the blanks and LPI cert. Anyway ... this was on a lab. The professor didn't go into detail .. and ironically no other student questioned what was going on...

Quote:

[popeye bin]$ ls -l > file.txt

[popeye bin]$ cat file.txt
total 8
-rw-rw-r-- 1 popeye popeye 16 Oct 7 07:38 file1.txt
-rw-rw-r-- 1 popeye popeye 17 Oct 7 07:38 file2.txt
-rw-rw-r-- 1 popeye popeye 0 Oct 7 07:38 file.txt
Notice the file size of file.txt

Quote:

[popeye bin]$ ls -l file.txt
-rw-rw-r-- 1 popeye popeye 178 Oct 7 07:38 file.txt

[popeye bin]$
I had to think about this for a few minutes.

It looks like the file is created and ls is executed, before the file is written to...

Is there some order of operation going on ?

linosaurusroot 10-07-2014 08:01 AM

Your shell is doing this: you are right that creation of the output happens before ls produces the listing.

Code:

receive command from user
fork into two (almost identical but nevertheless distinguishable) processes
  child process:
    redirect stdout to file.txt (creating new empty file)
    execute "ls -l" with stdout as defined above
    and end ... nothing follows successful execution (of ls)

  parent process:
    wait for the child process to end (and observe exit status)
    prompt user for next input



All times are GMT -5. The time now is 11:09 PM.