# Use of the "which" command for a script



## beamar (Jan 2, 2013)

I am very rusty on my programming. I am new to freebsd FreeBSD. I am making a script. In shell I can use the which command to find the location of an install directory. I know in shell if I do a *which mail* it will return 
	
	



```
/usr/bin/mail
```

*I*n my script I would like to set it to a variable to the result of the which command. An example would be 
	
	



```
MAIL="$which mail"
```

I can't seem to find anything on the web about using the which command in a script, any help would be great.


Thanks,
Brian


----------



## wblock@ (Jan 2, 2013)

```
MAIL=`$which mail`

MAIL=$( $which mail )
```


----------



## beamar (Jan 2, 2013)

*B*oth of those don't seem to work, I get a 'not found'.


----------



## wblock@ (Jan 2, 2013)

Well, they assume that you have set the value of $which.

```
WHICH="/usr/bin/which"
MAIL=`$WHICH mail`
MAIL=$( $WHICH mail )
```


----------



## shitson (Jan 2, 2013)

wblock@ said:
			
		

> ```
> MAIL=`$which mail`
> 
> MAIL=$( $which mail )
> ```



Why $which? Shouldn't it just be


```
MAIL=`which mail`
```

-ss


----------



## wblock@ (Jan 2, 2013)

shitson said:
			
		

> Why $which?



Because that was in the original post.  I would do this:

```
WHICH="/usr/bin/which"
MAIL=$( ${WHICH} mail )
```


----------



## ondra_knezour (Jan 2, 2013)

Seems to me you are overlooking that
`$ which which`

```
which: shell built-in command.
```

--------------------------------------
Edit: On my shell, which is tcsh


----------



## SirDice (Jan 2, 2013)

Don't use (t)csh for scripting unless you are a masochist, use /bin/sh.


----------



## beamar (Feb 13, 2013)

So I had my script working at one point. It started throwing errors recently and I'm not 100% sure what the prob is. After some testing, It looks like either /bin/sh is messed up or I don't have access?

I made a test script:

```
#!/bin/sh

echo "testing!"
```
Which returns :

```
$ sh test.sh
: not found
testing!
```

I looked through the folders, /bin/sh is there with the following permissions. 

```
-r-xr-xr-x  1 root  wheel  142952 Jan 31 01:45 sh
```

Any ideas?


----------



## SirDice (Feb 13, 2013)

beamar said:
			
		

> ```
> $ sh test.sh
> : not found
> testing!
> ```


I'm guessing there's something between the lines *#!/bin/sh* and *echo "testing!"*.


----------



## beamar (Feb 13, 2013)

*N*ope.

My test file, test.sh reads:

```
#!/bin/sh

echo "testing!"
```

*T*hat is it.


Brian


----------



## gqgunhed (Feb 13, 2013)

Hi beamar,
from your original code:

```
MAIL="$which mail"
```
try one of these two possibilities:

```
MAIL="$(which mail)"
```
 - leading $ with parentheses
or

```
MAIL="`which mail`"
```
 - backticks

Both ( $() or `` ) return the result of the enclosed command, which in your case is "which mail"

Any leading $ will be a reference to the original variable, so "$which" will try to return the value of the unset variable "which", which collides with the shell command "which".


----------



## beamar (Feb 13, 2013)

gqgunhed,

I believe I got a lot of that squared away. I ended up just doing a `whereis mail` and entered the complete path name. 

I did have the script (it's just a basic backup script) working for a few days.

Prob*lem* is: the server I have am using a shared server so *it* seems like everything is on lock down. The server uses DirectAdmin which doesn't allow backup scheduling for a standard user.


Brian


----------



## gqgunhed (Feb 13, 2013)

beamar said:
			
		

> nope.
> 
> My test file, test.sh reads:
> 
> ...



Maybe saved with DOS-newlines (\r\n) instead of UNIX-newlines (just \n)?
You can check that e.g. with vim and [CMD=":"]set list[/CMD] to show the invisible characters (tabs, spaces, newlines).

If you see "^M" in vi, try to [CMD=":"]set ff=unix[/CMD] and save the file again. Just a guess.


----------



## beamar (Feb 13, 2013)

gqgunhed said:
			
		

> Maybe saved with DOS-newlines (\r\n) instead of UNIX-newlines (just \n)?
> You can check that e.g. with vim and [CMD=":"]set list[/CMD] to show the invisible characters (tabs, spaces, newlines).
> 
> If you see "^M" in vi, try to [CMD=":"]set ff=unix[/CMD] and save the file again. Just a guess.




That was the prob*lem* 

*O*nto the next error!


*T*hanks,
Brian


----------



## beamar (Feb 13, 2013)

*A*lso want to point out this is the code I am using:

```
MAIL="$(which mail)"
if [ -z "$MAIL" ]; then
    echo "Error: MAIL not found"
    exit 1
fi
```

Made it easier to track down the problems. On my server it seems to be mysqldump which I had to us a locate mysqldumper and use a crazy path for my script to work.


----------



## beamar (Feb 18, 2013)

So to add more to this awesome thread, I have a new issue.

I have my script working great when I ssh in. My server uses DirectAdmin and on this server I don't have root or even admin access. It's a shared server so making changes is not possible.

So when I set up my script to run as a cron job, it gets an error:


```
Error: MYSQL not found
```

So with the research I have done, I believe the issue is a path issue. 



> Crontab jobs are submitted by the system and do not execute the normal system startup script (which sets PATH and similar things).



Do I just add this to my script?


```
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
```

Thanks,
Brian


----------



## beamar (Feb 18, 2013)

Also, as I see MODS posting in here, is it possible to go back and edit a post on this forum?


----------



## SirDice (Feb 18, 2013)

beamar said:
			
		

> Also, as I see MODS posting in here, is it possible to go back and edit a post on this forum?



Read your sign up email.

Thread 11799


----------



## gqgunhed (Feb 18, 2013)

@beamar: Maybe this helps:
crontab manpage, see here the section:


*But:* 
"MYSQL" does seem to be another error (as the binary is named mysql. 
Are you using a variable named "MYSQL" in your script? 

If so, assignment is:

```
MYSQL="some value"
```
but reference (aka read the value) is:

```
echo $MYSQL
```
note the "$" her.


----------



## beamar (Feb 19, 2013)

```
MYSQL="$(which mysql)"
if [ -z "$MYSQL" ]; then
    echo "Error: MYSQL not found"
    exit 1
fi
```


It's the which statement. I'm sure I can just set the path static and it will work fine. But I would like to keep it dynamic so I can move it across servers without much setup.


----------

