0

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:

  1. Change directory into a specified (hard coded) one
  2. Run a script in the current working directory
asked Jan 14, 2011 at 9:22
3
  • Is bash actually in /usr/bin, or /bin? Commented Jan 14, 2011 at 9:29
  • Mine is in /bin (Debian) ;) Commented Jan 14, 2011 at 9:30
  • It appears that you've found your own answer to your revised question (in a comment at the linked question on SO). You should link your accounts on the two sites. Also, it's preferred that you not cross-post. Heavy revision of a question makes earlier answers not make sense. Commented Jan 14, 2011 at 21:40

1 Answer 1

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.

answered Jan 14, 2011 at 9:33
2
  • 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? Commented Jan 14, 2011 at 12:13
  • Try using an absolute path. Commented Jan 16, 2011 at 10:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.