Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Linux] Shell scripting

Evil Scottish Overlord
Legend
Joined
May 18, 2007
Messages
5,844
Reaction score
5,250
Hey guys, I'm doing a bit of Linux shell scripting but shell scripting isn't a strong point of mine. What I'm doing is I'm running an Apache server on CentOS 6.5 and I'm using a non-default directory for storing my files. I want to have a script that when executed with a file as a variable, the file is copied to this directory and the httpd service is started on the condition that the file is found. I then want the Apache server to retrieve this file from the non-default directory and for a message to be echoed back onto the terminal that the file was either successfully or unsuccessfully retrieved.

As you can see I've made a start but I haven't got the copy command or the start httpd service command embedded in the condition. What's the most straightforward way to do this?

Code:
# This script will check to see if a file exists and if so,# copy the file in to the [DIRECTORY] directory. From here it
# will be executed after starting the Apache daemon, httpd.
# The script will then inform the user whether or not the file
# was successfully retrieved.

[ -f $1 ] && echo "File $1 exists, now copying file to [DIRECTORY] and starting httpd daemon..." || echo "File $1 does not exist. Please try again."
cp -rv $1 [DIRECTORY]
start httpd service
 
Junior Spellweaver
Joined
Oct 27, 2008
Messages
165
Reaction score
89
Code:
if [ condition  ]; then
   condition true instructions 
else
   condition false instructions
fi

In your case:
Code:
if [ -f $1 ]; then
   echo "File $1 exists, now copying file to [DIRECTORY] and starting httpd daemon..." 
   cp -rv $1 [DIRECTORY]
   start httpd service
else
   echo "File $1 does not exist. Please try again."
fi
 
Evil Scottish Overlord
Legend
Joined
May 18, 2007
Messages
5,844
Reaction score
5,250
Alright could use a little more help with this. I'm having a brain fart as to how I'd start the next part of my script, which is to have it access my Apache server to retrieve a test file from my non-default directory and display the success/error message. Any pointers? GHOST107
 
Junior Spellweaver
Joined
Oct 27, 2008
Messages
165
Reaction score
89
Every application in linux returns a exit status(0 means success, any other message is a error status), if any problems happened, the exit status is stored into "$?", if you want to get a file from your server you can use wget(below are the exit status of wget)


Code:
url=localhost/myFile.txt
wget -qO- $url &> /dev/null
case $? in 
   0) echo No problems occurred.;;
   1) echo Generic error code.;;
   2) echo Parse error—for instance, when parsing command-line options, the ‘.wgetrc’ or ‘.netrc’...;;
   3) echo File I/O error.;;
   4) echo Network failure.;;
   5) echo SSL verification failure.;;
   6) echo Username/password authentication failure.;;
   7) echo Protocol errors.;;
   8) echo Server issued an error response.;;
   *) echo Not documented error;;
esac
 
Last edited:
Evil Scottish Overlord
Legend
Joined
May 18, 2007
Messages
5,844
Reaction score
5,250
Ah poop yeah forgot about wget :laugh: alright cheers man, might need to rack your brains later though but thanks in the mean time!
 
Back
Top