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 08-18-2014, 06:33 PM   #1
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
BASH: $(cmd) not working in script? or What have I missed?


I've been for a while now, and I can't see what's wrong here:
Code:
#!/bin/bash
include()
{
  local f abort loc t found
  local -a search_locations=(./ ${HOME}/Scripts/bash/ /usr/share/bash/)
  abort=0
  for f in "$@"
  do
    echo "Looking for " \"${f}\"
    #Search for $f in the specified search locations.
    echo find ${search_locations[@]} -name \""${f}"\" -print 2>/dev/null
    t=$(find ${search_locations[@]} -name \""${f}"\" -print 2>/dev/null)
    echo t=\"$t\"
    found=0
    for loc in ${t}
    do
      echo loc=\"${loc}\"
      if [ -z "${loc}" ]
      then
        continue
      else
        echo Including ${loc}
        source "\"${loc}\""
        ((++found))
        break
      fi
    done
    ((found==0)) && echo \"${f}\" could not be located. >&2 && ((++abort))
  done
  ((abort != 0)) && exit ${abort}
}
include trace.sh
Gives me this output:
Code:
$ bash puzzle.sh 
Looking for  "trace.sh"
find ./ /home/Peter/Scripts/bash/ /usr/share/bash/ -name "trace.sh" -print
t=""
"trace.sh" could not be located.
However this is the result from a terminal session:
Code:
$ t=$(find ./ /home/Peter/Scripts/bash/ /usr/share/bash/ -name "trace.sh" -print 2>/dev/null);echo t=\"$t\"
t="/usr/share/bash/trace.sh"
Oh, not that I think it's significant:
Code:
$ bash --version
GNU bash, version 4.3.22(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
 
Old 08-18-2014, 07:50 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
I don't think that you want the quotes on the shell command lines. ${f} and ${loc} should be sufficient. The way you have it isn't find searching for a file named "trace.sh", quotes included.
 
Old 08-18-2014, 07:51 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
find searches for "trace.sh" (inc the quotes), not trace.sh, no?
edit, yes as norobro said
 
Old 08-18-2014, 08:07 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
My guess is your quote delimiting is causing the problem.

When you echo \""${f}"\"
You're delimiting the quotes so that it actually prints:
"trace.sh"
Including the quotes

When you send this to find, you're actually searching for the literal string "trace.sh", including the quotes. Since the file "trace.sh" (including the quotes) does not exist, it can't find a match. If you remove the delimiters in your actual find command you should fix the problem:
Code:
t=$(find ${search_locations[@]} -name "${f}" -print 2>/dev/null)
edit: beat to the punch
 
Old 08-18-2014, 09:10 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Original Poster
Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
O.K., that got it. Thanks.
 
Old 08-19-2014, 09:23 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I was a bit late to this and the solution has been provided, but I thought I would add for others who may come across thread:

1. Whenever unsure about exactly what you script is doing, first rule of thumb should be to use - - set -xv - - this would have identified what exactly was being passed to find (in this case)

2. The use of a for loop to read through items may be an issue if word splitting could be involved, so a while loop would have been better (here we would assume the OP can guarantee no spaces or such in the path to file)

3. A for loop will only populate the variable with items from the list provided, so a test to check if the variable is empty will never be true - - if [ -z "${loc}" ]

4. Additional quotes around the variable being sourced are redundant and may well cause similar issues to those found previously with find - - source "\"${loc}\""

5. Unless being worried about Posix compliance or running on a much older version of bash, one should favour [[]] over [] (list of differences can be found here)

6. Not knowing when the include function will be called, this is a just in case message. Should the function receive more than 255 parameters, the exit function at the end of the loop will hit the below issue:
Quote:
If n is specified, but its value is not between 0 and 255 inclusively, the exit status is undefined.
 
  


Reply

Tags
bash



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
Issue sg_modes cmd at cmd line, want to see the cmd in binary form NuUser Linux - Newbie 1 03-28-2012 08:08 AM
[SOLVED] find in bash script returns "missing argument to `-exec'" while cmd runs fine zhjim Programming 11 01-31-2012 08:56 AM
Mirror script missed last 64-current changelog update Biggen Slackware 4 07-20-2009 06:16 PM
bash script not working :'( aesahaettr Linux - Software 5 05-12-2004 01:08 AM
Bash script not working AMMullan Linux - General 2 09-28-2003 07:10 PM

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

All times are GMT -5. The time now is 10:31 AM.

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