Jump to content

[SOLVED] Shell script - Weird


Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/86850-solved-shell-script-weird/
Share on other sites

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.

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

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?

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..?

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.