#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Unix help please


    Using echo command answer whether the file size displayed by ls command and the character count displayed wc command are the same.




    Input to a command can come from a file instead of from the keyboard. This is achieved by input redirection operator <. In the following command, the input is redirected from the file /etc/hosts to the wc command which will display the number of lines, words, and characters in the hosts file.
    wc < /etc/hosts
    Issue the above command.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,450
    Rep Power
    1751
    Beginning to sound very much like home/course-work ...

    Look at the man pages for the commands you are being told to use:

    Code:
    man ls
    man wc
    For the ls command you will see that you will need the -l (long) option to provide file size information.

    For the wc command you will see that you'll need the -c (character) option to have it could characters in the specified file name.

    The question does not say how you are supposed to use the echo command to report on the file size comparisons, nor if you are to do this in a script or wholly from the command line. On that basis, and due to the fact that I don't have immediate access to a *nix system and this would all be from memory, I'll just suggest a method you can use.

    Assign the results of the commands to a pair of variables, compare those variables and if they do not hold the same number, do the needed echo command. There are, of course, caveats - the ls -l command does not produce JUST the file size information - that appears as 'field' 5 (I think), so you'd need a way to just obtain that bit (hint: man awk, or man cut). Likewise, the wc command is likely not to just produce the number you are after. Try it by hand, see what it does and work out a way of getting just the data you need.

    To assign the number to a variable, you'd have something like:

    Code:
    ls_filesize=$(ls -l myfilename | cut -f5)
    You'd do something similar for the wc command, then to test to see if they are the same you'd have something like:
    Code:
    if [ $ls_filesize -ne wc_charcount ]
    then
      echo Commands produce different sizes
    fi
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Smile


    Originally Posted by SimonJM
    Beginning to sound very much like home/course-work ...

    Look at the man pages for the commands you are being told to use:

    Code:
    man ls
    man wc
    For the ls command you will see that you will need the -l (long) option to provide file size information.

    For the wc command you will see that you'll need the -c (character) option to have it could characters in the specified file name.

    The question does not say how you are supposed to use the echo command to report on the file size comparisons, nor if you are to do this in a script or wholly from the command line. On that basis, and due to the fact that I don't have immediate access to a *nix system and this would all be from memory, I'll just suggest a method you can use.

    Assign the results of the commands to a pair of variables, compare those variables and if they do not hold the same number, do the needed echo command. There are, of course, caveats - the ls -l command does not produce JUST the file size information - that appears as 'field' 5 (I think), so you'd need a way to just obtain that bit (hint: man awk, or man cut). Likewise, the wc command is likely not to just produce the number you are after. Try it by hand, see what it does and work out a way of getting just the data you need.

    To assign the number to a variable, you'd have something like:

    Code:
    ls_filesize=$(ls -l myfilename | cut -f5)
    You'd do something similar for the wc command, then to test to see if they are the same you'd have something like:
    Code:
    if [ $ls_filesize -ne wc_charcount ]
    then
      echo Commands produce different sizes
    fi

    Thank you Simon for your kind reply.... you were right it is a course that Im trying to understand. It is an online course and trying my best to understand it as much as I can.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    857
    Rep Power
    388

    Cool


    Originally Posted by nancy1165
    .... you were right it is a course that Im trying to understand. It is an online course and trying my best to understand it as much as I can.
    Well, we wish you the best!

    Here is a link that summarizes the syntax if some unix commands.

IMN logo majestic logo threadwatch logo seochat tools logo