LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating a bash script which understands paths to files (https://www.linuxquestions.org/questions/linux-newbie-8/creating-a-bash-script-which-understands-paths-to-files-447101/)

en8scl 05-21-2006 03:21 PM

Creating a bash script which understands paths to files
 
Hi

I am new to linux and I am trying to create a script which will eventually be put in a cron job. However I'm not having much luck with any script at the mo (even this basic example as it can't find the files I'm referencing).

Even a simple cat command doesn't seem to work once in my script.

From the command line:

cat filename

work fines and displays the contents of the file to the screen

As soon as I create a test.sh script (which I have made executable) with the following lines it stops working:

#!/bin/bash
cat filename
echo hello world

and I get an output which says:

: No such file or directory
hello world

From this I assume it is running the script alright as it prints the 'hello world' part.

I have tried giving the full path to the file as well e.g.:

#!/bin/bash
cat /home/some/directory/filename
echo hello world

but it still can't find the file...

Where am I going wrong?

Please help me - its driving me nuts

acid_kewpie 05-21-2006 03:37 PM

What is the actual output from the script? if your path starts with a / then it is absolute so will always work if other factors like file ownerships and permissions are not conflicting.

en8scl 05-21-2006 03:47 PM

sh-3.00$ bash reset_demo_db.sh
: No such file or directoryweb/demo_reset/tsweb_zendemo.sql
Hello World
Wish this would work
sh-3.00$

is the exact copy from the console when the script has exactly this in it:

#!/bin/bash
cat /hsphere/local/home/tsweb/demo_reset/tsweb_zendemo.sql
echo Hello World
echo Wish this would work

en8scl 05-21-2006 03:48 PM

and I am logged in as the owner of both the script and sql file

ethics 05-21-2006 04:05 PM

It is all case sensitive so make sure that is ok, and that the path is correct

acid_kewpie 05-21-2006 04:06 PM

ok, so what does
Code:

ls -l /hsphere/local/home/tsweb/demo_reset/tsweb_zendemo.sql
say?

en8scl 05-21-2006 04:11 PM

[root@cp demo_reset]# ls -l /hsphere/local/home/tsweb/demo_reset/tsweb_zendemo.sql
-rw-r--r-- 1 tsweb tsweb 586027 May 17 22:49 /hsphere/local/home/tsweb/demo_reset/tsweb_zendemo.sql

bc8o8 05-21-2006 04:28 PM

This error:
Quote:

Originally Posted by en8scl
: No such file or directoryweb/demo_reset/tsweb_zendemo.sql

doesn't seem to match this line
Quote:

Originally Posted by en8scl
cat /hsphere/local/home/tsweb/demo_reset/tsweb_zendemo.sql

If that error was for that line (as displayed) it would read:
cat: /hsphere/local/home/tsweb/demo_reset/tsweb_zendemo.sql: No such file or directory

I'm wondering if there are some unprintable garbage characters that are causing weird problems. I would try deleting the whole line and retyping it. I know it sounds stupid -- but I've had similar issues that where solved by retyping.

en8scl 05-21-2006 04:42 PM

Thankyou so much bc8o8

You were right - started a completely new file and it worked fine - weird. How do I avoid that in the future?

bc8o8 05-21-2006 07:22 PM

What text editor were you using? I don't think I've seen the problem in Vim or Emacs, but I could be wrong. Typically it happens in lower quality editors when keystrokes are hit that don't have a printable ASCII equivalent. It's sort of like when you're in a terminal and you start seeing characters like ^[A -- except the editor doesn't know how to handle them. The best way to avoid them is to not hit those keystrokes :) a more realistic solution is to use a better editor just in case you do accidentally type them.

Glad I could help!


All times are GMT -5. The time now is 10:53 PM.