Challenge: The Phantom Bash Loop

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
It's often I read in this forum where posters are bashing this and that.
Hence lets have a BASH related challenge ?
Someone in another thread inspired this loop without using loop construct.
We have a challenge that was using Javascript, perhaps our lovely shell script (in specific BASH) is worthy of such a challenge too ?

I know we have quite a few unices savvy software engineers around, perhaps it would be a walk in the park for them ?

QUESTION
Please provide a BASH script having the following constraints.
1) Without resorting to any looping constructs and recursion in BASH or any other programs/applications/utilities other than BASH, print out a counting from 1 to N via the standard out (terminal display) when the script is invoked as such

Bash:
$ ./count.sh N

INPUT
0 <= N <= 100000

OUTPUT
Each separate line will show a count from 1 to N should N > 0 and nothing will be printed should N == 0.
Below are samples of the expected output
Bash:
$ ./count.sh 0
$ ./count.sh 1
1
$ ./count.sh 5
1
2
3
4
5
$ ./count.sh 10
1
2
3
4
5
6
7
8
9
10
$ ./count.sh 1000
1
2
3
4
5
6
7
8
9
10
11
...
993
994
995
996
997
998
999
1000
$

NOTE
Any other utilities are like ls, find, awk, etc. If it is not a builtin command in BASH, then it is considered a separate application/program/utilities from the shell script. There are cases where there are built-in commands in BASH that is also found as a separate utility in the unix system, such as echo, kill. In this case, you are free to use these built-in constructs found in BASH. We will stick with BASH version 4.3 and later, using the Linux environment for this challenge. I have also rule out recursion for this challenge because BASH allows for changing of the call stack to be virtually unlimited (obviously is limited by memory). There is something else in unix that will help to solve this problem. The solution can potentially loop forever as long as the system is running, meaning if time is given, your solution should keep running non-stop. However in the case of this question, we have set a max of N.

Happy bashing!
:)
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
can use repeat -1 ??

loop construct is: for, while, until??
Why isn't repeat a loop construct ?

This is what zsh documented in its manual
Code:
break [ n ]
              Exit from an enclosing for, while, until, select or repeat loop. If an arithmetic expression n is specified, then break n levels instead of
              just one.
Read like a loop construct, isn't it ?

UPDATE:
repeat EXPR COMMAND_LIST
is the short form of
repeat EXPR do COMMAND_LIST done

Thus it is definitely a loop construct.

Is that found in bash ?
:)
 
Last edited:

theMKR

Banned
Joined
Nov 3, 2016
Messages
70,933
Reaction score
13,557
then "if" izit a recursive?

lemme guess, its going to be something like

1st make a global variable.

some function()
if i < n
print i

goto some function

i know bash dun have goto, but got alternative way to do it.

liddat izit recursive?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
then "if" izit a recursive?

lemme guess, its going to be something like

1st make a global variable.

some function()
if i < n
print i

goto some function

i know bash dun have goto, but got alternative way to do it.

liddat izit recursive?
As long as it is not a bash looping construct and it is not recursive within the script/code constructs.
You are free to implement, you will see how elegant it is compared to other solutions.
Running the same function multiple times is not recursion

There has to be some form of "looping" going on to repeatedly perform the same task multiple times,
question is how and what approaches are available to you.
There are pretty ways, there are messy ways.

Use your global variable approach if you like, and we see if your code can run multiple copies
concurrently. It's not a requirement, but again, when you compared to other solutions, you will observe
the limitations.

My solution don't even try to be "funny". It is just unix programming implemented using bash script.

:)
 

xcodes

Great Supremacy Member
Joined
Dec 15, 2013
Messages
63,891
Reaction score
29,167
Invitation to @nubitol @xcodes @ng min teck @Trader11 @theMKR
Especially @peterchan75 whom inspired the initial "looping" challenge in another thread. ;)

Of course invitation is not limited to the above, ANYONE is welcome to give it a try.


wow thank you bro davidktw for inviting me ... I'm not strong in bash but let me go revise it and see if I can come back to take up this challenge ... bookmarked this thread first ... :)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
wow thank you bro davidktw for inviting me ... I'm not strong in bash but let me go revise it and see if I can come back to take up this challenge ... bookmarked this thread first ... :)

Sure! Looking forward to your participation.

:)
 

Clarence NMT

Supremacy Member
Joined
Dec 28, 2012
Messages
8,391
Reaction score
2,789
Then really write bash. Only did 3 or 5 bash program. Mostly like 1 or less than 10 line of code or command. Just stackoverflow ones
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Then really write bash. Only did 3 or 5 bash program. Mostly like 1 or less than 10 line of code or command. Just stackoverflow ones

Practise makes perfect.
Shell script is a very interesting language no matter which variant you go for.
If you don't know shell script well, you are simply not a good Unix software engineer.
There are knowledge found in unices not found in M$ Windows. I will share with you guys
when I reveal my solution. Not knowing at least one shell script is handicapped in the unix world.

