abstract.sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting abstract.sh
# 1  
Old 04-03-2012
abstract.sh

A highly abstract function invocation, just enjoy it ,guys!
Code:
#!/bin/bash

loop()
{
   for((${1}=0; ${1}<${2}; ++${1})); do
        eval \$\{{3..12}\}
   done
}

numb()
{
   echo -n "${!1}${!2}${!3} "
   (( ${3} == 2 )) && echo
}

eval loop" "{i..k}" 3" numb {i..k}

# 2  
Old 04-03-2012
And what is it actually for...? Why use it?

Rule of thumb is, if you're using eval, there's generally a better way.
# 3  
Old 04-03-2012
Quote:
Originally Posted by Corona688
And what is it actually for...? Why use it?

Rule of thumb is, if you're using eval, there's generally a better way.
Just for fun!
This style of coding is very Concise and flexible!Smilie
# 4  
Old 04-03-2012
Quote:
Originally Posted by huaihaizi3
Just for fun!
This style of coding is very Concise and flexible!Smilie
... and if you don't explain it, we will delete it....

This forum is full of busy people, so explain yourself, please.
# 5  
Old 04-03-2012
I was brave enough to run this, and see that it doesn't do anything malign:
Code:
$ ./unknown.sh

000 001 002
010 011 012
020 021 022
100 101 102
110 111 112
120 121 122
200 201 202
210 211 212
220 221 222

$

Though I still don't understand it's purpose.
# 6  
Old 04-03-2012
The most easy way to get the same result is
Code:
echo {0..2}{0..2}{0..2} | xargs -n 3

but, if we don't perform it this way, instead,generally speaking, we must need three nested-loop to accomplish it
Code:
for((i=0; i<3; ++i)); do
   for((j=0; j<3; ++j)); do
      for((k=0; k<3; ++k)); do
          echo -n "${i}${j}${k} "
          (( k == 2 )) && echo
      done
   done
done

My purpose is just to show that there are always better solutions to perform the tasks if only you can use your imagination as much as possible!
The post here is intended to show some core concepts and skills that we can perform in shell scripting!
Feel free to delete it if you consider it's useless, Neo!Smilie
# 7  
Old 04-04-2012
I'd argue there's a difference between 'better' and 'compact', especially when talking about efficiency, scalability, and safety... That'd blow up into swapdeath if you tried to generate millions of numbers, or do terrible things if someone put an `rm -Rf ~/` into that eval.

This isn't quite as brief, but manages 5 numbers in 2 loops, and can be turned higher just by tacking as many variables as you want onto that set.

Code:
#!/bin/bash

set -- A B C D E

while [ "$X" != "Z" ]
do
        [ "$X" = $1 ] && printf "\t"

        for X in $*
        do
                printf "%d" "${!X}"
        done

        for X in $* Z
        do
                [ $X = $2 ] && echo
                (($X=(++$X % $#))) && break
        done
done

Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Programming

Java Abstract Classes

Can anyone tell me if this is correct when creating classes in Java? public abstract class Animal { public class Point { public int x,y; } public class Animal { protected Point loc; protected String name; protected Random rng; String... (3 Replies)
Discussion started by: totoro125
3 Replies

2. Programming

C++ abstract (singleton) factory implementation...

I want to create an abstract factory template which will allow me to pass in an "ID" for a subclass and return the singleton instance of that class stored in the factory. It'd be easy to adapt for "multi-ton", but for its present use this isn't necessary. The requirements are: - I don't want... (2 Replies)
Discussion started by: DreamWarrior
2 Replies

3. Programming

how abstract class differs in Java and C++?

hello all, i want to know if there is any difference in working and syntax declaration of abstract class in Java and C++. (1 Reply)
Discussion started by: haravivar
1 Replies

4. Programming

Compiling multiple cpp files (abstract factory pattern)

Hi all, I have been working with java for awhile and because of my school projects I needed to switch C++. I tried to implement some patterns in C++ but unfortunately I couldn't. Specifically, I tried to implement abstract factory pattern but since I used separated files (habitual behavior from... (4 Replies)
Discussion started by: SaTYR
4 Replies
Login or Register to Ask a Question