LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script problem "No such file or directory" (https://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..


Nominal Animal 03-30-2011 06:28 PM

(Check the commands in your previous post, please. I think you'll want to edit them.)
Quote:

Originally Posted by cnmoore (Post 4308960)
Code:

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


Yes, that is what I meant. The issue is that now the mysql command part,
Quote:

Originally Posted by cnmoore (Post 4308960)
[CODE]
/usr/bin/mysql -u username -p password 'table'

fails. Have you made sure that the command
Code:

/bin/zcat '/home/mike/backups/mike_forum_110321-0100.sql.gz' | /usr/bin/mysql -u whoever -p xxxxxx 'test'
actually works?

cnmoore 03-30-2011 07:16 PM

Good question!

This works on command line:
Code:

/bin/gunzip <  /home/mike/backups/mike_forum_110321-0100.sql.gz |  /usr/bin/mysql -uGeorge -pxxxxx test
None of the variations introduced above work. Glad you asked!
Single quotes were in response to http://www.linuxquestions.org/questi...4/#post4308900

cnmoore 03-30-2011 08:08 PM

My son pointed out that even though echo -e "$STR" looks fine, $STR does not necessarily look good to the command processor. He suggested this:
Code:

if (test "$reply" == "y");  then
        echo -e "\nRestoring."
        STR="/bin/gunzip <  /home/mike/backups/$1 |  /usr/bin/mysql -umike_forum -psKeYzY4g $2"
        echo -e "$STR" > temp.sh
        chmod 777 temp.sh
        bash temp.sh
else
        exit
fi

and it works! Hooray!

Thanks to all of you for taking an interest. :newbie:

cnmoore 03-31-2011 01:27 PM

Surprising fact
 
I got to thinking more about this. My original script didn't work, but this does work:
Code:

STR="/bin/gunzip <  /home/mike/backups/$1 |  /usr/bin/mysql -uUser -pxxxx
eval $STR

Output with -x:
+ STR='/bin/gunzip < /home/mike/backups/mike_forum_110322-0100.sql.gz | /usr/bin/mysql uUser -pxxxx test'
+ eval /bin/gunzip '<' /home/mike/backups/mike_forum_110322-0100.sql.gz '|' /usr/bin/mysql uUser -pxxxx test
++ /bin/gunzip
++ /usr/bin/mysql uUser -pxxxx test

The 'eval' suggestion from Nominal Animal was right on, but when I tried it out it got tangled with a 'zcat' (which didn't work).

Thanks and apologies, Normal Animal.

cnmoore 03-31-2011 03:26 PM

Quote:

Originally Posted by Nominal Animal (Post 4308949)
You have a redirection and a pipe there,
so you actually need to do
Code:

eval "$STR"

Being new to scripting I was curious why this is. So I did some googling and found this nice clear explanation that variable expansion occurs after output redirecton:
http://linuxdevcenter.com/pub/a/linux/lpt/08_10.html
Quote:

Do you see what's happening? We're constructing a command that will look something like:

grep $grepopts $searchstring $file | sort $sortopts > $ofile
But the entire command is "hidden" in shell variables, including the I/O redirectors and various options. If the eval isn't there, this command will blow up in all sorts of bizarre ways. You'll see messages like | not found, because variable expansion occurs after output redirection. The "nested" variables (like $ofile, which is used inside of $output) won't be expanded either, so you'll also see $ofile not found. Putting an eval in front of the command forces the shell to process the line again, guaranteeing that the variables will be expanded properly and that I/O redirection will take place.

szboardstretcher 03-31-2011 03:27 PM

You are also doing in 3 lines, what can be done in 1.

cnmoore 03-31-2011 03:29 PM

Quote:

Originally Posted by szboardstretcher (Post 4310012)
You are also doing in 3 lines, what can be done in 1.

Please explain and suggest?

szboardstretcher 03-31-2011 03:33 PM

As i said before, when I re-wrote the part that was breaking...

Code:

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

can be done with:

Code:

/bin/zcat $1 | /usr/bin/mysql -u MyUser -pppppp $2
But thats your call. I just prefer to have less script to look at. Less is more.

Good luck and Regards.

cnmoore 03-31-2011 03:55 PM

Quote:

Originally Posted by szboardstretcher (Post 4310017)
As i said before, when I re-wrote the part that was breaking...

Code:

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

can be done with:

Code:

/bin/zcat $1 | /usr/bin/mysql -u MyUser -pppppp $2
But thats your call. I just prefer to have less script to look at. Less is more.

Good luck and Regards.

You're absolutely right. That does work and is certainly more compact and also more transparent - which I certainly think is a good thing in a script. I didn't know about zcat before - pipe built in is cool.

I got incompatible suggestion mixed up together and didn't absorb your very helpful http://www.linuxquestions.org/questi...4/#post4308837

Thanks!


All times are GMT -5. The time now is 09:11 AM.