Feel free to read this post -> A well known algorithm written in shell script.
There are 2 versions of them doing the same thing. One is a lot more performant than the other. Do you know why ?

Hint: The technique I used to solve this problem is not limited to Bash or other shells. I can do the same in Perl, Python, C too.

:)
 
Last edited:

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
OMG! Loop again!
Please tell me honestly. Yes or No?
Did you use this kind of fringe archaic coding technique to torch those interviewees that you dislike? :cautious:
If I give them this question, I will be torturing myself.
You sure the technique used to solve this problem is archaic ? You will be utterly surprised.
:)
 

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
If I give them this question, I will be torturing myself.
You sure the technique used to solve this problem is archaic ? You will be utterly surprised.
:)
It's fringe archaic. :cool: More of an academic exercise and of little practical importance. :) Maybe for just some academic bragging rights. With the current chip technology, hardware resources are no longer a constraint. Efficiency can take a back seat. AMD Epyc has 96 cores and can access upto 2Tb RAM. The current industry trend is low code environment.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
It's fringe archaic. :cool: More of an academic exercise and of little practical importance. :) Maybe for just some academic bragging rights. With the current chip technology, hardware resources are no longer a constraint. Efficiency can take a back seat. AMD Epyc has 96 cores and can access upto 2Tb RAM. The current industry trend is low code environment.

When my solution is out, then you see how archaic is it. :)

Efficiency take a backseat? Speaking from experience dealing with half a million subscribers? I think you are greatly mistaken.
Low code is not the industry trend. Please look around how many low code deployments there are and how much they cost.

None of these exercises is about bragging rights, it is all about how much you have inside that skull of yours and whether your thoughts are trapped inside or outside of it.

When efficiency is not as crucial as compared to the 8bits days, it doesn’t mean we can afford a O(n^2) solution as opposed to a O(n.logN) one. Please don’t be mistaken what it means by efficiency and what kind of efficiency we are talking about. The choice of performance we are talking about are in the realm of what you claimed to be academic but a deciding factor between capable of serving just thousands or serving millions. It surpasses your chip performance technology by a different magnitude.

When you are given an AMD epyc with 96 cores and addressable memory of up to 2TB, it doesn’t mean it comes cheap and definitely doesn’t mean your coding skill can afford to be sloppy. Regardless how much hardware performance is given to you, it still means using top-notch software engineering skill sets to exploit as much as possible out of them. What it only means is software engineers can afford to have more dynamism in their code which often requires more computing power to create expressive codes. For that we are willing to sacrifice in a lot of cases it is easier to share expressive codes than machine codes.
If you think for a moment that we can make do with bubble sort instead of quicksort/merge sort or other such scenarios, then you are gravely mistaken.

Last but not least, when you quote me high cpu cores count and abundant available memory, that doesn’t mean faster per thread/process. It only means you can have more concurrent tasks running. Unless you are capable of writing multithreaded/multiprocessing workflows, your code will still run single threaded. High core counts processor tend to have lower base frequency due to heat limitations, and turbo speeds are achievable only if the thermal envelope is not exceeded. When all the 96 cores are working hard, you can likely kiss the turbo speeds goodbye.

Take my the other SQL challenge. It introduces recursive CTE and uses lateral derived tables which are only found in version 8 for mysql. It also tested on the use of group_concat and the good understanding of how one join tables together, especially for complex ones that may have multiple nested tables. It doesn’t use any keys for joining, in fact uses cross joins which are very seldom used. Most people just know how to join tables via key criteria but doesn’t really appreciate how tables join together. It is done via gamification, but even that only one challenger manage to answer them. I really hope it is the case that others knows but not interested, instead of incapable of solving them.

Btw this question of mine is born out of industrial knowledge, so lets see if you have the industrial knowledge to answer it. If you can't answer it in the way I did with a single liner of bash code, it means you don't know this existed and has been prevalently the underlying technique of a unix system.

:)
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Wall of text! :oops:

It's time to look at low-code or risk being left behind biting dust messing with bolts and nuts.
https://www.prnewswire.com/news-rel...lysis-and-insights---technavio-301599026.html
No going into Risc vs Cisc rabbit hole.

NoSQL to reduce complexity. No?

low code wouldn’t be a replacement for what we have now. simply the business model to sustain low code deployment is non-attractive. I have already looked into it.

RISC and CISC is not a concern for a lot of us since we are mostly working on top of the OS and virtual machines. not the machine codes directly. RISC are beneficial to hosting companies because of the performance to power consumption. AWS is pushing for their graviton instances right now at competitive pricing.

NoSQL doesn’t reduce complexity. In fact it increases complexity to achieve the same as what RDBMS offers. If you are doing analytic works and need to employ map-reduce or cluster solution due to how NoSQL structure in unnormalise forms, the complex adds up. NoSQL is good at some stuffs, but not everything.

:)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
@peterchan75 For you, give you a break. You can solve in Perl with the same restriction. It is either you know or you don’t know. No need to think whether academia or not.

I will show you my solution in both Bash and Perl.

:)
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top