Quote:
Another question though.If I want to give the name of a file so that the script will check if it has more than 10 lines and then print it,what should I do...?Thanks,in advance..
|
Is this homework?
Anyway, here it is. This only works with text/ascii files.
Code:
#!/bin/bash
while [ $1 ] ; do
lines=$(wc -l $1 | awk '{print $1}')
if [ $lines -ge 10 ] ; then
echo "$1 has $lines lines"
cat $1
else
echo "$1 has less than 10 lines"
fi
shift
done
All you do is name the script and pass the file(s) to check.
Example: scriptname file1 file2 etc...
or
scriptname *.txt
scriptname *
This is how it works. The while loop will read each file argument and store the output of wc -l for that file into the variable called lines.
The if statement will test if the number stored in the variable lines is 10 or greater. If true, then it will print the file name and its contents. If false, then it will echo that the file has less then 10 lines and not print its contents.
If you pass two or more file arguements, the shift command will move the next file into position one (i.e $1)
A more seasoned script writer could write a similar script or better with fewer lines. It's good as a start point. Anyway, good luck.