How to create recursive function call in unix

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












#!/bin/sh
checking()

cd "$1"
for D in *;do
if [ -d "$D" ]; then
cd "$D"
for d in *;do
echo "$d"
if [ -d "$d" ]
then
`checking() $d`
fi
if [ -f "$d" ]
then
file_name=`echo "$d"

a=$(pwd)
checking() $a


I am converting session log binary files into text files and storing on some path using strings function. For every directory it is performing fine,but it is not checking the binary files for any sub directory present.



I tried using recursive function call but that call is not being fired..Please help







share|improve this question
















  • 1




    Well, for one, shell functions act like any other commands, so you'd call a function hello with something like hello arg1 arg2. Bash actually gives you an error message for something() $a, dash crashed when I tried that...
    – ilkkachu
    Nov 29 '17 at 10:18














up vote
1
down vote

favorite












#!/bin/sh
checking()

cd "$1"
for D in *;do
if [ -d "$D" ]; then
cd "$D"
for d in *;do
echo "$d"
if [ -d "$d" ]
then
`checking() $d`
fi
if [ -f "$d" ]
then
file_name=`echo "$d"

a=$(pwd)
checking() $a


I am converting session log binary files into text files and storing on some path using strings function. For every directory it is performing fine,but it is not checking the binary files for any sub directory present.



I tried using recursive function call but that call is not being fired..Please help







share|improve this question
















  • 1




    Well, for one, shell functions act like any other commands, so you'd call a function hello with something like hello arg1 arg2. Bash actually gives you an error message for something() $a, dash crashed when I tried that...
    – ilkkachu
    Nov 29 '17 at 10:18












up vote
1
down vote

favorite









up vote
1
down vote

favorite











#!/bin/sh
checking()

cd "$1"
for D in *;do
if [ -d "$D" ]; then
cd "$D"
for d in *;do
echo "$d"
if [ -d "$d" ]
then
`checking() $d`
fi
if [ -f "$d" ]
then
file_name=`echo "$d"

a=$(pwd)
checking() $a


I am converting session log binary files into text files and storing on some path using strings function. For every directory it is performing fine,but it is not checking the binary files for any sub directory present.



I tried using recursive function call but that call is not being fired..Please help







share|improve this question












#!/bin/sh
checking()

cd "$1"
for D in *;do
if [ -d "$D" ]; then
cd "$D"
for d in *;do
echo "$d"
if [ -d "$d" ]
then
`checking() $d`
fi
if [ -f "$d" ]
then
file_name=`echo "$d"

a=$(pwd)
checking() $a


I am converting session log binary files into text files and storing on some path using strings function. For every directory it is performing fine,but it is not checking the binary files for any sub directory present.



I tried using recursive function call but that call is not being fired..Please help









share|improve this question











share|improve this question




share|improve this question










asked Nov 29 '17 at 9:48









Paras Singh

61




61







  • 1




    Well, for one, shell functions act like any other commands, so you'd call a function hello with something like hello arg1 arg2. Bash actually gives you an error message for something() $a, dash crashed when I tried that...
    – ilkkachu
    Nov 29 '17 at 10:18












  • 1




    Well, for one, shell functions act like any other commands, so you'd call a function hello with something like hello arg1 arg2. Bash actually gives you an error message for something() $a, dash crashed when I tried that...
    – ilkkachu
    Nov 29 '17 at 10:18







1




1




Well, for one, shell functions act like any other commands, so you'd call a function hello with something like hello arg1 arg2. Bash actually gives you an error message for something() $a, dash crashed when I tried that...
– ilkkachu
Nov 29 '17 at 10:18




Well, for one, shell functions act like any other commands, so you'd call a function hello with something like hello arg1 arg2. Bash actually gives you an error message for something() $a, dash crashed when I tried that...
– ilkkachu
Nov 29 '17 at 10:18










1 Answer
1






active

oldest

votes

















up vote
3
down vote













I note a couple of things in your function:



  • You change into a directory, then iterate over the files, then do both again (cd; for d in *; do cd; for d in * ...). This seems like it would skip every other level of the directory tree during a recursive walk. One iteration over the files should be enough

  • I don't think you need to use a subshell when recursively calling the function (there's the backtick command substitution in `checking() $d`). That could be useful if you needed variables local to each instance of the function call, but many shells can do that directly (using local in Bash, apparently typeset in ksh93)

  • And of course the syntax for calling a function: given hi() echo "hello $1"; we run it like any other command, simply with hi "$name".

  • We can nix the awk and use the shell's parameter expansion to pick the file extension. (but do check the borderline case when there is no dot in the filename.) Or use case "$f" in *.bin) ...

