Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lynkynpark86

macrumors 6502
Original poster
Does anyone know how I would do this in a shell script?
If x is a file: somecommand
If x is a directory: someothercommand
 
[ is an alias for test, which is quite funny I think. 😀 Try: man [ it will bring up the manual for test. It gives a bit more syntactically pleasing look if used in a script.

Code:
if [ -f $1 ]
then
    echo file
else if [ -d $1 ]
    echo directory
fi
 
test and [ are /bin utilities, which would work in all shells
[[ ... ]] is a bash (and ksh) built in
 
test and [ are /bin utilities, which would work in all shells
[[ ... ]] is a bash (and ksh) built in

FWIW, test and [ are bash builtins. They also exist as /bin tools, but bash (like other shells) searches its builtins first, unless the command contains a slash.

Try this:
Code:
type [ test
type is another builtin, otherwise similar to which (which isn't a builtin in bash).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.