Visit Our UNIX and Linux User Community


Begining Shell scripter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Begining Shell scripter
# 1  
Old 10-09-2007
Begining Shell scripter

I am trying to write for my class project a shell script, using bourne shell, that will take one command, either -l or -w. If -l is entered then the script should output the names of all the files in my working directory sorted in increasing order of the number of lines in the file. If -w is input then the same ill be done except that it will be sorted in increasing order by the number of words in the files. I know the solu'n will use the command ls and a pipeline to sort. but I don't know how to list the files or sort the files by the number of words or lines that they have. so far I have
#!/bin/sh

print=`ls | sort`
echo $print

this is not exactly correct but if I knew how to sort the files according to the number of lines each had, then I could rearrange this code to adapt.
Any help would be really appreciated. The project is not worth much at all but I am curious as how to sort this way. Thank you.
# 2  
Old 10-09-2007
do a 'man wc' for more information on the word count command.
# 3  
Old 10-10-2007
#!/bin/bash
if [ $# -ne 1 ]
then
echo "usage: `basename $0` <-l|-w>"
exit 1
fi

case "$1" in
-l)
echo lines/filename
wc -l * | sort | grep -v ` basename $0` | grep -v ' total'
;;
-w)
echo words/filename
wc -w * | sort | grep -v ` basename $0` | grep -v ' total'
;;
*)
echo "usage: $0 <-l|-w>"
exit 1
;;
esac

Previous Thread | Next Thread
Test Your Knowledge in Computers #602
Difficulty: Medium
MySQL 8.0 incorporates a transactional data dictionary that stores information about database objects.
True or False?

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

new scripter

Hello all, new to scripting (and unix!) here. Im trying to write a script for something i have to do for work and for some reason it just isnt working. im using the ksh. Can someone please tell me why $PRODUCT=$1 CONFIG=./etc/$PRODUCT_config.xml print Using $CONFIG prints... (1 Reply)
Discussion started by: Gwoodhouse0
1 Replies

2. Shell Programming and Scripting

Use of #! at the begining ?

All, i have some doubt on #!/bin/bash or #!/bin/ksh at the begining of script. Need to know folowing thing ? -- what would be the problem if i will mention in the first line #!/bin/bash and will use korn shell syntax and the reverse ? -- Can i mention at the... (1 Reply)
Discussion started by: jambesh
1 Replies

Featured Tech Videos