In your case, I don't believe it is good to restrict the output to a certain length, because your wc
output may have different lengths. However you could do this by using cut
:
Change the last line of your script to
wc "$input" | cut -c 1-6
This will only output the first 6 characters.
I believe, you better use this:
wc "$input" | sed -e s/"$input"$//
This will remove the content of the $input
variable (in your case the file name) from the output. This version ignores the output length and can deal with longer output strings as well.
[edit]:
Explanation of the second command:
wc "$input" #-> wc command as used before
| #-> redirect output of wc to the next command
sed -e #-> call the program "sed" with the following expression
s/"$input"$// #-> replace everything between / 1 and 2 with everything between / 2 and 3
#->in this case: replace filename with nothing. The second "$" represents the end of the line (so the filename has to be the last element of the line)
By the way, there are a few other errors in your script:
echo "Enter a file name:\c " -> you don't need the \c
read input
if [ -f $input ] -> you should always quote variables
then
echo "line words chars:\c "
'echo wc $input' -> you should not use echo and '' here, if you want to execute wc
-> you are missing a "fi"
so this code should work:
echo "Enter a file name:"
read input
if [ -f "$input" ]
then
echo "line words chars:"
wc "$input"
fi