0

I have an if then looking for a certain set of files. They all start with lib, so I could either set an if then up for each one or I was hoping there's a way I can use an asterisk.

Here's what I have and what I tried.

 d=myfolder
 if [[ -e $d/lib* ]]; then
  mv /etc/launchd.conf $d/launchd.conf
 else
  2>/dev/null; true
 fi

so as you can see, if any of those lib files are present I want to move the launchd.conf file as well. but the asterisk isn't doing it. Any ideas?

1 Answer 1

1

From man bash

[[ expression ]]
      Return a status of 0 or 1 depending on the evaluation of the conditional 
      expression expression.  Expressions  are composed  of  the primaries 
      described below under CONDITIONAL EXPRESSIONS. Word splitting and pathname
      expansion are not performed on the words between the [[ and ]]; tilde 
      expansion, parameter and variable  expansion,  arithmetic expansion, command 
      substitution, process substitution, and quote removal are performed.
      Conditional operators such as -f must be unquoted to be recognized as primaries.

Or in short, the pattern is not expanded.

A simple alternative could look like

if [[ $(echo $d/lib*) != "$d/lib*" ]]; then
    mv /etc/launchd.conf $d/launchd.conf
fi

Short explanation on how this works:

  • echo $d/lib* expands to a list of library files if any are present, this list is not equal to $d/lib* (which doesn't get expanded) so the mv is executed
  • OTOH if no library files are present, the echo returns the string $d/lib*, the != evaluates to false and the mv is not executed
5
  • I'm attempting to implement the alternative you provided but it's not quite doing what I need. Also, I may need to clarify, I'm using the asterisk to append the file-name, not to find an executable. I'm going to attempt to explain what the alternative does to see if I'm understanding it right.
    – ElRojito
    Nov 17, 2014 at 19:50
  • I may have misread your question but isn't your goal to see whether there are any files matching $d/lib*?
    – nohillside
    Nov 17, 2014 at 19:54
  • Sorry, I waited too long to add the end of my comment haha. the goal is to see if there are any files matching $d/lib... because there are multiple files that all start with lib but end in something else. Examples: libgenkit.dylib, libgenkitsa.dylib, libimckit.dylib, libimckitsa.dylib. So, if any of those files are in the $d folder, I want to move launchd.conf. If they aren't, do nothing. if the variable (myfolder) with /lib* attached is not the same as myfolder/lib* (thus, if that is true) then move launchd.conf.
    – ElRojito
    Nov 17, 2014 at 19:58
  • Which is, if I didn't make a typo of some sort, what the alternative should do. echo $d/lib* expands to a list of library files if any are present, this list is not equal to $d/lib* (which doesn't get expanded) so the mv is executed. OTOH if no library files are present, the echo returns $d/lib*, the != evaluates to false and the mv is not executed. At least that's how it did work here :-)
    – nohillside
    Nov 17, 2014 at 20:05
  • 1
    Patrix, thank you for the response. It did work, the issue I was running into was I didn't correctly escape the quotes since it's being run in a do shell script command in AppleScript.
    – ElRojito
    Nov 17, 2014 at 22:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .