1832978 Members
2756 Online
110048 Solutions
New Discussion

Re: whence

 
SOLVED
Go to solution
panchpan
Regular Advisor

whence

Hello.

Could you please let me know what will below command do:

if [ -f ${2} -o "$(whence $2)" != "" ];

Thank you!
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: whence

Hi:

The statement tests whether or not the second argument ($2) passed to the script or to a script function, represents a file ('-f'), *or* if the name of the argument represents a command.

# whence date
/usr/bin/date

# [ -f date ] && echo ok || echo not_ok
not_ok

# [ -f /usr/bin/date ] && echo ok
ok

The above shows that an argument of 'date' does not represent a file, but 'whence date' returns '/usr/bin/date' and thus the expression (statement) would evaluate to true.

Regards!

...JRF...
Oviwan
Honored Contributor

Re: whence

Hy

It checks whether the file in $2 exsits or not

Regards
Peter Godron
Honored Contributor

Re: whence

Hi,
if second parameter is a local file or a command then the condition would return true.

Example:
#!/usr/bin/sh
# My name is a.sh
if [ -f ${1} -o "$(whence $1)" != "" ]
then
echo "valid file or command"
fi

First test:
$ ./a.sh date
valid file or command

Second test:
$ ./a.sh fred
$

Third test:
$ touch fred
$ ./a.sh fred
valid file or command


panchpan
Regular Advisor

Re: whence

THANK YOU