Problem with sort-of array in SH


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with sort-of array in SH
# 1  
Old 04-19-2010
Problem with sort-of array in SH

Hello to everyone!

I'm really new in shell scripting and I'm experiencing a very odd problem. This is my first post in this forum, hope you can help!

I know that declaring arrays in Bourne Shell is impossible. But this is where I start having problems - system administrator did not install ksh. So after searching some in google, I found an alternative way to declare an array, however I do not know if this is gonna work.

Code:
for i in $*
do
for word in $i
do
echo "$word"
done
done

It reads values from command line and prints them out. I need characters entered in command line to be printed out in one line without spaces and I've done it by myself too:

Code:
echo "Formatted text:"
for var in $*
do
printf "%s$var"
done > "text"
cat text

But what about arrays? Would this work for me? (Found it by googling):

Code:
echo "Enter the line: \c"
read temp
n=0
for word in $temp
do
n=`expr $n + 1`
   eval part$n="$word"
done

All I could achieve was printing out the number of $n which was storing information. Creating another loop below this code trying to take values from part$n or $part$n did not give any results:

Code:
for var in $part$n
do
printf "%s$var"
done

Also, what exactly 'expr' and 'eval' stands for?

Any help would be appreciated,

onstock

Last edited by onstock; 04-19-2010 at 04:48 AM..
# 2  
Old 04-19-2010
On what platform are you running?
# 3  
Old 04-19-2010
Code:
echo $* | tr -d ' '

js.

---------- Post updated at 10:23 AM ---------- Previous update was at 10:20 AM ----------

