LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2019, 01:13 PM   #1
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Question Recursion in Bash: share your opinion.


My own is it should be avoided. As I think recursion is being done by sub shell calls. But then there is problem with script termination cause Ctrl+C kills only low bottom shell. But maybe I am mistaken. I think it is not harmful if from very beginning it is known that recursion is never deep. Here balance is simple code with recursion against more complicated code without recursion. It is not only theoretical problem. I just recently have seen bash script with recursion - I am not quite sure what to think about this and only my own opinion can be biased.
 
Old 11-15-2019, 02:12 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Mostly recursion is realized with a function - not with calling the own script as a sub shell.
A call to a function only puts something on a stack.
If local variables are used in the function then you can realize powerful things with recursion.
 
2 members found this post helpful.
Old 11-15-2019, 02:18 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
"Recursion in bash", or any language, does not have a single meaning for everyone, so your question does not have a clear answer.

It would be helpful if you gave an example of the use of recursion which prompted your question.

Last edited by astrogeek; 11-15-2019 at 02:24 PM.
 
Old 11-15-2019, 05:39 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
trap "kill 0" SIGINT
or
Code:
trap "kill 0" SIGINT EXIT
or instead of "kill 0" , start a function to perform cleanups
 
Old 11-15-2019, 10:04 PM   #5
bgstack15
Member
 
Registered: Jul 2017
Distribution: korora
Posts: 90

Rep: Reputation: Disabled
I use plenty of functions in shell scripts. I can't think of any that call themselves though. I guess I just haven't had any use cases for such a thing when writing in shell (bash or bourne shell). I have written a script that takes parameters, and can call itself with a "--child" parameter, which makes the child backgrounded invocation act differently, and then sets a number of traps to receive signals from the master script. I was rather fond of it but I ultimately rewrote that architecture so it's not in use anymore with the self-calling script.
 
Old 11-15-2019, 11:12 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Sort of recursion: a script might check the running user: if it is `root` then execute itself with `su - oracle -c "$0 $@"` to switch to the correct user.
 
Old 11-16-2019, 03:11 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
again a useless pseudo-theoretical question?
recursion itself is fine, as long as it is correctly implemented. There is nothing wrong with it. The language itself is completely irrelevant.
 
5 members found this post helpful.
Old 11-17-2019, 06:14 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
The following is an example for recursion with a function:
Code:
#!/bin/bash
# check_file_queues.sh - Nagios plugin
# checks the #files in /var/{spool,log,adm,crash,opt} and subdirectories
# skips NFS-mounted sub dirs and prunes at a depth of 4

set +f
PATH=/bin:/usr/bin:/usr/sbin:/sbin
export PATH

case $1 in
(-*) echo "Usage: $0 [#files]"; exit 3;;
(*[!0-9]*) echo "$0: '$1' is not an integer"; exit 3;;
esac

maxfiles=${1:-1000}

for mtab in /etc/mnttab /etc/mtab /proc/mounts
do
  [ -r $mtab ] && break
done
if [ ! -r $mtab ]
then
  mtab=/tmp/mtab.$$
  trap "rm -f $mtab" 0 HUP INT SYS
  mount | awk '$1~/^\//' > $mtab
fi

nfsmnt=`awk '($2~/^\/var\// && $3=="nfs") {printf " %s ", $2}' $mtab`

