How to create recursive function call in unix
Clash 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
shell-script recursive
add a comment |Â
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
shell-script recursive
1
Well, for one, shell functions act like any other commands, so you'd call a functionhello
with something likehello arg1 arg2
. Bash actually gives you an error message forsomething() $a
,dash
crashed when I tried that...
â ilkkachu
Nov 29 '17 at 10:18
add a comment |Â
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
shell-script recursive
#!/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
shell-script recursive
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 functionhello
with something likehello arg1 arg2
. Bash actually gives you an error message forsomething() $a
,dash
crashed when I tried that...
â ilkkachu
Nov 29 '17 at 10:18
add a comment |Â
1
Well, for one, shell functions act like any other commands, so you'd call a functionhello
with something likehello arg1 arg2
. Bash actually gives you an error message forsomething() $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
add a comment |Â
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 (usinglocal
in Bash, apparentlytypeset
in ksh93) - And of course the syntax for calling a function: given
hi() echo "hello $1";
we run it like any other command, simply withhi "$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 usecase "$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.)
Put the function body within(...)
instead of...
to get rid of the need forcd ..
.
â Kusalananda
Nov 29 '17 at 11:00
@Kusalananda, well, yeah, that's the subshell solution.
â ilkkachu
Nov 29 '17 at 11:14
add a comment |Â
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 (usinglocal
in Bash, apparentlytypeset
in ksh93) - And of course the syntax for calling a function: given
hi() echo "hello $1";
we run it like any other command, simply withhi "$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 usecase "$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.)
Put the function body within(...)
instead of...
to get rid of the need forcd ..
.
â Kusalananda
Nov 29 '17 at 11:00
@Kusalananda, well, yeah, that's the subshell solution.
â ilkkachu
Nov 29 '17 at 11:14
add a comment |Â
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 (usinglocal
in Bash, apparentlytypeset
in ksh93) - And of course the syntax for calling a function: given
hi() echo "hello $1";
we run it like any other command, simply withhi "$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 usecase "$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.)
Put the function body within(...)
instead of...
to get rid of the need forcd ..
.
â Kusalananda
Nov 29 '17 at 11:00
@Kusalananda, well, yeah, that's the subshell solution.
â ilkkachu
Nov 29 '17 at 11:14
add a comment |Â
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 (usinglocal
in Bash, apparentlytypeset
in ksh93) - And of course the syntax for calling a function: given
hi() echo "hello $1";
we run it like any other command, simply withhi "$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 usecase "$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.)
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 (usinglocal
in Bash, apparentlytypeset
in ksh93) - And of course the syntax for calling a function: given
hi() echo "hello $1";
we run it like any other command, simply withhi "$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 usecase "$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.)
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 forcd ..
.
â Kusalananda
Nov 29 '17 at 11:00
@Kusalananda, well, yeah, that's the subshell solution.
â ilkkachu
Nov 29 '17 at 11:14
add a comment |Â
Put the function body within(...)
instead of...
to get rid of the need forcd ..
.
â 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Well, for one, shell functions act like any other commands, so you'd call a function
hello
with something likehello arg1 arg2
. Bash actually gives you an error message forsomething() $a
,dash
crashed when I tried that...â ilkkachu
Nov 29 '17 at 10:18