I am attempting to write my first shell script (I'm running Ubuntu 10.x)
This is what my 'script' looks like
cd /some/path/to/scripts
# pwd (if uncommented, this shows we HAVE really changed directory to /some/path/to/scripts
# echo `ls` (if uncommented, shows that testscript is in our working directory)
# the next line is where bash LIES: 'testscript: No such file or directory'
. testscript # (./testscript doesn't work either)
I put the following debug statements (after the cd), in the script above to make sure everything was ok:
pwd
echo `ls -lhrt`
and it displayed all the files in the directory. So I dont understand the error message I am getting.
[Edit]
I have changed my question - to focus on the MAIN issue as to why I am getting the 'No such file or directory' error message.
Here are the facts:
- testscript is executable and resides in /some/path/to/scripts
- when I manually type the commands in the script above, testscript runs successfully
My question then is this:
How is it that these same (trivial) commands work on the command line, and yet fail to work when executed from a batch script?.
Equally (if not more important), is the question - how do I fix this?
Put simply I want to write a script that does this:
- Change directory into a specified (hard coded) one
- Run a script in the current working directory
1 Answer 1
The shebang is pointing to the wrong location for the interpreter, it should be the absolute path, which (since you want to use bash for the script) is most likely:
#!/bin/bash
The purpose of the shebang is to explicitly state the interpreter that the script should be run with. If you do not specify an interpreter, it will be run on whatever interpreter you are running at the time it is run.
edit: I only just noticed you have another error listed in your title. This is probably because you are using . testscript, which most likely is supposed to be ./testscript (if you are trying to execute it). Make sure you have done chmod +x to make the script executable, or it will fail.
-
testscript already has the executable flag set (it runs if I cd to its folder and type ./testscript). This is exactly the same commands in my wrapper script - so I don't understand why it reports 'no such file or directory). using EITHER . testscript or ./testscript results in the same error mesage. The only thing I can thing of is that the cwd is immediately reset after thecd command - but then again, that can't be because if I enter pwd aafter the cd command, it shows I have successfully changed the working directory. SO whats going on?skyeagle– skyeagle2011年01月14日 12:13:42 +00:00Commented Jan 14, 2011 at 12:13
-
Try using an absolute path.Matthieu Cartier– Matthieu Cartier2011年01月16日 10:39:56 +00:00Commented Jan 16, 2011 at 10:39
bashactually in/usr/bin, or/bin?