4

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

drewrockshard
  • 2,043
  • 10
  • 35
  • 47
  • what does build_ext_names output? – jcomeau_ictx Jul 07 '11 at 06:37
  • It outputs the `( -name filename.ext -o -name filename.otherext )` code. Basically, there's a variable that allows you to put in whatever extensions you want "allowed" and then this function reads in this from an array and builds the code required for that. It handles single and multiple options. It's pretty cool. – drewrockshard Jul 07 '11 at 09:02

2 Answers2

12

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
Community
  • 1
  • 1
l0b0
  • 55,365
  • 30
  • 138
  • 223
2

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
}
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • This is something for me to look into. I could totally utilize this type of function in the future. Glad you opened my eyes to this (local bash variables)! – drewrockshard Jul 07 '11 at 07:19