Ubuntu Shellscript Path Variable - bash

I have the following Shellscript that I call from my crontab, which works fine until it calls php code that involves shell commands like wget or find.
#!/bin/sh
PATH=/opt/server/php/bin:/usr/bin/wget:/bin/egrep:/usr/bin/find
cd /opt/server/apache2/htdocs/webapp/
php oil refine job:handler
For each Command I did a which command to look up the path, then I added it to the Path Variable. Nevertheless it does not find the commands and I get messages like these:
sh: wget: not found
sh: find: not found
How would I fix this? I know that this is a common problem, but I didn't find a good explanation for this here on stackoverflow. Also: I know that calling the script from bash versus crontab might result in different enviroment settings, but eitherway I get these Errors.

Good sir, the PATH is a string that describes the directories that contain executables, not the executables themselves.
Perhaps use something like this
PATH=/opt/server/php/bin:/usr/bin:/bin

Related

Why do self made symlinks/aliases in /usr/local/bin not work as expected with 'which' command?

I've been creating bash scripts recently, and would like to store them in my /usr/local/bin directory. When I ls this directory, I see many, what I believe to be, symlink paths. Example:
brew -> /usr/local/HomeBrew/bin/brew
I've been able to successfully create symlinks to my own scripts and stored them in my /usr/local/bin with the ln command:
ln -s /User/me/Projects/bash/my_script /usr/local/bin/my_script
and everything runs as expected.
The problem I'm encountering, is when I to try run which my_script it's not returning a nice stdout result, like it is with the other scripts. For example running which brew returns: /usr/local/bin/brew in a nice stdout format to use with other commands.
Running which my_script, will successfully detect my script but, returns
my_script: aliased to ~/Projects/bash/my_script
This makes combining with other commands more difficult.
Can anyone explain what the difference is, and how I could possibly fix this?
You must have defined an alias named my_script, which is shadowing your symbolic link. You can do a
type -a my_script
to find all definitions. To bypass the alias, invoke it using
command my_script
Aside from this: Are you sure that you really want a link from /usr/local/bin into something which is below your /User/me? This does not look sane to me. Wouldn't it be better to simply put /User/me/Projects/bash into your PATH?

How does "which" work in osx?

I'm trying to install a linter for sublime tex in OSX. It cannot be found in sublime. According to the docs, this is likely because the PATH is wrong. It says I should try this:
hash -r
which linter
but replace linter with the "linter executable". I tried
which standard
which sublimeLinter-contrib-standard
which fooBarBaz
but neither of them returns anything. Do I need to execute this in a particluar directory or is something else wrong?
which uses the value of PATH that it inherits. The fact that which returns nothing confirms that you need to add the appropriate directory to your PATH.
which command looks through the directories defined in your shell’s PATH variable, as well as any aliases you have defined in your ~/.bash_profile file, to find the location of the command given as an argument. This is useful when you want to find out exactly which version of a command is being used. Here’s an example:
$ which ls
/bin/ls
This tells you that when you use the ls command, it is /bin/ls that is run. This command will also tell you if a specific command is a shell builtin.

cron: run a script that sources a function

I have script that does a bunch of stuff. It sources a bunch of functions that are in the directory the script is being run from. i.e.
/home/me/script.sh
/home/me/function1
/home/me/function2
If I cd into /home/me and run ./script.sh everything works fine. The functions are sourced and do what needs to be done.
However, if I try to run this as a cron job, it will run up until the point I am trying to source the functions, and then it just stops and the process is terminated (if I run it directly from the directory, at least I get some errors).
Like wise, if I try to run this from another directory, I get a bunch of errors. e.g.
cd /opt/
/home/me/script.sh
function1: command not found
function2: command not found
I'm sure this has something to do with environmental variables, but I have no idea which ones. I have tried setting (in crontab):
PATH=/home/me
SHELL=/bin/bash
But that doesn't work either. Any help is appreciated. I don't want to hard code in the paths to the functions, and instead make them relative to the path the script is in (preferably the same dir).
Please let me know if you need any more information.
You are most probably aware of this, but just to be clear: A shell function does not have a path. They just need to be loaded into the current shell by sourcing the script that contains them:
source /path/to/functions
or
cd /path/to/functions
source functions
If you are talking about shell programs (scripts) instead, then you need to account for the fact that on Unix-like OS, the current directory is never in the PATH by default:
/path/to/functions/function1
or
cd /path/to/functions
./function1
You tagged your question Bash, but note that to be POSIX-compatible (e.g. if using sh), you have to use the . keyword (instead of either . or source on Bash) and the same restrictions regarding the PATH as for command execution apply, see dot:
. ./function1

call blat from shell script, w8

Trying to execute blat http://www.blat.net/ from a shell script. I set the environmental PATH variable, and i can call blat from any location with the command prompt. Sending email from the command line works fine. But I'm not able to call it from within a shell script.
The (simplified) script
#!/bin/bash
blat
I get:
$ sh script.sh
script.sh: line 2: blat: command not found
I also tried by specifying the absolute path C:/Windows/System32/blat but that didn't work either.
There are many ways to solve this. If you will only run blat from Bash, you can
just put it in /usr/local/bin AKA C:\cygwin64\usr\local\bin.
If you need to run it from Bash and cmd/PowerShell, Windows "out of the box" has
pretty poor support for that. With Linux/Bash your PATH will usually look
something like this:
/usr/local/bin:/usr/bin
This is great because you have a system space, and a user space for programs.
However Windows looks like this:
C:\Windows\System32;C:\Windows
As you can see by default the PATH only has system space. This is bad because it
encourages people to do dumb things like put user programs alongside protected
operating system files. What Windows needs is the equivalent of
/usr/local/bin, a folder on the PATH, ahead of everything where people can
dump command line programs. Until that happens you just have to add your own:
Move blat.exe to C:\Users\<user>\Documents
Add that to PATH:
SETX PATH "%PATH%;C:\Users\<user>\Documents" /M
When you add it to Windows PATH, it automatically gets added to Bash PATH, so
the program should be available to all shells.

basic Unix command not found

I've installed mysql on my laptop.
Now when I launch a terminal window and a unix command like grep, I get this message:
-bash: grep: command not found
-bash: cat: command not found
...
What's happen ? Do you have skills or any ideas to retrieve my precious unix functions ?
I'd like to put this in a comment, but I don't have enough reputation, sorry!
Basic Unix commands like grep or cat are most likely in this folder:
/usr/bin
In order for them to work, you need to have the correct set up of your PATH variable. You might have changed it unknowingly during the installation of mysql.
You can fix this temporarily by trying something like:
export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
This will add whatever you have on your PATH at the moment, all the "normal" routes to your precious unix functions. Try the commands again after executing this line in your terminal, and let us know :)

Resources