Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In bash, is there a way to know if a given script has been invoked with:

$ myscript.sh myfile

or:

$ myscript.sh < myfile

Inside some scripts I always have accessed the contents of myfile with $1, but now I'd like to change the behavior as different cases.

EDIT: I also would like a third case when invoked without any redirection:

$ myscript.sh
share|improve this question

2 Answers 2

up vote 12 down vote accepted

EDIT: changed -t to -t 0, which does correctly detect input from terminal or file.

I think the key here is knowing whether your input is coming from a terminal or from a file. There is a test for this (man test, see -t).

Assuming you're running a bash script:

if [ -t 0 ]; then
    echo "Input from terminal"
    if [ $# -eq 0 ]; then
        echo "No input files specified on command line.  Error." >&2
    else
        echo "Input file given on command line.  It is $1"
    fi
else
    echo "Input coming from stdin"
fi

You can handle the different scenarios by substituting actual code in for the echo statements above.

Update, quickie test script:

#!/bin/bash
[ -t 0 ] && echo "t is true" || echo "t is false"

Running:

$ test.sh testfile
t is true
$ test.sh < testfile
t is false
$
share|improve this answer
    
-t always evaluates to true running the script from the command line with < myfile :( –  mschonaker Jun 27 '11 at 7:28
    
@mschonaker - Using -t 0 gives the correct results. Updated answer. –  Mark Mann Jun 27 '11 at 7:39
    
Thanks! That worked for me! –  mschonaker Jun 27 '11 at 7:48

In general, the expressions $1, $2, etc. expand to the 1st, 2nd, etc. argument given on the script command line.

So, when you invoke a script as:

myscript.sh myfile

then $1 within the script expands to myfile (and $2, $3, etc. all are the empty string).

When you invoke a script as:

myscript < myfile

the redirection of STDIN from myfile is done by the parent shell, so the script is actually called with no arguments and $1 expands to the empty string.

share|improve this answer
    
How do I detect if called with no arguments in the command line (the third case)? Sorry if you answered before my edit. Thanks. –  mschonaker Jun 27 '11 at 7:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.