LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script problem "No such file or directory" (http://www.linuxquestions.org/questions/programming-9/bash-script-problem-no-such-file-or-directory-871964/)

cnmoore 03-30-2011 01:18 PM

Bash script problem "No such file or directory"
 
This part of my CentOS bash script fails:
Code:

echo "Proceed? y or n"
read -n 1 reply
if (test "$reply" == "y");  then
        echo -e "\nRestoring."
        STR="/bin/gunzip <  /home/mike/backups/$1 |  /usr/bin/mysql -uMyUser -ppppppp $2"
        echo -e "$STR"
        "$STR"
else
        exit
fi

Output:

Code:

Proceed? y or n
y
Restoring.
/bin/gunzip <  /home/mike/backups/mike_forum_110321-0100.sql.gz |  /usr/bin/mysql -uMyUser -ppppppp test
restore_db.sh: line 29: /bin/gunzip <  /home/mike/backups/mike_forum_110321-0100.sql.gz |  /usr/bin/mysql -uMyUser -ppppppp test: No such file or directory

(Above edited for security.)

The "No such file or directory" is baffling. I have tried everything I could think of, such as putting in full paths and putting quotes around $STR.

All the files exist, and if the $STR text is run from the command line it runs fine. What could the trouble with the script be?

szboardstretcher 03-30-2011 01:39 PM

What input are you giving this script? Parameters?

Well, anyway, my guess is going to be:

Code:

echo "Proceed? y or n"
read -n 1 reply
if (test "$reply" == "y");  then
        echo -e "\nRestoring."
        /bin/zcat $1 | /usr/bin/mysql -u root $2
else
        exit
fi


cnmoore 03-30-2011 01:47 PM

Here's the whole script, again edited for security.

Code:

#!/bin/bash
# Restore of xxxx_yyyyy or test  database
cd /home/mike/backups

if [ -z "$1" ]; then
  echo usage: $0 [name of file.sql.gz to restore from] [name of database]
  ls -la
  exit
fi
echo -e "
The file is"
if !(file $1); then
        echo "File does not exist"
        exit
fi
echo  -e "
echo -e "\nRestoring to database: $2"
The database will be restored to an earlier date
and anything added since then will be lost.
"

echo "Proceed? y or n"
read -n 1 reply
if (test "$reply" == "y");  then
        echo -e "\nRestoring."
        STR="/bin/gunzip <  /home/mike/backups/$1 |  /usr/bin/mysql -uMyUser -ppppppp $2"
        echo -e "$STR"
        "$STR"
else
        exit
fi

exit
if


szboardstretcher 03-30-2011 02:03 PM

The above will work.

Are you trying to accomplish something else that my fix doesn't take into account?

cnmoore 03-30-2011 02:20 PM

I don't understand your fix. Root is not the owner of the database in question.

I am simply trying to do this within a shell script:

Code:

mysql -uMyUser -ppppppp databasename < dumpfile.sql
This is the way to restore a database from a mysqldump file. See for instance
http://php.about.com/od/learnmysql/s...l_backup_2.htm or
http://www.techiecorner.com/31/how-t...sql-dump-file/

It works fine from SSH command line but I know from other scripting attempts that I can't get mysql to run from within a script. I do not understand why this is.

szboardstretcher 03-30-2011 02:24 PM

Quote:

Originally Posted by cnmoore (Post 4308800)
I don't understand your fix. Root is not the owner of the database in question.

Code:

        /bin/zcat $1 | /usr/bin/mysql -u MyUser -ppppppp $2
Replace root with your user. Replace ppp with your password.

Here is the code i used locally on my server:

Quote:

echo "Proceed? y or n"
read -n 1 reply
if (test "$reply" == "y"); then
echo -e "\nRestoring."
/bin/zcat $1 | /usr/bin/mysql -u root $2
else
exit
fi
Which works when I run:

Quote:

restore.sh some_database.sql.gz test_database
Feel free to modify the script to fit your situation.

cnmoore 03-30-2011 02:33 PM

Same old same old.

Restoring.
/bin/zcat mike_forum_110321-0100.sql.gz | /usr/bin/mysql -uMyUser -ppppppp test
test.sh: line 30: /bin/zcat mike_forum_110321-0100.sql.gz | /usr/bin/mysql -uMyUser -ppppppp test: No such file or directory

(user and password edited here for security)

I better go ask the MySQL people I suppose. Asked here bcause I've gotten so much great help here in the past. But this seems to be a stumper.

szboardstretcher 03-30-2011 02:46 PM

Quote:

Originally Posted by cnmoore (Post 4308811)
Same old same old.

Restoring.
/bin/zcat mike_forum_110321-0100.sql.gz | /usr/bin/mysql -uMyUser -ppppppp test
test.sh: line 30: /bin/zcat mike_forum_110321-0100.sql.gz | /usr/bin/mysql -uMyUser -ppppppp test: No such file or directory

(user and password edited here for security)

I better go ask the MySQL people I suppose. Asked here bcause I've gotten so much great help here in the past. But this seems to be a stumper.

There is a space between -u and MyUser

And,.. have you checked to be sure that this file,.. is in the directory you are running this from?


Output from this script working.

Code:

[root@sandbox-charlie ~]# cat go.sh
echo "Proceed? y or n"
read -n 1 reply
if (test "$reply" == "y");  then
        echo -e "\nRestoring."
        /bin/zcat $1 | /usr/bin/mysql -u root $2
else
        exit
fi
[root@sandbox-charlie ~]# ./go.sh crowd_db.sql.gz test
Proceed? y or n
y
Restoring.
[root@sandbox-charlie ~]# mysqldump test > test.sql
[root@sandbox-charlie ~]# ls -alh test.sql
-rw-r--r-- 1 root root 2.5M Mar 30 15:53 test.sql
[root@sandbox-charlie ~]#


cnmoore 03-30-2011 02:56 PM

I've tried that, also tried putting space after -p, but makes no difference. I think you are guessing? Sort of reassuring that it baffles someone else.

I posted at http://forums.mysql.com (has to wait for moderation in order to show up there)
Quote:

I get error with this script and every other one where I tried to call mysql. The line works fine when called from SSH command line.

/bin/zcat mike_forum_110321-0100.sql.gz | /usr/bin/mysql -uMyUser -pxxxxxxxx test
test.sh: line 30: /bin/zcat mike_forum_110321-0100.sql.gz | /usr/bin/mysql --uMyUser -pxxxxxxxx test: No such file or directory

User and password edited here for security.
All the files exist.
Is there some special way needed to run mysql from a script?

szboardstretcher 03-30-2011 03:01 PM

Wait.

Are you changing your script at all?

This should be the whole new script. All by itself. Once you see that it works, you can incorporate it into your old script.

Code:

echo "Proceed? y or n"
read -n 1 reply
if (test "$reply" == "y");  then
        echo -e "\nRestoring."
        /bin/zcat $1 | /usr/bin/mysql -u MyUser -pppppp $2
else
        exit
fi

It works. I've just tried it on Centos, Slackware, and Ubuntu.

cnmoore 03-30-2011 03:19 PM

My script has the full path for the .sql.gz file.
STR="/bin/zcat /home/mike/backups/$1 | /usr/bin/mysql -u MyUser -p pppppp $2"
Here's the output of your cmds:

[mike@www bin]$ which mysql;which zcat;pwd;ls -alh test.sh;ls -alh /home/mike/backups/*.sql.gz
/usr/bin/mysql
/bin/zcat
/home/mike/bin
-rwxrwxrwx 1 mike mike 726 Mar 30 14:53 test.sh
-rw-r--r-- 1 mike mike 1.1M Sep 13 2010 /home/mike/backups/ibf_skins_09-13-10.sql.gz
-rw-r--r-- 1 mike mike 664M Mar 21 01:03 /home/mike/backups/mike_forum_110321-0100.sql.gz
-rw-r--r-- 1 mike mike 667M Mar 22 01:03 /home/mike/backups/mike_forum_110322-0100.sql.gz
-rw-r--r-- 1 mike mike 668M Mar 23 01:03 /home/mike/backups/mike_forum_110323-0100.sql.gz
-rw-r--r-- 1 mike mike 668M Mar 24 01:03 /home/mike/backups/mike_forum_110324-0100.sql.gz
-rw-r--r-- 1 mike mike 668M Mar 25 01:03 /home/mike/backups/mike_forum_110325-0100.sql.gz
-rw-r--r-- 1 mike mike 668M Mar 26 01:03 /home/mike/backups/mike_forum_110326-0100.sql.gz
-rw-r--r-- 1 mike mike 668M Mar 27 01:02 /home/mike/backups/mike_forum_110327-0100.sql.gz
-rw-r--r-- 1 mike mike 668M Mar 28 01:03 /home/mike/backups/mike_forum_110328-0100.sql.gz
-rw-r--r-- 1 mike mike 666M Mar 29 01:03 /home/mike/backups/mike_forum_110329-0100.sql.gz
-rw-r--r-- 1 mike mike 664M Mar 30 01:03 /home/mike/backups/mike_forum_110330-0100.sql.gz
-rw-r--r-- 1 mike mike 1.1M Sep 8 2010 /home/mike/backups/skin_tables_09-07-10.sql.gz
[mike@www bin]$

I don't understand your script. You are making a dump. I already have many dumps. :)

Nominal Animal 03-30-2011 04:14 PM

Quote:

Originally Posted by cnmoore (Post 4308750)
This part of my CentOS bash script fails:
Code:

  STR="/bin/gunzip <  /home/mike/backups/$1 |  /usr/bin/mysql -uMyUser -ppppppp $2"
  "$STR"


You're trying to run the entire string as a command; the shell is trying to find a command of that name, spaces and all. Try
Code:

  STR="/bin/gunzip < '/home/mike/backups/$1' | /usr/bin/mysql -uMyUser -ppppppp '$2'"
  $STR

instead.

cnmoore 03-30-2011 04:28 PM

Well, that at least gives me a different error. :D
Get invalid option if $STR is invoked without quotes. Get "No such file.." if it has quotes.

Code:

echo "Proceed? y or n"
read -n 1 reply
if (test "$reply" == "y");  then
        echo -e "\nRestoring."
        STR="/bin/gunzip < '/home/mike/backups/$1' | /usr/bin/mysql -u whoever -p xxxxxxx '$2'"

        echo -e "$STR"
        $STR

Output:
Restoring.
/bin/gunzip < '/home/mike/backups/mike_forum_110321-0100.sql.gz' | /usr/bin/mysql -u whoever -p xxxxxx 'test'
/bin/gunzip: invalid option -- u
gunzip 1.3.5

Nominal Animal 03-30-2011 05:04 PM

Quote:

Originally Posted by cnmoore (Post 4308918)
Well, that at least gives me a different error. :D
Get invalid option if $STR is invoked without quotes.

Oops, my bad. You have a redirection and a pipe there,
Quote:

Originally Posted by cnmoore (Post 4308918)
Code:

STR="/bin/gunzip < '/home/mike/backups/$1' | /usr/bin/mysql -u whoever -p xxxxxxx '$2'"

so you actually need to do
Code:

eval "$STR"

cnmoore 03-30-2011 05:19 PM

If I use zcat,

Restoring.
/bin/zcat '/home/mike/backups/mike_forum_110321-0100.sql.gz' | /usr/bin/mysql -u whoever -p xxxxx 'test'
test.sh: line 29: /bin/zcat: No such file or directory
[mike@www bin]$

Where would that eval $STR go?

When I did this:
Code:

      STR="/bin/zcat '/home/mike/backups/$1' | /usr/bin/mysql -u mike_forum -p sKeYzY4g '$2'"
        echo -e "$STR"
        eval "$STR"

the output was

Code:

/bin/zcatp '/home/mike/backups/mike_forum_110321-0100.sql.gz' | /usr/bin/mysql -u whoever -p xxxxxx 'test'
/usr/bin/mysql  Ver 14.14 Distrib 5.1.54, for unknown-linux-gnu (x86_64) using readline 5.1
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software, ... etc..



All times are GMT -5. The time now is 03:04 PM.