LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-24-2004, 06:33 PM   #1
kidestranged
LQ Newbie
 
Registered: Jan 2004
Posts: 24

Rep: Reputation: 15
Shell Scripting


I'm writing a shell script that copies files and backs up a database. I use a PHP page on the browser to input the variables.

My question is -

How do I handle the output so that I can display it in the browser? Here is a simplified version of the script.

#!/bin/sh
cp /path/to/some_file.txt /some/other/location/new_name.txt


I want to the browser to output the results of the copy. Do I write to a text file and then have PHP read it? How should this be done?
 
Old 04-24-2004, 06:55 PM   #2
mangeli
LQ Newbie
 
Registered: Oct 2003
Posts: 18

Rep: Reputation: 0
Re: Shell Scripting

Quote:
Originally posted by kidestranged
I'm writing a shell script that copies files and backs up a database. I use a PHP page on the browser to input the variables.

My question is -

How do I handle the output so that I can display it in the browser? Here is a simplified version of the script.

#!/bin/sh
cp /path/to/some_file.txt /some/other/location/new_name.txt


I want to the browser to output the results of the copy. Do I write to a text file and then have PHP read it? How should this be done?
I believe there is an execute command in php that when passed through a php page it will return the results in the browser window.

I would check on php.net.....
 
Old 04-24-2004, 08:35 PM   #3
kidestranged
LQ Newbie
 
Registered: Jan 2004
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks - I have the PHP part sorted but I'm new to shell scripting and I don't know how to test if a linux command was successful.

for the following commands:

cp /path/to/some_file.txt /some/other/location/new_name.txt
&
mysql -uuname -ppass db_name<'dump_file.sql

How can I test whether or not the copy and dump worked?
if (successful copy)
do this


if (successful database action)
do this


For those commands, linux will only output anything if they failed so how do I check for an output in a shell script?


Thanks
 
Old 04-25-2004, 05:39 PM   #4
kidestranged
LQ Newbie
 
Registered: Jan 2004
Posts: 24

Original Poster
Rep: Reputation: 15
I'm going to bump this one time because I'm struggling to find my answers on Goggle.

I'm new to shell scripting but I've I do a lot of coding in PHP and C#. With these languages, there is always an exit status on commands and functions.

if (database function) {
do something
} else {
handle the error
}

The question is, how do I handle this with a shell script? If I copy a file, how do I test the success of the copy? If I run a database command from the shell, how do I know if any errors were returned?

Thanks for the help so far.
 
Old 07-27-2004, 09:28 PM   #5
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Code:
#!/bin/sh
if cp /path/to/some_file.txt /some/other/location/new_name.txt && mysql -uuname -ppass db_name<'dump_file.sql; then 
  echo success 
else 
  echo failure 
fi
 
Old 07-27-2004, 11:46 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
You can access a command's return status with the $? variable. An exit status of 0 traditionally indicates success. Non-zero indicates failure, and sometimes indicates teh nature of the failure. ToniT's example uses this information implicitly, but an explicit example:

Code:
#!/bin/bash

#Assuming that your PHP gives this script the source and destination
# arguments in the first and second arguments respectively
# The double quotes are used to preserve file/path names that
# include spaces or other character that the shell might otherwise
# interpret incorrectly
cp "${1}" "${2}"
cp_exit_status=$?

if [ ${cp_exit_status} -eq 0 ] ; then
  echo Copy successful
  exit 0
else
  echo Copy failed
  exit 1
fi
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Shell Scripting again yaadhav Linux - Newbie 8 08-04-2005 10:21 AM
shell scripting pcdebb Linux - Newbie 17 11-20-2003 03:49 PM
shell scripting markus1982 Linux - General 2 05-20-2003 07:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:15 PM.

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