10.Write a shell script to count the number words,lines,character from given file accept file name from commandline.
#!/bin/bash
# Check if a filename was provided
if [ $# -ne 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
# Assign the filename to a variable
filename=$1
# Check if the file exists
if [ ! -f "$filename" ]; then
echo "Error: File '$filename' not found!"
exit 1
fi
# Use wc to count lines, words, and characters
lines=$(wc -l < "$filename")
words=$(wc -w < "$filename")
chars=$(wc -c < "$filename")
# Print the results
echo "File: $filename"
echo "Lines: $lines"
echo "Words: $words"
echo "Characters: $chars"
Comments
Post a Comment