LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2007, 11:06 AM   #1
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Rep: Reputation: 15
script in korn shell


Hi

I very neatly would like to split my input lines into two variables. Each bit of data is divided by a space. My code is below, how can I do this?

Thanks

#!/bin/ksh
i = 0
while read line_by_line
do

let "i=i+1"
echo $i
echo ${line_by_line}
done < myfile.txt

Input file

0 10
10 3
20 67
30 89
 
Old 02-07-2007, 11:20 AM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Add a second variable in the read statement. The first lexical object will go into the first variable while the rest of the line will go into the second variable.
Code:
...
while read first second
do

let "i=i+1"
echo $i
echo $first $second
done < myfile.txt
You can test it by printing a constant in between $first and $second.
Code:
...
echo $first then $second
...

Last edited by stress_junkie; 02-07-2007 at 11:31 AM.
 
Old 02-07-2007, 12:02 PM   #3
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Original Poster
Rep: Reputation: 15
OK how do I do an if then. Its not working I am not sure whether I need to use the expr statement?
Can somebody show me the correct statement for two number being less than or equal to.

Thanks
 
Old 02-07-2007, 12:07 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
while read line
do
    read var1 var2
    if [[ "$var1" != "$var2" ]] ; then
       echo "$var1 does not equal $var2"
    fi
done < inputfile
 
Old 02-07-2007, 12:11 PM   #5
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Original Poster
Rep: Reputation: 15
More questions.

How can I perform non integer additions and arithmetic?
 
Old 02-07-2007, 12:33 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,267

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
i would create a c-program and call it in the script with the appropriate arguements.

awk may be able to do float math but im not sure of syntax.

Last edited by schneidz; 02-07-2007 at 12:35 PM.
 
Old 02-07-2007, 12:39 PM   #7
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Original Poster
Rep: Reputation: 15
what about bc is that a way?
 
Old 02-07-2007, 05:40 PM   #8
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by disruptive
what about bc is that a way?
Why don't you just try it and find out? A little self reliance goes a long way.

You could have found out about the read command using multiple variables just by reading the man page. You could find out about using bc in a script by trying it.

You're not going to get very far if you refuse to do the simplest things to help yourself.

Last edited by stress_junkie; 02-07-2007 at 05:43 PM.
 
Old 02-07-2007, 09:18 PM   #9
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Yes. You can use bc.

ksh lets you force an arithmetic evaluation with the (( )) construct -
eg (( 3 + 1 )).... Try it with float values and see if it works or not...
 
Old 02-07-2007, 09:33 PM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
for non-integer arithmetic, you can also use zsh which supports it internally, as well as many other nice features.

If you're going to do a lot more sophisticated stuff, I'd consider moving to a more complete language. Perl or Python are both nice, and both have huge libraries of pre-written modules for doing a great deal of stuff. They also both have quite nice mechanisms for you to put your own code in mudules and allow easy re-use/sharing.

C is a lot harder to do well because yuo have to manage your own memory. Of course it has it's moments. There isn't really anything which can touch C for OS coding. (/me dons aspestos underpants).
 
Old 02-19-2007, 09:36 AM   #11
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Original Poster
Rep: Reputation: 15
Thanks for the tip-off about zsh,

I am trying the same thing, however I am noting that when I am reading in values from a file, they are not being added as floating point values but instead appear truncated. I have looked but to no avail. Can you suggest where the program is failing.

#!/bin/zsh
let "i = 0"
let "sum = 0"
echo "Min MC: "
read MinMC

#x=2.5
#y=3.456
#(( res = x / y ))

#echo $res
#(( res = x + y ))
#echo $res
#(( res = x * y ))
#echo $res


while read MC Value
do
#echo $MC $Value
if ((MinMC <= MC))
then

((i=i+1))
# echo $i
let "sum=sum+Value"
#else
#echo nothing
fi
#echo ${Value}
done < myfile.txt
echo Sum: $sum
echo count: $i
((average = sum / i))
echo average: $average
 
Old 02-21-2007, 12:17 AM   #12
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jim mcnamara
Yes. You can use bc.

ksh lets you force an arithmetic evaluation with the (( )) construct -
eg (( 3 + 1 )).... Try it with float values and see if it works or not...
ksh93 can do arithmetic with decimal fractions, but other versions, including pdksh, cannot.
 
Old 02-21-2007, 03:54 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Void, Debian, Puppy, Raspbian
Posts: 3,478

Rep: Reputation: 222Reputation: 222Reputation: 222
you can only do integer arithmetic in korn.
use dc or perl or python
 
Old 02-21-2007, 12:33 PM   #14
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by bigearsbilly
you can only do integer arithmetic in korn.
use dc or perl or python
Korn Shell 93 can do decimal fractions:

Code:
$ echo $0
/usr/local/bin/ksh93
$ echo $(( 1.3 * 2.4 ))
3.12
 
Old 02-21-2007, 01:01 PM   #15
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by cfaj
Korn Shell 93 can do decimal fractions:

Code:
$ echo $0
/usr/local/bin/ksh93
$ echo $(( 1.3 * 2.4 ))
3.12
I was surprised to see this, so I checked...
Code:
matthew@chubby:~$ ksh
$ echo $(( 1.3 * 2.4 ))
3.1200000000000001
$ exit
matthew@chubby:~$ which ksh
/usr/bin/ksh
matthew@chubby:~$ ksh --version
zsh 4.3.2 (i686-pc-linux-gnu)
matthew@chubby:~$ ls -l `which ksh`
lrwxrwxrwx 1 root root 29 2007-02-06 17:16 /usr/bin/ksh -> /etc/alternatives/usr.bin.ksh
Aha, the old alternatives thing... OK, install actual ksh:
Code:
matthew@chubby:~$ sudo apt-get install ksh
Password:
...
matthew@chubby:~$ ksh --version
  version         sh (AT&T Labs Research) 1993-12-28 r
matthew@chubby:~$ ksh
$ echo $(( 1.3 * 2.4 ))
3.12
Wow - it works. Now for pdksh:
Code:
matthew@chubby:~$ pdksh
$ echo $(( 1.3 * 2.4 ))
pdksh:  1.3 * 2.4 : unexpected `.'
So there you have it. ksh93 - floating point works, but pdksh not.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Korn Shell PS1 c19h28O2 Linux - Newbie 4 04-25-2006 07:56 AM
Korn shell and Arrays jpc82 Programming 1 06-28-2005 03:27 AM
unix korn shell script that interact with windows java program ann124 Programming 5 11-09-2004 10:14 PM
Korn shell script Muzica Solaris / OpenSolaris 4 09-06-2004 01:47 PM
install korn shell namgor Linux - Newbie 1 07-12-2004 03:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration