Certainly! Here’s a simple shell script that takes a filename as an argument and counts the number of lines in that file using the wc
(word count) command:
#!/bin/bash
# Check if a filename is provided as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename="$1"
# Check if the file exists
if [ ! -f "$filename" ]; then
echo "File not found: $filename"
exit 1
fi
# Count the number of lines using wc
line_count=$(wc -l < "$filename")
echo "Number of lines in $filename: $line_count"
Save this script to a file (e.g., count_lines.sh
), make it executable using chmod +x count_lines.sh
, and then run it with the filename as an argument:
./count_lines.sh yourfile.txt
Replace yourfile.txt
with the actual filename you want to count the lines for. The script will provide you with the number of lines in the specified file.