jaymc Posted January 20, 2008 Share Posted January 20, 2008 Here is a part of my script ##################### # BACKUP BOTH DATABASES # ##################### echo "----------------- Stopping MYSQL and copying the raw database folders..." #service mysqld stop cd /var/lib/mysql cp -ap db1 /home/backups/mysql/db1_temp cp -ap db2 /home/backups/mysql/db2_temp #service mysqld start I am getting this error when I run test.sh ./test.sh : command not found ----------------- Stopping MYSQL and copying the raw database folders... : No such file or directory/lib/mysql However, if I do cd /var/lib/mysql within a terminal, no issues as you would expect Am I missing something? Quote Link to comment Share on other sites More sharing options...
trq Posted January 20, 2008 Share Posted January 20, 2008 Am I missing something? Yes, the shebang line. eg; #!/bin/sh Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 20, 2008 Author Share Posted January 20, 2008 Yeh, I tried that but I get this error when including that at the top : bad interpreter: No such file or directory It is in /bin by the way Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 20, 2008 Author Share Posted January 20, 2008 Have also tried #!/bin/bash Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 21, 2008 Author Share Posted January 21, 2008 Any ideas? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 21, 2008 Share Posted January 21, 2008 is a path a link to some other place ? Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 21, 2008 Author Share Posted January 21, 2008 Huh? no.. Basically, if I do cd /var/lib/mysql in a command shell its fine If I put that in a shell script and run it as an ./sh it cant find it? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 21, 2008 Share Posted January 21, 2008 your error say no such file /lib/mysql is non existence very strange Quote Link to comment Share on other sites More sharing options...
trq Posted January 21, 2008 Share Posted January 21, 2008 the command not found part leads me to believe it can't find a shell to execute with, yet its doing the echo, so it hard to tell. What about changing your script too.... #!/bin/bash ##################### # BACKUP BOTH DATABASES # ##################### echo "----------------- Stopping MYSQL and copying the raw database folders..." #service mysqld stop cp -apr /var/lib/mysql/db1 /home/backups/mysql/db1_temp cp -apr /var/lib/mysql/db2 /home/backups/mysql/db2_temp #service mysqld start This script may also need to be run as root as a normal user shouldn't have access to /var/lib/mysql. Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 21, 2008 Author Share Posted January 21, 2008 Its stranger that its saying /lib/mysql doesnt exist when the command is actually cd /var/lib/mysql Is #!/bin/bash important? I ran another script without that and it worked fine, so cant really see the relivence I thought # was a comment anyway P.S - I am running it as root Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 21, 2008 Author Share Posted January 21, 2008 Ok, this works cp -ap /var/lib/mysql/db1 /home/backups/mysql/db1_temp However, I really want/need to use the CD command for the rest of this script and for future.. Any more ideas.. this is rather abnormal? Quote Link to comment Share on other sites More sharing options...
trq Posted January 21, 2008 Share Posted January 21, 2008 Is #!/bin/bash important? I ran another script without that and it worked fine, so cant really see the relivence Yes it is relevent. it tells your shell which interpritor to use to execute the script. Without it, I think bash may try to execute it, which would be why its working. Shell scripts should always have a shebang line on the very first line. I'm not sure why the cd wouldn't be working especially if your running the script as root. I usually avoid changing directories within script and use full paths instead. It can make your code easier to read. Any reason why you really need to cd? Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 21, 2008 Author Share Posted January 21, 2008 Erm, I think when I did tar -cf file.tar /home/blah/blah/file.txt when I open the tar in winrar or something It includes all the folders, even though they are empty So in the rar is literally /home/blah/blah/file.txt rather than just file.txt If CD isnt working, surely other commands will fail further along the lines without the correct shebang, if that is the problem in the first place..? Quote Link to comment Share on other sites More sharing options...
trq Posted January 21, 2008 Share Posted January 21, 2008 Erm, I think when I did tar -cf file.tar /home/blah/blah/file.txt when I open the tar in winrar or something It includes all the folders, even though they are empty So in the rar is literally /home/blah/blah/file.txt rather than just file.txt If CD isnt working, surely other commands will fail further along the lines without the correct shebang, if that is the problem in the first place..? Ah yeah, that makes sense about the tar thing. Sorry though, but Ive no idea why cd wouldn't be working. Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 21, 2008 Author Share Posted January 21, 2008 What about this #!/bin/bash Sometimes I see people using #!/bin/bsh #!/bin/sh I have locations to all mentioned above btw, but just wondering if I should be using one rather than the other? Quote Link to comment Share on other sites More sharing options...
effigy Posted January 21, 2008 Share Posted January 21, 2008 Perhaps this script is being saved in a Windows format rather than a Unix one? i.e., line endings of "\r\n" instead of "\n". Quote Link to comment Share on other sites More sharing options...
trq Posted January 21, 2008 Share Posted January 21, 2008 It depends what program you want to execute the script. The most common (and default on most systems) is /bin/bash. sh is an older shell, while bash (bourne again shell) is the newer. Some bash specific code won't run when executed via sh. Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 21, 2008 Author Share Posted January 21, 2008 Perhaps this script is being saved in a Windows format rather than a Unix one? i.e., line endings of "\r\n" instead of "\n". I think we have a winnner Looks like your right, although hav only tested a partial amount of the script Cheers man Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.