[SOLVED] Bash script problem "No such file or directory"
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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.
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
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.
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 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?
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.
/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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.