LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2011, 01:18 PM   #1
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Rep: Reputation: 0
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?
 
Old 03-30-2011, 01:39 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
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

Last edited by szboardstretcher; 03-30-2011 at 02:01 PM.
 
Old 03-30-2011, 01:47 PM   #3
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
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
 
Old 03-30-2011, 02:03 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
The above will work.

Are you trying to accomplish something else that my fix doesn't take into account?
 
Old 03-30-2011, 02:20 PM   #5
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
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.

Last edited by cnmoore; 03-30-2011 at 02:24 PM.
 
Old 03-30-2011, 02:24 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
Quote:
Originally Posted by cnmoore View Post
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.

Last edited by szboardstretcher; 03-30-2011 at 02:27 PM.
 
Old 03-30-2011, 02:33 PM   #7
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
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.

Last edited by cnmoore; 03-30-2011 at 02:39 PM. Reason: Added last paragraph
 
Old 03-30-2011, 02:46 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
Quote:
Originally Posted by cnmoore View Post
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 ~]#

Last edited by szboardstretcher; 03-30-2011 at 02:57 PM.
 
Old 03-30-2011, 02:56 PM   #9
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
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?
 
Old 03-30-2011, 03:01 PM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693Reputation: 1693
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.

Last edited by szboardstretcher; 03-30-2011 at 03:05 PM.
 
Old 03-30-2011, 03:19 PM   #11
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
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.
 
Old 03-30-2011, 04:14 PM   #12
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by cnmoore View Post
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.
 
Old 03-30-2011, 04:28 PM   #13
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
Well, that at least gives me a different error.
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

Last edited by cnmoore; 03-30-2011 at 04:36 PM.
 
Old 03-30-2011, 05:04 PM   #14
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by cnmoore View Post
Well, that at least gives me a different error.
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 View Post
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"
 
1 members found this post helpful.
Old 03-30-2011, 05:19 PM   #15
cnmoore
Member
 
Registered: Sep 2010
Location: Sunnyvale, CA
Distribution: CentOS 5.5
Posts: 89

Original Poster
Rep: Reputation: 0
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..
 
  


Reply

Tags
mysql, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
[SOLVED] "!#/bin/bash: No such file or directory"? When running a script C_Blade Linux - Newbie 12 04-08-2010 10:15 PM
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 04:37 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration