Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hello- I'm very new to shell scripting in Linux and would greatly appreciate any help. I am trying to write a script that quietly mounts any .dmg in a given ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    15

    I need some help with grep

    Hello-

    I'm very new to shell scripting in Linux and would greatly appreciate any help. I am trying to write a script that quietly mounts any .dmg in a given folder when a user logs into OS X. I've already learned the login hook and I can write a script that mounts .dmgs specified in a script but I can't figure out how to mount all of the files in a given folder. I know it is very simple, proably a line or two and it has to do with piping grep into hdiutil but I can't get the right syntax. In my mind it looks like this:

    hdiutil attach < ls /directory/|grep \.dmg

    This doesn't work though. Any help would be appreciated.

    -= robbie =-

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try this:

    Code:
    ls /directory | grep .dmg | xargs hdiutil attach
    Regards

  3. #3
    Just Joined!
    Join Date
    May 2007
    Posts
    15

    Getting closer...

    Thanks for the response, but I still get an hdiutil error when I execute that command. The exact error is hdiutil: attach failed - No such file or directory. If the directory does not contain any .dmg files it returns no error, but if there are any .dmg files it errors out. Also, shouldn't there be a \ infront of .dmg, I thought . was a wildcard?

    I really really really appreciate the help, xargs seems to be the key to making it work. Time to do some more research...

  4. #4
    Just Joined!
    Join Date
    May 2007
    Posts
    15

    Argh!

    Is there something wrong with hdiutil, does it not work with multiple files? I've simplified this command and it seems that it should be hdiutil mount /directory/*.dmg but it doesn't work with multiple files. If there is only one file in /directory it works, but if multiple files exist it doesn't! I've found several references to using hdiutil like this, even exact examples and they don't work on any system I have access too (I'm in an Apple Store). Please Linux gurus- save my sanity. Thanks in advance.

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Franklin52 View Post
    Try this:

    Code:
    ls /directory | grep .dmg | xargs hdiutil attach
    Regards
    using shell wildcard expansion.
    Code:
    ls /directory/*.dmg | <command...>

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by rkasowan View Post
    Is there something wrong with hdiutil, does it not work with multiple files? I've simplified this command and it seems that it should be hdiutil mount /directory/*.dmg but it doesn't work with multiple files. If there is only one file in /directory it works, but if multiple files exist it doesn't! I've found several references to using hdiutil like this, even exact examples and they don't work on any system I have access too (I'm in an Apple Store). Please Linux gurus- save my sanity. Thanks in advance.
    if you suspect its not working with multiple files, why not use a loop to go through every dmg files?
    Code:
    for dmgfile in `ls /directory/*dmg` 
    do
       your mount command here
    done

  7. #7
    Just Joined!
    Join Date
    May 2007
    Posts
    15

    Revenge of the Argh

    Ok, so now I get a similar message:

    hdiutil: attach: missing image argument
    Usage: hdiutil attach [options] <image>
    hdiutil attach -help


    I don't get it. I've been all over the net today looking for the proper syntax, it should be much easier than this. Is there something wrong/different with my system or my flavor of Linux (Darwin)?

    I'm really new- I might be missing a big something. I know Linux is smarter than I am- this has to be operator error. I'm learning from tutorials onine- they are good but sometimes you get what you pay for. If anyone wants to teach this newb a lesson it would be greatly appreciated.

    Apple OS X 10.4.8

    Here is my script:

    #!/bin/bash
    for dmgfile in `ls /Users/Shared/diskImgs/*dmg`
    do
    hdiutil mount -noverify
    done

    Here is a screen capture of my terminal window:

    localhost:/ imac$ chmod -x test.sh
    localhost:/ imac$ sh test.sh
    hdiutil: attach: missing image argument
    Usage: hdiutil attach [options] <image>
    hdiutil attach -help
    hdiutil: attach: missing image argument
    Usage: hdiutil attach [options] <image>
    hdiutil attach -help

    There are two file in the folder /Users/Shared/diskImgs:

    diskimg1.dmg
    diskimg2.dmg

    I've also tried:

    ls /Users/Shared/diskImgs/*.dmg | hdiutil attach
    ls /Users/Shared/diskImgs/*dmg | hdiutil attach
    ls /Users/Shared/diskImgs | grep .dmg | xargs hdiutil attach
    and countless combinations of grep, xargs, ls, hdiutil, <, >, |

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by rkasowan View Post
    #!/bin/bash
    for dmgfile in `ls /Users/Shared/diskImgs/*dmg`
    do
    hdiutil mount -noverify
    done
    you did not use the variable dmgfile. it should be something like this, like what you did successfully with one file:
    Code:
    for dmgfile in `ls /Users/Shared/diskImgs/*dmg` 
    do
       hdiutil mount -noverify $dmgfile
    done

  9. #9
    Just Joined!
    Join Date
    May 2007
    Posts
    15

    $dmgfile was the key!

    Thank you thank you thank you thank you thank you thank you thank you!

    I thought dmgfile was some sort of magic phrase, I didn't know it was a variable. That works perfectly! Now each .dmg file in the directory is auto mounted when that user logs in!

    Thanks you for the assistance, I've spent way too much time trying to figure this out. I would have never reached this conclusion on my own. Thanks again! -= robbie =-

  10. #10
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I should just warn you that if you have a dmg file that contains spaces in the filename, your script will no longer work. Anytime you use a variable in Bash scripting, you should surround it in quotes:
    Code:
    for dmgfile in `ls /Users/Shared/diskImgs/*dmg` 
    do
       hdiutil mount -noverify "$dmgfile"
    done
    Also, just for the record, this is a very simple one-line find command:
    Code:
    find . -maxdepth 0 -iname '*.dmg' -exec hdiutil mount -noverify "{}" \;
    If you learn the nuances of the find command, you will find it to be a very powerful command.
    DISTRO=Arch
    Registered Linux User #388732

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...