how to run non-standard commands in bash script?

Hello All. I suspect that this will be a clear noob question, but I haven't been able to figure it out using the usual methods, so I turn to you.

I've written a script to create input files for the quantum chemistry program NWCHEM. Generally you create an input file and then execute it by typing at the command line:
>> nwchem inputfile.nw
and hitting enter. I've written a script for a huge batch of jobs that will create the folders and sub-folders, write the correct input file to the sub-folders and then...

Well, then it has to execute the command "nwchem inputfile.nw", but "nwchem" isn't a standard command like "cd" or "mkdir" so it doesn't do anything. I've tried just echoing it to the line along with the inputfile.nw name, but then of course it doesn't actually RUN it. I'm guessing that this is a path issue, but I'm not sure who to a) find the folder that contains the nwchem command that I use when I just type it to the command line manually and b) how to specify that and run it from the .sh script.

Is there an easier way to do this? Something that just tells bash to treat a particular group of words as a command just like mkdir or cd?

Thanks for any help you can give me. The process of trying to get this done has almost taken longer than just doing it all by hand without a script.

cheers,
EMF

Since you can type and run the command nwchem in shell, the path of it has already in the PATH environment variable.
That is, it can also run in scripts without specifying the absolute path.
So I think your problem is another case.
If you wanna find out the path of the command, type "which nwchem" in shell.

Yes, the path comes back /opt/local/share/nwchem/nwchem. Does this tell anyone anything about why I can't just use it in the .sh script the same way I do other (more standard) commands?

Thanks again for any help.

---------- Post updated at 09:11 PM ---------- Previous update was at 09:01 PM ----------

If it helps, here is what I have in there now:

path=$PATH:/opt/local/share/nwchem/nwchem
nwchem $basis_name$func_name$spin_name".nw"

This gives the error:
./nw_chem_DIMERscript.sh: line 152: nwchem: command not found

When you say "it doesn't do anything" do you mean that you are getting an error message like "not found or no such file/directory," or literally nothing is happening?

There is a possibility that the command is being executed, but it is quietly aborting. Assuming that nwchem is either a bash or Kshell script, consider adding the command

set -x

at the start of nwchem -- if nwchem is being invoked, this might give you a clue as to why it is not executing as you intended.

I create all of the directories and then write the input file in that directory under the name $basis_name$func_name$spin_name".nw", then try to run the program by outputting
>> nwchem $basis_name$func_name$spin_name.nw
in the same manner that I would in the shell by hand but it tells me that this is not a command.

I included the set -x as you said in the following way:
path=$PATH:/opt/local/share/nwchem/nwchem
set -x nwchem $basis_name$func_name$spin_name".nw"
and it output a list of the failed if/then statements (but not the ones that passed), one (but not all) of the directory changes, the exit command etc. It didn't tell me "command not found" as before.

I meant that set -x should be added inside of nwchem near the top (after the #!line if you are using that, otherwise as the first command). Sorry for the confusion.

You probably didn't get an error message this time round because the nwchem command after the 'set -x' was absorbed by the set command and there was no attempt to execute it.

Since you've said that you've been getting a 'not found' message, it does appear that it is a PATH issue, however from what you've posted it looks like you have your PATH setup to include the directory where nwchem exists.
Im not sure what is going on.

You can add path=$PATH:/opt/local/share/nwchem/nwchem in ~/.bash_profile
then you have two choice:
1 source ~/.bash_profile
so ,you can use your command now.

2 log out and then log in
then you can find it works

When I put set -x in there properly as you say, it gives the following for the lines where I specify the path and try to execute nwchem:
+ path=.:/opt/local/bin/x86_64:/opt/gridengine/bin/lx26-amd64:/opt/intel/impi/3.1/bin64:/usr/kerberos/bin:/usr/java/jdk1.5.0_10/bin:/opt/intel/itac/7.1/bin:/opt/intel/fce/10.1.011/bin:/opt/intel/idbe/10.1.011/bin:/opt/intel/cce/10.1.011/bin:/opt/globus/bin:/opt/globus/sbin:/opt/intel/clck/1.1:/usr/local/bin:/bin:/usr/bin:/opt/Bio/ncbi/bin:/opt/Bio/mpiblast/bin/:/opt/Bio/EMBOSS/bin:/opt/Bio/clustalw/bin:/opt/Bio/t_coffee/bin:/opt/Bio/phylip/exe:/opt/Bio/mrbayes:/opt/Bio/fasta:/opt/Bio/glimmer/bin:/opt/Bio/glimmer/scripts:/opt/Bio/gromacs/bin/:/opt/eclipse:/opt/ganglia/bin:/opt/ganglia/sbin:/opt/maven/bin:/opt/openmpi/bin/:/opt/rocks/bin:/opt/rocks/sbin:/usr/X11R6/bin:/usr/local/dislin/bin:/opt/local/share/nwchem/nwchem
+ nwchem SDDbeckehandh3_spin.nw
./nw_chem_DIMERscript.sh: line 152: nwchem: command not found

Also, when I submit the script by hand, the following message is given:
nwchem SDDBPW911_spin.nw
# of processors not specified. Defaulting to 1
Submitting NWCHEM job SDDBPW911_spin ....

Your job 4675247 ("SDDBPW911_spin.19706.script") has been submitted
The job number is just the number that it has going into the queue, but the number between spin. and .script corresponds to something else. I'm not sure what. Does this fact that it's going to a queuing system matter here? It's still executed from the shell with just the simple nwchem command after all...

---------- Post updated at 08:57 AM ---------- Previous update was at 08:54 AM ----------

I added the path as you said. Here is what the ./bash_profile looks like now:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
path=$PATH:/opt/local/share/nwchem/nwchem
export PATH
unset USERNAME

Is this correct? I tried it as is and it didn't change anything.

Does anyone have any other ideas about where I might look for answers to this question? I had always assumed that the problem would end here and that someone on this board would know the answer...

Earlier you said you added this to your .sh script:

If "which nwchem" returns "/opt/local/share/nwchem/nwchem", this means nwchem is in "/opt/local/share/nwchem" -- so if you want to add this directory to the PATH, that section should read as follows:

PATH=$PATH:/opt/local/share/nwchem
nwchem $basis_name$func_name$spin_name".nw"

Have you tried simply putting the full path in your bash script rather than changing the $PATH variable?
What I mean is, inside your script that creates the folders and such write

... make folders ...
/opt/local/share/nwchem/nwchem
... other stuff ...

This works fine for me. Below is a script named script that makes a directory and a file, and then runs nwchem. nwchem for me simply prints out "This is nwchem". Note that nwchem is called by its full path (~ is just a shortform for /Users/****/).

  1 #!/bin/bash
  2 mkdir newdir
  3 touch newdir/file1
  4 ~/Documents/nwchem/nwchem

output as follows

dhcp23:nwchem ******n$ ./script
This is nwchem
dhcp23:nwchem *****$ ls -l
total 40
drwxr-xr-x  3 ****  staff   102  4 Aug 12:20 newdir
-rwxr-xr-x  1 *****  staff  9560  4 Aug 12:17 nwchem
-rw-r--r--  1 ***** staff    81  4 Aug 12:17 nwchem.cxx
-rwxrwxrwx  1 *****  staff    71  4 Aug 12:18 script

nwchem is run and the directory and file inside it is created.
You may also want to check that you have execute permissions for nwchem.
If not, change it with chmod, chmod 744 nwchem will give you read write and execute permissions and others only read.

1 Like

That worked! Thanks so much! You've made my job 1000% easier.

Cheers,
EMF

lol, the easy way is the best way.