Use of the "which" command for a script

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
Code:
/usr/bin/mail

In my script I would like to set it to a variable to the result of the which command. An example would be
Code:
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
 
Well, they assume that you have set the value of $which.
Code:
WHICH="/usr/bin/which"
MAIL=`$WHICH mail`
MAIL=$( $WHICH mail )
 
Seems to me you are overlooking that
$ which which
Code:
which: shell built-in command.
:)
--------------------------------------
Edit: On my shell, which is tcsh
 
Don't use (t)csh for scripting unless you are a masochist, use /bin/sh.
 
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:
Code:
#!/bin/sh

echo "testing!"
Which returns :
Code:
$ sh test.sh
: not found
testing!

I looked through the folders, /bin/sh is there with the following permissions.
Code:
-r-xr-xr-x  1 root  wheel  142952 Jan 31 01:45 sh

Any ideas?
 
Hi beamar,
from your original code:
Code:
MAIL="$which mail"
try one of these two possibilities:
Code:
MAIL="$(which mail)"
- leading $ with parentheses
or
Code:
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".
 
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.

Problem 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
 
beamar said:
nope.

My test file, test.sh reads:

Code:
#!/bin/sh
echo "testing!"

that is it.


Brian

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.
 
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 problem :)

Onto the next error!


Thanks,
Brian
 
Also want to point out this is the code I am using:
Code:
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.
 
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:

Code:
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?

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

Thanks,
Brian
 
@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:
Code:
MYSQL="some value"
but reference (aka read the value) is:
Code:
echo $MYSQL
note the "$" her.
 
Code:
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.
 
Back
Top