shell script question

I have script as following..

server_status= some command | grep "Total error: 0"
if [ "$server_status= "server may down""]; then
echo " Server $(hostname) is Down" >>Result
fi
else
echo " Server is OK on $(hostname)" >>Result

the if command seems to be not working properly for some reason. I dont even see the out put file.

when I do
server_status= some command | grep "Total error: 0"

it gave me the error "server may down".

Any ideas. Please help

Your 'fi' is in the wrong place.

if [ something ] then;
  do something
else
  do something else
fi

if I put 'fi', it should give me the error right?

if [-f /src/srv1] then
server_status= some command | grep "Total error: 0"
if [ "$server_status= "server may down""]; then
echo " Server $(hostname) is Down" >>Result
fi
else
echo " Server is OK on $(hostname)" >>Result
fi

its going to else condition, and now I got a output file and i see that server is OK, even the server is down. I think problem with "if [ "$server_status= "server may down""]; then" . I'm really not sure if this is right. Thanks.

If you used code tags it would be easier to read for everyone who's trying to help you.

Please use the code tags to make this more readable. I didn't at first notice the nested if statements. Also, something burned into my head in high school (many, many moons ago) was code formatting for easier reading/debugging.

if [-f /src/srv1]
then
  server_status= some command | grep "Total error: 0"
  if [ "$server_status= "server may down""]
  then
    echo " Server $(hostname) is Down" >>Result
  else
    echo " Server is OK on $(hostname)" >>Result
  fi
fi

your code will say the server is OK only if /src/srv1 doesn't exist. I've moved the code around a bit, give this a go.

Fix your code.

if [ "$server_status= "server may down""]

Change it to:

if [ "$server_status" = "server may down"]

Thanks for your reply. code tags???

I tried with

if [ "$server_status" = "server may down"]

but I dont see the out file name when server is down. I think as soon as it reads this line "server_status= some command | grep "Total error: 0" and as the server is down, it's giving error saying server is down. it should go to the next line and check if condition, then it should create a file.

small change is the code, instead of checking for file, I'm checking for the directory with -d option.
Thanks

well, I surprised that you never heard something like "code tags" during your one year.

anyways, Whenever you post the code (or part of your code), Please select the code and click on the "#" button on the toolbar.
you can see the difference between your post and others reply in this thread.

about your problem:

server_status= some command | grep "Total error: 0"

what is the actual command here?
also if there is a command then you have to use "$()" or "``".

server_status=$(some command | grep "Total error: 0")

I never realised that...anyways here is the command

part_sync_status_edir2=`/opt/novell/eDirectory/bin/ndspath ndsrepair --config-file /srv/eDirectory/edir2/nds.conf -E | grep "Total errors: 0"`;

this is basically novell edirectory related command. when I run this command normally I get output the screen, I'm trying to run this command with shell script to automatically run that command. But when I run that command I'm getting output to the screen instead of going to next line. here is my full script

if [ -f "/srv/eDirectory/edir2/nds.01" ]; then

part_sync_status_edir2=`/opt/novell/eDirectory/bin/ndspath ndsrepair --config-file /srv/eDirectory/edir2/nds.conf -E | grep "Total errors: 0"`;
  if [ "$part_sync_status_edir2" == "" ]; then
	echo "Edir2 Partition Synchronization Error(s)!! Please Check!!" | mail -s "Edir2 Health Check: Edir2 Partition Sync. Error(s)!" admin@company.com
  fi

Thanks

if you add "set -x" to the beginning of your script you can see what is happening as well as what the variables are set to while it's running. Is it possible the output of the ndspath is different than what you're expecting? Also, I was wondering if your output is going to stderr instead of stdout.

Dont worry about the output coming on to the screen for now.
That can be fixed with a redirection.

  1. In the output that you see on the screen, do you see the line
    "Total errors: 0"

  2. In the below line:

if [ "$part_sync_status_edir2" == "" ]; then
Why are you using "==" ?
Should it not be "=" ?

I think the below code is good enough:

if [ "$part_sync_status_edir2" ]; then

---------- Post updated at 03:54 PM ---------- Previous update was at 02:44 PM ----------

Dont you think this will be good enough?

 
if [ -f "/srv/eDirectory/edir2/nds.01" ]; then
   /opt/novell/eDirectory/bin/ndspath ndsrepair --config-file /srv/eDirectory/edir2/nds.conf -E
   if [ "$?" = "0" ]; then
      echo "All is fine.....removethisline later"
   else
      echo "Edir2 Partition Synchronization Error(s)!! Please Check!!" | mail -s "Edir2 Health Check: Edir2 Partition Sync. Error(s)!" admin@company.com
   fi
fi
if [ "$part_sync_status_edir2" ]; then

This seems to be working ok but need to do some more testing. I have a question here.
is that work if "part_sync_status_edir2" value not equal to 0 or if there is any other value. Some times i get server may be down error when I run the command. does it work either scenarios?

Try this:

server_status=`some command | grep "Total error: 0"`

the symbol ` is the back quote. It is needed to redirect output of your command line to variable.

  1. Actually, if the return value is not "0",then IF will work accordingly, take care of it.

  2. I dont know what "part_sync_status_edir2" does. You will have to experiment.
    Within "part_sync_status_edir2" if you ECHO "THIS FAILED", then because the last command executed is ECHO, which was successful, then it will be considered as 0.
    But if the last command is an EXIT command with a value, like "exit -1" then you are good to go.

  3. Let us take your case. see the code.

 
part_sync_status_edir2=`/opt/novell/eDirectory/bin/ndspath ndsrepair --config-file /srv/eDirectory/edir2/nds.conf -E | grep "Total errors: 0"`;

Here, because grep is the last command, you will "greps" failure code and not ndspath's failure code.

So the best practise is change the code to (&& instead of a |):

 
part_sync_status_edir2=`/opt/novell/eDirectory/bin/ndspath ndsrepair --config-file /srv/eDirectory/edir2/nds.conf -E && grep "Total errors: 0"`;

Hope I have successfully confused enough. :slight_smile:

Is "part_sync_status_edir2" a script?