Simpler would be:
#!/bin/sh
# usage: get_mount path
# returns: mount pount of given usb path
if [ $# -eq 1 ]; then
df -h | awk /$1/'{print $6}'
else
echo "Usage: `basename $0` path"
fi
# One liner:
# [ $# -eq 1 ] && df -h | awk /$1/'{print $6}' || echo "Usage: `basename $0` path"
Couple of comments:
1. awk works in most cases and code looks cleaner. Output of df is tabular and awk is best suited for it.
2. You wrote [path] in usage comment. "[]" in unix syntax's syntax means "optional". Nothing or "{}" mean mandatory.
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
What if $1 is "Used" or "Available"?
I didn't get what you mean by Used or Available?It will also fail if there is whitespace in $1.
$1 should be used as an argument to df, not a pattern in awk.
Yes. Soln is quite clear as well I guess. "/$1/"
Also although what you say is correct (pass it to df not awk) the difference is if you wanna pass it to df you need to know exact string, to awk it'll be a regex.
E.g. if passed to df: xxx.sh sda3 won't work, but when passed to awk xxx.sh sda3 will still give you the mount point for say /dev/sda3.And I'd recommend using 'df -P', or it might split the output onto two lines (I forgot that, too):
There's no need for an external command, and error messages should be sent to stderr: echo "Usage: ${0##*/} path" >&2
Good point.I don't use awk when there will only be a few lines to be read. The shell is much faster.
As a general coding practice yes. But in this case speed won't matter.
Prime reason why awk is my first choice is because those who donno awk can still guess what's {print $1} but those who donno shell won't understand most of the code you posted.
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75