LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 10-26-2008, 11:40 AM   #1
szcs
LQ Newbie
 
Registered: Oct 2008
Location: Hungary
Posts: 1

Rep: Reputation: 0
Need help with bash script /simplified sort command/


Hello!
I'm quite new to Linux...
I would like to make a bash script that works like the "sort" command, but the script itself does not contain the "sort" command.
It should have two options: sorting numerically and lexiologically /with being able to sort with ignoring upper case letters/.
This is how it looks like now:

Code:
#!/bin/bash

#Simplified sort script
#Assuming inputs az space separated words or numbers
#Switches:
#-f <FileName>:The script gets the input data from the file
#-i: Ignore case.
#-n: Sort in numerical mode
#-i and -n can not be defined at the same time

#uncomment this line of you want to see the steps in detailed format
#set -x

#The usage script
usage="Usage: $0 [-f] <FileName> [-i] [-n]"
#Stores the file`s name, if -f is given
fileName=""
#Set to 1 if -i is given
ignoreCase=0
#Set to 1 if -n is given
numerical=0
#Stores the values given by the user, or read from the file
values[0]=0

#Function to sort the values in lexical mode
function sortByName()
{
echo "sortByName $1 $# $@"
echo "$*"
}

#Function to sort the values in numberical mode
function sortByValue()
{
sortedValues[${#}]=0
indexi=0
min=50000000
lastMin=-50000000

for i in ${@}
do
indexj=0

echo "-------------------------"
for j in ${@}
do
if [ ${indexj} -ge ${indexi} ]
then
echo
echo "j: ${j}"
echo "min: ${min}"
echo "lastmin: ${lastMin}"
echo
if [ ${j} -gt ${lastMin} ]
then
if [ ${j} -lt ${min} ]
then
min=${j}
j=50000000
fi
fi
fi
((indexj++))
done
sortedValues[${indexi}]=${min}
lastMin=${min}
((indexi++))
min=50000000
done

for ((i=0;i<=$#;i++))
do
echo -n "${sortedValues[${i}]}"
done

echo
}

#Reads the given switches
while getopts :f:in opt
do
echo "ehhe $opt"
case $opt in
f)
fileName=$OPTARG
;;
i)
if [ $numerical -eq 1 ]
then
echo "-i and -n cannot be set at the same time"
exit;
fi
ignoreCase=1
;;
n)
if [ $ignoreCase -eq 1 ]
then
echo "-i and -n cannot be set at the same time"
exit;
fi
numerical=1
;;
\?)
echo "$usage"
exit
;;
esac
done

#If fileName is given get the values from it
if [ "$fileName" != "" ]
then
values=`cat $fileName`
else
echo "Enter values:"
read values
fi

if [ $numerical -eq 1 ]
then
sortByValue $values
else
sortByName $values
fi
I have problems with making the two processes that do the two types of sorting. What kind of algorithm should I use, and how would that look like in bash? Any advice or help would be great, even with the script above.
Thank you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to sort mp3 bitrates above and below 128k Damoek Linux - Newbie 3 03-25-2008 10:15 AM
Bash script - how to get 'basename' command to do what I want..? craftybytes Linux - Software 8 02-14-2008 03:07 AM
Bash Script as command whited Programming 15 12-10-2007 06:14 AM
Bash script to sort image files dtcs Programming 5 09-26-2006 09:50 PM
bash script to sort files by extension otheralex Programming 7 08-19-2005 02:40 AM


All times are GMT -5. The time now is 03:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration