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 ...
- 05-12-2007 #1Just 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 =-
- 05-12-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Try this:
RegardsCode:ls /directory | grep .dmg | xargs hdiutil attach
- 05-12-2007 #3Just 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...
- 05-12-2007 #4Just 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.
- 05-13-2007 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 05-13-2007 #6Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 05-13-2007 #7Just 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, <, >, |
- 05-13-2007 #8Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 05-14-2007 #9Just 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 =-
- 05-14-2007 #10
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:
Also, just for the record, this is a very simple one-line find command:Code:for dmgfile in `ls /Users/Shared/diskImgs/*dmg` do hdiutil mount -noverify "$dmgfile" done
If you learn the nuances of the find command, you will find it to be a very powerful command.Code:find . -maxdepth 0 -iname '*.dmg' -exec hdiutil mount -noverify "{}" \;DISTRO=Arch
Registered Linux User #388732


Reply With Quote
