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 |
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.
|
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 |
and I am logged in as the owner of both the script and sql file
|
It is all case sensitive so make sure that is ok, and that the path is correct
|
ok, so what does
Code:
ls -l /hsphere/local/home/tsweb/demo_reset/tsweb_zendemo.sql |
[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 |
This error:
Quote:
Quote:
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. |
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? |
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. |