View Single Post
  #6 (permalink)  
Old 02-07-2009, 09:09 AM
cfajohnson cfajohnson is offline
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 93
Thanks: 0
Thanked 16 Times in 16 Posts
Rep Power: 3
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by nixcraft View Post
Try the following:

Code:
#!/bin/bash
INPUT=$1

usage(){
    echo "Usage: $0 input.txt"
    exit 1
}

die(){
    echo "$@"
    exit 2
}
[ $# -ne 2 ] && usage

[ ! -f $INPUT ] && die "File $INPUT does not exist!"

That will fail if $INPUT contains spaces or is empty.

Code:
[ ! -f "$INPUT" ] && die "File $INPUT does not exist!"
Quote:
Code:
while read line 
do
    echo "$line" | grep -o . | sort -n |tr -d '\n'; echo ""

That can fail because most versions of grep so not have an -o option. Use
sed instead.

Code:
echo "$line" | sed 's/./&\
/g' | sort | tr -d '\n'; echo ""
(sort doesn't need the -n option.)
Quote:
Code:
done < $INPUT

That will fail if $INPUT contains spaces.

Code:
done < "$INPUT"
Reply With Quote