0

I have a script written the unix shell language (NOT in bash or any other shell, in sh) that prints the mount point of a given usb (i.e, it takes the path of a usb (such as /dev/sdb1) as an argument). Here it is:

#!/bin/sh
# usage: get_mount [path]
# returns: mount pount of given usb path

pth=$1
echo $pth
mountPoint="`df -h | grep $pth | tr -s \" \"| cut \"-d \" -f6`"
echo $mountPoint

The problem is, when I run this, it just prints a blank string, and I know the command works because I've tried it in a terminal with no problem: it's just assigning it to a variable is what's not working. Anyone have any ideas? Thx in advance!

2
  • This script worked with the only device listed on my system's df -h (/dev/fd). Can you please post the relevant part of output from your df -h? Commented Dec 18, 2010 at 23:09
  • Can you give us a sample input and desired output? I'm fairly certain sh will have a certain number of Parameter Expansion techniques to totally remove the need for tr and/or cut Commented Dec 18, 2010 at 23:09

2 Answers 2

2

You'll simplify your life by making effective use of single quotes as well as double quotes:

mountPoint="`df -h | grep $pth | tr -s ' ' | cut -d' ' -f6`"

The first step in debugging this is to remove the cut command and see what it produces:

mountPoint="`df -h | grep $pth | tr -s ' '`"
echo $mountPoint

Does it still print 6 (or more) columns?

Note that if you misspell the argument to the command, then the grep will pass nothing through to the cut.

On my machine (a Mac), I get the output from df -h:

Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   465Gi  189Gi  277Gi    41%    /
devfs          111Ki  111Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
map auto_home    0Bi    0Bi    0Bi   100%    /home
/dev/disk1s1   1.8Gi  8.8Mi  1.8Gi     1%    /Volumes/BLACKBERRY

Note that some of the file system names have spaces in them. It is unlikely to be a factor in your problem, but it could throw things off (the mount point is then field 7).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I remember trying to use single quotes as well and it not working, but for some reason now it does... thanks anyway!
@DVK: two lines of output per FS would mess things up too - but the OP says that it works from the command line...
0

Move the \" before the -d in cut to after the -d.

mountPoint="`df -h | grep $pth | tr -s \" \"| cut -d \" \" -f6`"

3 Comments

That isn't it; that quote helps specify that the field delimiter is a blank.
Yep...but the argument to the -d can be attached to it, and the original notation in the question achieves that by using "-d " to pass the blank as the argument to -d. I think in terms of "-d' '", but the original was OK, albeit not in a form I'd use in my own scripts.
Except it didn't work in the orignal way it was posted, and I made that change and it worked. Perhaps it needed to be double escaped or something, but once I got it working as posted above, I stopped testing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.