ksh : Building an array based on condition result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh : Building an array based on condition result
# 1  
Old 02-18-2014
ksh : Building an array based on condition result

I want to build an Errorlog. I would like to build an array as I move through the if statements and print the array once all error conditions have been defined. The results need to be comma delimited.

tsver will be static "1.9.6(2)"
other vars $prit $lt $rt can have the same or a different value as tsver.
For those vars not matching tsver, their values should be written to a csv.


Example errorlog file
Quote:
name,ip,pri,left,right,
cow,10.9.1.135,,,,
sheep,10.9.1.139,,,,
hen,10.9.1.141,1.3.6(2),,,
rooster,10.9.1.158,,,,
duck,10.9.1.162,,1.1.1(2),,
pig,10.9.1.164,,1.9.5(2),,
horse,10.9.1.171,,,2.2.2(2),

This code works, but it seems cheep .... My question is how do I build an array in ksh as I step through the version mismatch tests ?

Code:
 if [[ "$prit" != "$tsver" ]]
   then 
   echo -n "$prit," >> Errorlog
   else 
   echo -n "," >> Errorlog
  fi
#
if [[ "$lt" != "$tsver" ]]
   then
   echo -n "$lt," >> Errorlog
   else
   echo -n "," >> Errorlog
  fi

#
if [[ "$rt" != "$tsver" ]]
   then
   echo -n "$rt," >> Errorlog
   else
   echo -n "," >> Errorlog
  fi

Looking through Unix shell by Quigley and searching the web. But havent found anything that gives a good example.


THANKS !!
# 2  
Old 02-18-2014
This doesn't seem to need any array. Here is what I would do... (untested)
Code:
output=""
[[ "$prit" != "$tsver" ]] && output="${output}${prit}"
output="${output},"
[[ "$lt" != "$tsver" ]] && output="${output}${lt}"
output="${output},"
[[ "$rt" != "$tsver" ]] && output="${output}${rt}"
output="${output},"
echo $output >> Errorlog

Whatever you do you need one last echo with no -n so you terminate your line.
This User Gave Thanks to Perderabo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

If condition with array

hi, say if my array has the following integer values.array=( 5 10 15 20 )and A assigns to 5.A=5How do I if condition to check against if value A is in this array or not, in shell script? thx (5 Replies)
Discussion started by: martin5kh
5 Replies

2. Shell Programming and Scripting

ksh - building a var

This works #!/bin/ksh FILE="file.txt" dosumtin () { date >> FILE } for i in {1..5} do dosumtin done cat $FILE But instead of building a file, I want to do the same with a var or an array. That is, to build one that saves all 5 of the subs execution responses in a var or an... (8 Replies)
Discussion started by: popeye
8 Replies

3. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

4. Shell Programming and Scripting

how to output the array result into a matrix

I have a file like this: ASSPASVFETQY,hTRBV12-4,hTRBJ2-5,2 ASSPASTGGDYGYT,hTRBV18,hTRBJ1-2,2 ASSPASGDGYT,hTRBV5-1,hTRBJ1-2,2 ASSPASFPEDTQY,hTRBV27,hTRBJ2-3,2 ASSPARVNYGYT,hTRBV5-1,hTRBJ1-2,2 ASSPARTSGGLNEQF,hTRBV6-4,hTRBJ2-1,2 ASSPARQSYNEQF,hTRBV11-1,hTRBJ2-1,2... (3 Replies)
Discussion started by: xshang
3 Replies

5. Shell Programming and Scripting

ksh: how to extract strings from each line based on a condition

Hi , I'm a newbie.Never worked on Unix before. I want a shell script to perform the following: I want to extract strings from each line ,based on the type of line(Nameline,Subline) and output it to another file.Below is a sample format. 2010-12-21 14:00"1"Nameline"Midterm"First Name:Jane ... (4 Replies)
Discussion started by: angie1234
4 Replies

6. Shell Programming and Scripting

Result set into an array

Hi, I have an issue with the result set. I wanted to run db2 query against db2 server in unix environment using perl script. I wanted to get the result set into an array. $db=<<DB_Name>> connect to $db get connection state this is my query = SELECT DISTINCT 'R' FROM... (0 Replies)
Discussion started by: solo123
0 Replies

7. Shell Programming and Scripting

Filter the column and print the result based on condition

Hi all This is my output of the some SQL Query TABLESPACE_NAME FILE_NAME TOTALSPACE FREESPACE USEDSPACE Free ------------------------- ------------------------------------------------------- ---------- --------- ---------... (2 Replies)
Discussion started by: jhon
2 Replies

8. Solaris

building solaris-based enterprise router-firewall project

hi guys, its been a while since my last visit here, could not keep up the pace on this ever changing industry :) i'd just doing my home research under vmware to make a solaris-based router-firewall using zones - doing a lot of reading about zones & review solaris zone functionality. and... (4 Replies)
Discussion started by: stdout
4 Replies

9. Shell Programming and Scripting

Prase a file and store and result to an array

Dear all, I have a file having the following formats: ThreadFail=Web1=1234 ThreadFail=Web2=2345 ThreadFail=Web3=12 ConnectionFail=DB1=11 ConnectionFail=DB2=22 The number of lines will be different from every time . How can I parse the file and store the result to an a array inside... (6 Replies)
Discussion started by: youareapkman
6 Replies

10. Shell Programming and Scripting

Prase a file and stored the result to an array

Dear all , I have a file whose content has the following format: jboss.web:type=ThreadPool,name=AAAA jboss.web:type=ThreadPool,name=BBBB How can I parse this file to get the value of the name of each line (AAAA , BBBB) and store the result to an array ? (array = AAAA , array = BBBB).... (4 Replies)
Discussion started by: youareapkman
4 Replies
Login or Register to Ask a Question