Here's a rather simple function for walking a directory tree:



#!/bin/sh

walk()
cd "$1"
for f in *; do
if [ -d "$f" ] ; then
echo descending into "$f"...
walk "$f"
else
ext=$f##*.
if [ "$ext" = "bin" ] ; then
echo "found .bin file: $f"
fi
fi

done
cd ..


walk .


(Though note that the cd "$dir"; ... ; cd .. structure can cause issues if someone moves a directory right at the moment the program is processing that directory. The .. link would change, and returning through it would continue from the new location, which might even be outside the original tree. We could work around that by using absolute path names all the time, or with pushd/popd (in Bash), or by putting the whole function or calls to it in subshells.)






share|improve this answer






















  • Put the function body within (...) instead of ... to get rid of the need for cd ...
    – Kusalananda
    Nov 29 '17 at 11:00










  • @Kusalananda, well, yeah, that's the subshell solution.
    – ilkkachu
    Nov 29 '17 at 11:14










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407693%2fhow-to-create-recursive-function-call-in-unix%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













I note a couple of things in your function:



  • You change into a directory, then iterate over the files, then do both again (cd; for d in *; do cd; for d in * ...). This seems like it would skip every other level of the directory tree during a recursive walk. One iteration over the files should be enough

  • I don't think you need to use a subshell when recursively calling the function (there's the backtick command substitution in `checking() $d`). That could be useful if you needed variables local to each instance of the function call, but many shells can do that directly (using local in Bash, apparently typeset in ksh93)

  • And of course the syntax for calling a function: given hi() echo "hello $1"; we run it like any other command, simply with hi "$name".

  • We can nix the awk and use the shell's parameter expansion to pick the file extension. (but do check the borderline case when there is no dot in the filename.) Or use case "$f" in *.bin) ...

Here's a rather simple function for walking a directory tree:



#!/bin/sh

walk()
cd "$1"
for f in *; do
if [ -d "$f" ] ; then
echo descending into "$f"...
walk "$f"
else
ext=$f##*.
if [ "$ext" = "bin" ] ; then
echo "found .bin file: $f"
fi
fi

done
cd ..


walk .


(Though note that the cd "$dir"; ... ; cd .. structure can cause issues if someone moves a directory right at the moment the program is processing that directory. The .. link would change, and returning through it would continue from the new location, which might even be outside the original tree. We could work around that by using absolute path names all the time, or with pushd/popd (in Bash), or by putting the whole function or calls to it in subshells.)






share|improve this answer






















  • Put the function body within (...) instead of ... to get rid of the need for cd ...
    – Kusalananda
    Nov 29 '17 at 11:00










  • @Kusalananda, well, yeah, that's the subshell solution.
    – ilkkachu
    Nov 29 '17 at 11:14














up vote
3
down vote













I note a couple of things in your function:



  • You change into a directory, then iterate over the files, then do both again (cd; for d in *; do cd; for d in * ...). This seems like it would skip every other level of the directory tree during a recursive walk. One iteration over the files should be enough

  • I don't think you need to use a subshell when recursively calling the function (there's the backtick command substitution in `checking() $d`). That could be useful if you needed variables local to each instance of the function call, but many shells can do that directly (using local in Bash, apparently typeset in ksh93)

  • And of course the syntax for calling a function: given hi() echo "hello $1"; we run it like any other command, simply with hi "$name".

  • We can nix the awk and use the shell's parameter expansion to pick the file extension. (but do check the borderline case when there is no dot in the filename.) Or use case "$f" in *.bin) ...

Here's a rather simple function for walking a directory tree:



#!/bin/sh

walk()
cd "$1"
for f in *; do
if [ -d "$f" ] ; then
echo descending into "$f"...
walk "$f"
else
ext=$f##*.
if [ "$ext" = "bin" ] ; then
echo "found .bin file: $f"
fi
fi

done
cd ..


walk .


(Though note that the cd "$dir"; ... ; cd .. structure can cause issues if someone moves a directory right at the moment the program is processing that directory. The .. link would change, and returning through it would continue from the new location, which might even be outside the original tree. We could work around that by using absolute path names all the time, or with pushd/popd (in Bash), or by putting the whole function or calls to it in subshells.)