rdepth=0
recurse(){
 local i cnt=0
 for i in "$1"/*
 do
  [ -h "$i" ] && continue
  if [ -d "$i" ]; then
   [ $((rdepth++)) -lt 4 ] && [[ $nfsmnt != *" $i "* ]] && recurse "$i"
   ((rdepth--))
  else
   ((cnt++))
  fi
 done
 if [ $cnt -ge $maxfiles ]; then
  echo "WARNING: $1 has $cnt files"
  exit 1
 fi
} 

recurse /var/spool
recurse /var/log
recurse /var/adm
recurse /var/crash
recurse /var/opt
echo "OK: less than $maxfiles files in /var dirs"
 
2 members found this post helpful.
Old 11-18-2019, 09:03 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Personally I wouldn't do any programming in shell apart from very general stuff.
Why not use a better tool for the job?

It's just amateur
 
Old 12-01-2019, 01:24 AM   #10
Rexfree
LQ Newbie
 
Registered: Dec 2019
Location: London
Posts: 3

Rep: Reputation: 0
Smile Thanks

Quote:
Originally Posted by NevemTeve View Post
Sort of recursion: a script might check the running user: if it is `root` then execute itself with `su - oracle -c "$0 $@"` to switch to the correct user.

Wow, thanks. This is exactly what I was looking for
 
Old 12-01-2019, 06:50 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
I'm glad it was useful, here is a more complete version
Code:
#!/bin/sh

if [ "$(id -u)" -eq 0 ]; then
    echo 'root: switch to projects'
    exec su - projects -c "$0 $*"
    exit
fi
id -a
printf -- "#%s#\n" "$@"
You can see its weak point: parameters with spaces or quotes aren't forwarded properly.

Last edited by NevemTeve; 12-01-2019 at 06:51 AM.
 
Old 12-01-2019, 07:11 AM   #12
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,765

Rep: Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764
I have built both kinds of solutions using BASH or KSH in different environments: using recursive functions or a script that called itself. It is more important in a script using recursion to limit the levels of recursion than in a compiled language, but it is important in both. The important point is not "it uses recursion", it is that it solves the problem and avoids adding new issues or vulnerabilities. I was a bit surprised when I discovered just how powerful BASH could be as a tool to solve problems I would otherwise have used C or JAVA to address, and lighter, faster, and more maintainable than either for certain specific cases.

Lessons: 1. never do recursion for recursions sake. Only use it when you need it to solve the problem, or when it generates the BEST solution. 2. Do not fear your tools, understand them. Never be afraid to use what they can do for you. 3. never neglect your error checking or avoidance, even for a prototype. I cannot enumerate how many "first draft" solutions I saw go into production because the fell into a managers hands before they were ready. After this many years I can now laugh at the results, but in the moment it is NOT funny. Avoid this scenario.
 
1 members found this post helpful.
Old 12-01-2019, 12:03 PM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
Quote:
Originally Posted by bigearsbilly View Post
Personally I wouldn't do any programming in shell apart from very general stuff.
Why not use a better tool for the job?

It's just amateur
hm. although it is completely offtopic here, shell is the best tool in a lot of cases.
Obviously it has some limitations, and there are cases, when you need another language. But otherwise you can definitely write even professional programs in shell.
 
2 members found this post helpful.
Old 12-05-2019, 01:05 AM   #14
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Arguably the whole of most GNU/Linux installations could be called amateur :/
But then, one should check one's /usr/bin, /usr/sbin and what have you directories for shell scripts:
Code:
for i in /usr/*bin/*; do [ -f "$i" ] && [ -x "$i" ] && head -1 "$i" | grep -I '^#.*sh'; done
 
Old 12-05-2019, 06:32 AM   #15
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,765

Rep: Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764
Quote:
Originally Posted by ondoho View Post
Arguably the whole of most GNU/Linux installations could be called amateur :/
But then, one should check one's /usr/bin, /usr/sbin and what have you directories for shell scripts:
Code:
for i in /usr/*bin/*; do [ -f "$i" ] && [ -x "$i" ] && head -1 "$i" | grep -I '^#.*sh'; done
A better test would involve "file". Something as simple as
Code:
file /usr/bin/* | grep shell
will find all of the shell scripts. Sonce the BASH scripts are identified as "Bourne-Again shell script" in my version, my identification of bash shells in a folder might look like
Code:
file /usr/bin/* | grep Bourne
One should not, however, expect to find any examples of recursion in those scripts.

Last edited by wpeckham; 12-05-2019 at 06:34 AM.
 
  


Reply

Tags
bash recursion



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New to Bash | Need help with recursion. VauxhallVXR Programming 2 05-30-2009 05:47 PM
using recursion in bash script drkstr Linux - Software 4 07-09-2006 08:48 PM
Bash Local Variable Recursion With Array jshivers Programming 0 06-16-2006 04:31 PM
tar: '--no-recursion' option doesn't prevent recursion Earl Parker II Slackware 12 08-17-2004 02:49 AM
Writing bash script with recursion.. ray5_83 Programming 4 08-04-2004 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration