Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: if statement execute command and function

I've run into an issue in which I think should be easy to resolve, but for the life of me, I can't figure it out. Could be that it's really late; not sure.

So I have a shell script and I have an if statement that I need to run. The problem is that I have a function inside this bash script that I am using to actually build part of this find command inside the if statement. I want to know how I can do both, without receiving an error [: too many arguments.

Here's the current code:

if [ -n `find ./ `build_ext_names`` ];then

That's all I really need to post. I need to figure out how to run that build_ext_names inside that find command, which in-turn is inside the ifstatement

like image 529
drewrockshard Avatar asked Dec 09 '22 07:12

drewrockshard


2 Answers

Michael Aaron Safyan has the right idea, but to fix the immediate problem you can just use the simpler $(command) construct instead of ```command` `` for command substitution. It allows for much simpler nesting:

if [ -n "$(find ./ "$(build_ext_names)")" ]; then
like image 114
l0b0 Avatar answered Jan 11 '23 05:01

l0b0


This is easier if you split it up:

function whateverItIsYouAreTryingToDo() {
   local ext_names=$(build_ext_names)
   local find_result=$(find ./ $ext_names)
   if [ -n "$find_result" ]  ; then
       # Contents of if...
   fi
}
like image 44
Michael Aaron Safyan Avatar answered Jan 11 '23 06:01

Michael Aaron Safyan