1

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."
  • 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

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"
2

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.

  • +1 for stating the obvious (wc does them all, no need for other tools). But why wc -w -l -c "$1" ? wc "$1" does the same... – don_crissti Feb 20 '15 at 20:16
  • Just as a reminder of the wc flags, so that it would be easy to edit the script to display only lines, for instance. – Greg Marks Apr 20 '15 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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