2

I am trying to write a script to imitate this script output in linux bash:

(bob@server:~> filesize

Enter a file name (or q! to stop): fee

fee uses 123 bytes.

Enter a file name (or q! to sp): fi

There is no file called fi.

Enter a file name (or q! to stop): foe

foe uses 9802 bytes.

Enter a file name (or q! to stop): q!

bob@server:~>)

My script looks like this (the script name is filesize):

#!/bin/bash
while true; do
        read  -p "Enter a filename (Or q! to stop) : " X
        case $X in
                [q!]* ) exit;;
                * ) echo "$X uses "$(wc -c <$X)" bytes";./filesize;;
        esac
done

After I type anything other than q! and it reads $X uses $(wc -c <$X), I have to type q! twice to make the command exit.

How do I make it so that I only have to type q! once to make the command exit, instead of typing it the multiple times that I read a size of a file?

2
  • 1
    Noobs (sorry if you aren't one) always want to read input interactively, which is usually a horrible idea... Learn how to use command line arguments and write scripts that can be piped up. That said, the reason is you have a redundant self-call ./filesize in your script, which is pointless. You're already in a infinite loop until you break out. Just remove that. Commented Nov 30, 2015 at 0:25
  • The problem with [q!] is that it matches a single character which is either q or !. Commented Dec 1, 2015 at 11:43

1 Answer 1

3
filesize(){ stat -c %s -- "$@";} 

And if you insist on having all the blabber around it:

filesize(){ stat -c %s -- "$@";} 
while :; do
    read  -p "Enter a filename (Or q! to stop) : " x
    case "$x" in
        'q!') exit;;
       *) printf '%s\n' "$x uses $(filesize "$x") bytes";;
    esac
done

The function alone is much more Unix philophy than the while loop, however.

wc -c < "$x" is OK too. The difference is that stat will tell you the size right away without having to do the counting.

Sign up to request clarification or add additional context in comments.

5 Comments

This is great! thank you, but after the command prints out the size of the file in bytes, when it reads "Enter a filename (or q! to stop) : again, it doesn't print on the next line. How would I edit the script to print on the next line?
The '%s\n' instead of '%s' should fix that. Sorry about that. I didn't test it. Printf gives you more robustness over echo in cases where the filename starts with a hyphen.
The pattern [q!]* doesn't really do what OP thinks it does. I'd suggest that you not reproduce the error.
@rici, Instead of just saying "The pattern [q!]* doesn't really do what OP thinks it does. I'd suggest that you not reproduce the error.", it would be much more helpful if you'd either explain what is wrong with it and or suggest what is should be. Thanks.
@user3439894 Thanks. Made it correspond with the message. OP may feel free to a question about them shell patterns if he feels like it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.