Help when using Typeset in AIX Korn Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help when using Typeset in AIX Korn Shell
# 1  
Old 11-27-2012
Help when using Typeset in AIX Korn Shell

Guys, please help! I am currently using an AIX server however whenever I tried to use the typeset -F3, the variable is resulting with a "#".

In the given example below, I declared x to be a decimal holding 3 decimal places = 1.455. However whenever I tried to echo the $x, the resulting value became 3#1. Please let me know how can I make $x to be exactly the value I assign to this variable (1.455). Thanks in advance!
Code:
home/zzavilz> typeset -F3 x=1.455
home/zzavilz> echo $x
3#1

Server Info:
Code:
home/zzavilz> uname -a
AIX sverpdev 1 6 00F6F6764C00
 
home/zzavilz> echo $SHELL
/usr/bin/ksh

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.
Please use code tags next time for your code and data.

Last edited by vbe; 11-27-2012 at 08:47 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-27-2012
Hi, you need to use ksh93 for this.
# 3  
Old 11-27-2012
Thanks you for your response Scrutinizer!

But is there any other way I can achieve to have a variable with 3 decimal places even without using the ksh93?
# 4  
Old 11-27-2012
What do you want to do with that variable?
# 5  
Old 11-27-2012
You can have a string variable holding the string representation of a float variable.

But the feature of floating point variables and arithmetic is unique to ksh93 and zsh, afaik.
# 6  
Old 11-27-2012
Thanks scrutinizer and hergp!

Actually this is the code I have. Here, I am using two files. First is the cluster_file.list and item_sales.dat.

The script will parse the cluster_file.list and check the total sales for the cluster. If total sales of the cluster is greater than 100 and less than 200 then adjustment needs to be done. The adjustment will be 200 - the total sales of the cluster / divided by the total lines of the cluster. The calculated adjustment will be distributed/added to all ITEMS under the cluster.


Input:
Code:
cluster_file.list
CLUSTER_A

 
item_sales.dat
ITEM1,CLUSTER_A,80
ITEM2,CLUSTER_B,50
ITEM3,CLUSTER_C,85

Code:
Code:
#==================================================================================
# f_adjustMinimums
#----------------------------------------------------------------------------------
# DESCRIPTION:
# This function will be used to adjust the sales based on minimum
#
# Parameter 
# a. Cluster List File
# b. Sales File
#----------------------------------------------------------------------------------
function f_adjustMinimums {
if [[ $DETAIL_LOG = "true" ]]; then set -x; fi; FUNCTION=$0

# Assign Cluster and Sales File into Arrays
CLUSTER=`cat $1`
SALES=`cat $2`
set -A CLUSTER_TEMP
 
# Adjust sales based on minimum = 200
    for cluster in "${CLUSTER[*]}"
    do
        # Get the total sales of cluster being evaluated
        total_sales=`echo "${SALES[*]}"|grep $cluster|awk -F"," '{x+=$3} END {print x}'`
        # Validate the total sales of cluster being evaluated if sales > 100 and sales < 200
        if [[ $total_sales -gt 100 && $total_sales -lt 200 ]]; then
 
            # Get the total no. of lines of cluster being evaluated
            typeset -i cluster_lines=`echo "${SALES[*]}"|grep $cluster|wc -l`
 
            # Get the difference between 200 and total_sales
            typeset -F3 diff="200 - $total_sales"
 
            # Calculate the quotient to be distributed in the cluster
            typeset -F3 quotient="$diff/$cluster_lines"
 
            # Adjust all sales value of current cluster (new sales value = origvalue + quotient)
            echo "${SALES[*]}"|grep $cluster|while read line
            do
                item_id=`echo $line|awk -F"," '{print $1}'
                old_sales_value=`echo $line|awk -F"," '{print $3}'
                typeset -F3 new_sales_value="$old_sales_value + $ quotient"
                SALES_TEMP=`echo "${SALES[*]}"|sed "s/$item_id,$cluster,$old_sales_value/$item_id,$cluster,$old_sales_value/g"`
                SALES_TEMP=`echo "${SALES_TEMP[*]}"`
            done
        else
            : # Do nothing as the total sales is less than 100 or greater then 200
        fi
    done
# Replace Sales File based on minimum functionality
echo "${SALES[*]}" > $2
}

Output
Code:
ITEM1,CLUSTER_A,97.5 
ITEM2,CLUSTER_B,50
ITEM3,CLUSTER_A,102.5

Analysis:
total_sales of CLUSTER A 165 = 85 + 80
cluster_lines of CLUSTER_A = 2
diff 35 = 200 - 165
quotient=17.5 ($diff/$cluster_lines) # Distribute 17.5 to all ITEMS in CLUSTER_A