share|improve this answer






















  • Put the function body within (...) instead of ... to get rid of the need for cd ...
    – Kusalananda
    Nov 29 '17 at 11:00










  • @Kusalananda, well, yeah, that's the subshell solution.
    – ilkkachu
    Nov 29 '17 at 11:14












up vote
3
down vote










up vote
3
down vote









I note a couple of things in your function:



  • You change into a directory, then iterate over the files, then do both again (cd; for d in *; do cd; for d in * ...). This seems like it would skip every other level of the directory tree during a recursive walk. One iteration over the files should be enough

  • I don't think you need to use a subshell when recursively calling the function (there's the backtick command substitution in `checking() $d`). That could be useful if you needed variables local to each instance of the function call, but many shells can do that directly (using local in Bash, apparently typeset in ksh93)

  • And of course the syntax for calling a function: given hi() echo "hello $1"; we run it like any other command, simply with hi "$name".

  • We can nix the awk and use the shell's parameter expansion to pick the file extension. (but do check the borderline case when there is no dot in the filename.) Or use case "$f" in *.bin) ...

Here's a rather simple function for walking a directory tree:



#!/bin/sh

walk()
cd "$1"
for f in *; do
if [ -d "$f" ] ; then
echo descending into "$f"...
walk "$f"
else
ext=$f##*.
if [ "$ext" = "bin" ] ; then
echo "found .bin file: $f"
fi
fi

done
cd ..


walk .


(Though note that the cd "$dir"; ... ; cd .. structure can cause issues if someone moves a directory right at the moment the program is processing that directory. The .. link would change, and returning through it would continue from the new location, which might even be outside the original tree. We could work around that by using absolute path names all the time, or with pushd/popd (in Bash), or by putting the whole function or calls to it in subshells.)






share|improve this answer














I note a couple of things in your function:



  • You change into a directory, then iterate over the files, then do both again (cd; for d in *; do cd; for d in * ...). This seems like it would skip every other level of the directory tree during a recursive walk. One iteration over the files should be enough

  • I don't think you need to use a subshell when recursively calling the function (there's the backtick command substitution in `checking() $d`). That could be useful if you needed variables local to each instance of the function call, but many shells can do that directly (using local in Bash, apparently typeset in ksh93)

  • And of course the syntax for calling a function: given hi() echo "hello $1"; we run it like any other command, simply with hi "$name".

  • We can nix the awk and use the shell's parameter expansion to pick the file extension. (but do check the borderline case when there is no dot in the filename.) Or use case "$f" in *.bin) ...

Here's a rather simple function for walking a directory tree:



#!/bin/sh

walk()
cd "$1"
for f in *; do
if [ -d "$f" ] ; then
echo descending into "$f"...
walk "$f"
else
ext=$f##*.
if [ "$ext" = "bin" ] ; then
echo "found .bin file: $f"
fi
fi

done
cd ..


walk .


(Though note that the cd "$dir"; ... ; cd .. structure can cause issues if someone moves a directory right at the moment the program is processing that directory. The .. link would change, and returning through it would continue from the new location, which might even be outside the original tree. We could work around that by using absolute path names all the time, or with pushd/popd (in Bash), or by putting the whole function or calls to it in subshells.)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '17 at 12:13

























answered Nov 29 '17 at 10:38









ilkkachu

50.3k677138




50.3k677138











  • Put the function body within (...) instead of ... to get rid of the need for cd ...
    – Kusalananda
    Nov 29 '17 at 11:00










  • @Kusalananda, well, yeah, that's the subshell solution.
    – ilkkachu
    Nov 29 '17 at 11:14
















  • Put the function body within (...) instead of ... to get rid of the need for cd ...
    – Kusalananda
    Nov 29 '17 at 11:00










  • @Kusalananda, well, yeah, that's the subshell solution.
    – ilkkachu
    Nov 29 '17 at 11:14















Put the function body within (...) instead of ... to get rid of the need for cd ...
– Kusalananda
Nov 29 '17 at 11:00




Put the function body within (...) instead of ... to get rid of the need for cd ...
– Kusalananda
Nov 29 '17 at 11:00












@Kusalananda, well, yeah, that's the subshell solution.
– ilkkachu
Nov 29 '17 at 11:14




@Kusalananda, well, yeah, that's the subshell solution.
– ilkkachu
Nov 29 '17 at 11:14

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407693%2fhow-to-create-recursive-function-call-in-unix%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay