Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to create a shell script df. As, I want to type in df name_of_file and then I can get the contents of the file.

If the file name is in the directory, I want it to show its contents. If not, I want to show the size in words, lines, and bytes. How do I go about this? So far, I have this:

#!/bin/bash
FILENAME=$FILENAME
FILEBYTES=$(stat -c%s "$FILENAME")
FILEWORDS=$(stat -w%s "$FILENAME")
FILELINES=$(stat -l%s "$FILENAME")
echo "Size of $FILENAME in bytes = $FILEBYTES  bytes, $FILEWORDS words, $FILELINES lines."
share|improve this question
    
What does it mean that 'If not, I want to show the size in words, lines, and bytes' when the file name isn't in the directory? –  slm Nov 2 '13 at 2:22
1  
Please explain what you expect to happen when the file's present in the directory vs. not. What you've stated is contradictory "...file name is in the directory, I want it to show its contents. If not, I want to show the size..." –  slm Nov 2 '13 at 2:28
    
I actually meant that I do want all of that info when I do a df filename command. So, when I do df filename... I'd want it to display the contents –  Gary Nov 2 '13 at 2:29
    
OK, that's kind of what I figured. Also what do you mean by filewords? –  slm Nov 2 '13 at 2:31
    
how many words are in the file, eek. –  Gary Nov 2 '13 at 2:31

2 Answers 2

up vote 2 down vote accepted

I would do it like so:

#!/bin/bash

FILENAME=$1

[ -f "$FILENAME" ] || exit

FILEBYTES=$(stat -c%s "$FILENAME")
FILEWORDS=$(wc -w "$FILENAME" | awk '{print $1}')
FILELINES=$(wc -l "$FILENAME" | awk '{print $1}')

printf "Size of %s -- %s bytes, %s words, %s lines.\n" \
        "$FILENAME" "$FILEBYTES" "$FILEWORDS" "$FILELINES"

Example

Say I have this file:

$ ls -l | grep bbbb
-rw-rw-r--   1 saml saml    3283 May 18 02:49 bbbb

Now I run your script, I called it df.bash. The df command is already taken in Unix:

$ ./df.bash bbbb 
Size of bbbb -- 3283 bytes, 386 words, 94 lines.

Look for a file that doesn't exist:

$ ./df.bash bbbbbbb
$ 

Alternative method

You can save some wasted calls by consolidating and having awk do the counting instead of wc.

#!/bin/bash

FILENAME=$1

[ -f "$FILENAME" ] || exit

FILEBYTES=$(stat -c%s "$FILENAME")
FILEWORDS=$(awk '{ total = total + NF }; END {print total}' "$FILENAME")
FILELINES=$(awk 'END {print NR}' "$FILENAME")

printf "Size of %s -- %s bytes, %s words, %s lines.\n" \
        "$FILENAME" "$FILEBYTES" "$FILEWORDS" "$FILELINES"
share|improve this answer
    
Thank you! this is perfect –  Gary Nov 2 '13 at 2:49
    
@Gary - you are very welcome. Thanks for your Q! –  slm Nov 2 '13 at 2:52
    
@jasonwryan - thanks I'll add those as alternative methods. –  slm Nov 2 '13 at 3:22
    
@Gary - you close the other Q before I could help you, To get a list of the top 5 words in a file: stackoverflow.com/questions/11850823/…. Also if you need help come into the chat we can help you if you're stuck in there: chat.stackexchange.com/rooms/26/unix-and-linux –  slm Nov 2 '13 at 6:14

Are you asking for this?

 #! /bin/bash
 if [[ -f $(basename "$1") ]]
    then
       cat "$1"
    else
       wc -w -l -c "$1"
 fi

By the way, df is already a command to display disk space usage.  Better to call your script something else.

share|improve this answer
    
Thank you! I will test this out –  Gary Nov 2 '13 at 2:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.