Original value of ITEM1 in CLUSTER A = 80, Changed to 97.5
Original value of ITEM3 in CLUSTER A = 85, Changed to 102.5
The difference needs to be distributed based on the # of lines of CLUSTER_A

Last edited by Scrutinizer; 11-27-2012 at 10:39 AM.. Reason: code tags
# 7  
Old 11-27-2012
Quote:
Originally Posted by zzavilz
typeset -F3 diff="200 - $total_sales"
typeset -F3 quotient="$diff/$cluster_lines"
This is ksh93 syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Typeset is not working in Linux korn shell scripting

Hi All, Kindly help on the below issue ASAP. Its very urgent. I have script in which we have below two lines for code and it is not working. Please help. files_to_process=" abc_*.log def_*.log ghi_*.log " typeset -A dir_list ${files_to_process} the script is failing in Linux... (3 Replies)
Discussion started by: tvbhkishore
3 Replies

2. Shell Programming and Scripting

Command substitution inside of a variable expression (AIX, KORN)

Hello all. This is my first post/question on this site. I’m a new Systems Analyst with previous experience with BASH. Although now I'm using AIX, and I’m trying to get a feel for the Korn shell (for those of you that don’t know AIX only uses the KORN shell). I hope I put this into the correct... (10 Replies)
Discussion started by: sydox
10 Replies

3. Shell Programming and Scripting

Korn shell behaviour in AIX

Hi, Consider the code snippet below: fun() { while read x do echo $x done < somefile_that_does_not_exists } fun echo I am here Korn shell on HPUX prints the message "I am here", while the behaviour is different on AIX korn shell. We do not get the message on AIX. Any... (5 Replies)
Discussion started by: 116@434
5 Replies

4. UNIX for Dummies Questions & Answers

List of 'if -f' options - AIX / Korn Shell

Hi all, Can someone point me in the right direction for a manual on the various statement options for 'if'. Basically I have a piece of code which says: if ] and I wondered what the -f was. I know the '!' means not equal.. As usual any help much appreciated.. (5 Replies)
Discussion started by: Grueben
5 Replies

5. Shell Programming and Scripting

how the typeset command works in shell script

typeset -l section section=${2:-.} what does these 2 lines meaning? (1 Reply)
Discussion started by: venkatababu
1 Replies

6. Shell Programming and Scripting

How to activate Korn Shell functionnalities in Bourne Shell

Hi All I have writing a Korn Shell script to execute it on many of our servers. But some servers don't have Korn Shell installed, they use Borne Shell. Some operations like calculation don't work : cat ${file1} | tail -$((${num1}-${num2})) > ${file2} Is it possible to activate Korn Shell... (3 Replies)
Discussion started by: madmat
3 Replies

7. Shell Programming and Scripting

AIX Korn shell sed -s problem

In a Korn shell script I have, cat ../header | sed -e 's/flag1/$cnumb/g' > header.txt The header is short {{Company flag1}} But the result in header.txt is {{Company $cnumb}} The value of $cnumb is 120. I am trying to get the value of $cnumb into the header. I have tried /'$cnumb'/g,... (10 Replies)
Discussion started by: jcarrott
10 Replies

8. AIX

AIX 4.2 Korn shell and grep question

Ho do I find out the verion of the Kron shell on my client`s system ? There is no one to ask. They are not knowledged enough (hard to believe but yes). Also, on that AIX 4.2, I am trying to figure out how to do a grep using a search patter like below but does not seam to work. The '*' do... (11 Replies)
Discussion started by: Browser_ice
11 Replies

9. Shell Programming and Scripting

grep, awk, typeset in a shell script..

For e.g I have a file named "relation" which has three coloums i.e JOHN MARY JACK PETE ALISIA JONNY TONY JACKIE VICTOR If I do grep -w 'JOHN' relation | awk '{print""$1" is husband of "$2" & father of "$3""}' It gives out JOHN i husband of MARY & father of JACK (which is desired... (7 Replies)
Discussion started by: nick_25
7 Replies

10. Shell Programming and Scripting

how to convert from korn shell to normal shell with this code?

well i have this code here..and it works fine in kornshell.. #!/bin/ksh home=c:/..../ input=$1 sed '1,3d' $input > $1.out line="" cat $1.out | while read a do line="$line $a" done echo $line > $1 rm $1.out however...now i want it just in normal sh mode..how to convert this?... (21 Replies)
Discussion started by: forevercalz
21 Replies
Login or Register to Ask a Question