Here's another in bash (not bourne):
Code:
var="$*"
echo ${var// /}

js.

Last edited by zaxxon; 04-19-2010 at 11:26 AM.. Reason: use code tags please, ty
# 4  
Old 04-19-2010
Quote:
Originally Posted by vbe
On what platform are you running?
It's FreeBSD 6.2-RELEASE-p3 (GENERIC) #0

@jsmoriss

Thank you, I will try it out as soon as I get back home! Smilie

Code:
echo $* | tr -d ' '

Works great and efficient. Thank you! But I'm wondering if there is something that could be done in sort-of array (code written in my first post - code #3)? If it is possible to use it, I could use it for my further studying of scripting.

Thanks again for your help Smilie

Last edited by onstock; 04-19-2010 at 03:01 PM..
# 5  
Old 04-19-2010
Quote:
Originally Posted by onstock
Works great and efficient. Thank you! But I'm wondering if there is something that could be done in sort-of array (code written in my first post - code #3)? If it is possible to use it, I could use it for my further studying of scripting.
You want to create "array like variables" in bourne shell? Hm. How about this?

Code:
n=0
for val in a b c
do 
    eval var$n="$val"
    n="`expr $n + 1`"
done

That would give you 3 variables, in this example, $var0, $var1, and $var2.

js.
# 6  
Old 04-19-2010
jsmoriss,

Thank you for your fast reply!

Your example works, but as I mentioned in my first post, I have troubles with printing out values:

Quote:
All I could achieve was printing out the number of $n which was storing information.
Code:
for val in $*
do
        eval var$n="$val"
        n="`expr $n + 1`"
        echo $val$n
done

And the result is:

Code:
hallerx@ktl ~ $ sh dir1 10 5 9
1
2
3

I am sure I'm calling the output in a wrong way (echo line). How could I print out the values stored in this array?

Thanks for your help Smilie
# 7  
Old 04-19-2010
Try something like this:

Code:
echo "Enter the line: "
read temp
n=0
for word in $temp
do
  n=`expr $n + 1`
  eval part$n="$word"
done

i=1
while [ $i -le $n ]
do
  eval echo "\$part$i"
  i=`expr $i + 1`
done

Code:
eval string ...
            Concatenate all the arguments with spaces.  Then re-parse and execute the command.

Eval first evaluates the variables and the executes the result, a kind of execute in two stages.

Probably you are running posix, not bourne shell which would allow you to lose the expr statements:

instead of
Code:
n=`expr $n + 1`

you could use
Code:
n=$((n + 1))

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort multidimensional Array

Hello I have a problem. I create a Multidimensional Array Like this: ENTRY="$kunnum-$host" ENTRY="$host" ENTRY="# $3" for key in "${!ENTRY}"; do ENTRIES=${ENTRY} # INDEX=IP(5) donedeclare -p declare -A ENTRIES=(="unas15533" ="unas" ="# RDP-Terminal 2"... (12 Replies)
Discussion started by: Marti95
12 Replies

2. UNIX for Beginners Questions & Answers

Array length: ls and sort

Hi there, I'm listing files and sorting them. When I try to get length of array variable in which these files are stored I get 1 as value. That's weird. files_info="$(find $input_dir -name "*_CHR$i.info" | sort )" printf ${#files_info}"\n" #print length #--loop through... (6 Replies)
Discussion started by: genome
6 Replies

3. UNIX for Dummies Questions & Answers

Sort array elements from same field

Hi, input: line1|error_type_a@15 line1|error_type_c@10 line1|error_type_b@5 line2|error_type_f@3 line2|error_type_a@1 I would need to place all the second fields with common first field on the same line, BUT with sorted error position number: line1|error_type_b@5; error_type_c@10;... (5 Replies)
Discussion started by: beca123456
5 Replies

4. Shell Programming and Scripting

awk array sort

I have a file as below Input File: 5/11/2012, dir1, 134 5/11/2012, dir2, 2341 5/11/2012, dir3, 2134 5/11/2012, dir4, 2334 5/12/2012, dir1, 234 5/12/2012, dir2, 2341 5/12/2012, dir3, 2334 5/13/2012, dir1, 234 5/13/2012, dir2, 2341 5/13/2012, dir3, 2334 5/13/2012, dir4, 21134 Now... (6 Replies)
Discussion started by: chakrapani
6 Replies

5. Programming

Merge sort when array starts from zero(0) index???

Hi friends, I have implemented the merge sort algorith in c, before I put forward my question, you please have a look at my code. // The array is sorted, as 1234 #include <stdio.h> #include <stdlib.h> #include <math.h> int A = {4, 3, 2, 1}; void Merge_Sort(int , int, int); void... (0 Replies)
Discussion started by: gabam
0 Replies

6. Shell Programming and Scripting

sort piping to awk array - help please

Hi I'm just learning programming and need some help. I've taken a data file which has a list of numbers eg: 3 5 32 533 13 2 And I've used sort -n and to sort and then piped it to awk to arrange into an array. #!/bin/sh sort -n data.txt | awk ' { array=$1 } (4 Replies)
Discussion started by: EL_Chemso
4 Replies

7. Shell Programming and Scripting

perl sort array by field

Hi Everyone, Any simple code can simplify the code below, please advice. Thanks # cat 2.pl #!/usr/bin/perl use warnings; use strict; my @aaaaa = <DATA>; my @uids; foreach (@aaaaa) { my @ccccc = split (",", $_); push @uids, $ccccc;... (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. UNIX for Dummies Questions & Answers

Sort after 2. column in array in Perl

Hey How do I sort an array numerically after the second column? My values are integers like 1, 2, 3, 4... and they are not unique, so I can't just reverse my hash and sort by keys. I wanna sort my file/array so that I get the lines with the highest value in the top - that is descending. ... (2 Replies)
Discussion started by: Banni
2 Replies

9. Shell Programming and Scripting

How To Sort Array of Hashes ??

Some one plz help me how to sort an array of hashes ..... for e.g i have an array as @AoH = ( { ques => 10, marks => 32, }, { ques => 32, marks => 22, }, { ques => 2, marks => 41, }, ); now i want to sort this array with increasing value of "ques" ..... plz... (3 Replies)
Discussion started by: coolguyshail
3 Replies

10. Shell Programming and Scripting

sort() array of strings in perl

I have a perl script with an array of clients. @arr = ("The ABC Corp", "AA Corp.", "BB Corp"); If I run @a = sort (@arr); I will get @a = ("AA Corp", "BB Corp", "The ABC Corp"); but I want @a = ("AA Corp, "The ABC Corp", "BB Corp"); How do I sort array without changing... (2 Replies)
Discussion started by: photon
2 Replies
Login or Register to Ask a Question