LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Condition loop. (https://www.linuxquestions.org/questions/linux-newbie-8/condition-loop-915542/)

Sha_unix 11-25-2011 03:22 PM

Condition loop.
 
Hi,

i am checking for a file sha.txt if not found then it will execute ./scr1 else it will exit.

i tried below elif statement to try if ./scr1 script gives error it should exit not execute below commands. But i am unsuccessful. Please help.

Code:

cd $HOME
if [ ! -f sha.txt ]; then
      ./scr1
 elif [ $# -eq 0 ];then
      echo " The output is correct"
      echo " successfull"
else
    echo "The file sha.txt is not found"
exit
fi


jhwilliams 11-25-2011 03:39 PM

if/elif/else are all at the same level. You want to nest an if statement, to check something that happens in the first if.

Also, $# is the number of positional parameters, you seem to want $?, last command's return code.

Code:

if [ ! -f sha.txt ]; then
    ./scr1
    if [ $? -eq 0 ]; then
        echo " The output is correct"
    fi
else
    echo "The file sha.txt is not found"
    exit
fi


TB0ne 11-25-2011 03:49 PM

Quote:

Originally Posted by Sha_unix (Post 4533927)
Hi,
i am checking for a file sha.txt if not found then it will execute ./scr1 else it will exit. i tried below elif statement to try if ./scr1 script gives error it should exit not execute below commands. But i am unsuccessful. Please help.

Code:

cd $HOME
if [ ! -f sha.txt ]; then
      ./scr1
 elif [ $# -eq 0 ];then
      echo " The output is correct"
      echo " successfull"
else
    echo "The file sha.txt is not found"
exit
fi


How about telling us what error(s) you're getting??? And again, as with your other threads, have you tried looking any scripting guides up, or trying some different permutations of this?
Code:

cd $HOME
if [ ! -f sha.txt ]
 then
      ./scr1
 elif [ $# -eq 0 ]
 then
      echo " The output is correct"
      echo " successfull"
else
    echo "The file sha.txt is not found"
exit
fi


TB0ne 11-25-2011 03:50 PM

Quote:

Originally Posted by Sha_unix (Post 4533927)
Hi,
i am checking for a file sha.txt if not found then it will execute ./scr1 else it will exit. i tried below elif statement to try if ./scr1 script gives error it should exit not execute below commands. But i am unsuccessful. Please help.

Code:

cd $HOME
if [ ! -f sha.txt ]; then
      ./scr1
 elif [ $# -eq 0 ];then
      echo " The output is correct"
      echo " successfull"
else
    echo "The file sha.txt is not found"
exit
fi


How about telling us what error(s) you're getting??? And again, as with your other threads, have you tried looking any scripting guides up, or trying some different permutations of this?
Code:

cd $HOME
if [ ! -f sha.txt ]
 then
      ./scr1
 elif [ $# -eq 0 ]
 then
      echo " The output is correct"
      echo " successfull"
else
    echo "The file sha.txt is not found"
exit
fi

Hard-coding a filename into a script isn't a good thing to do, nor is calling the scr1 script from a "./" directory. Makes your scripts much more prone to failure, and harder to maintain.

Sha_unix 11-25-2011 03:57 PM

i tried all permutation.. what i am getting is, in ./scr1 script i just wrote echo " Here everything is fine" all the conditions were running fine. But when i changed that script (scr1) as eho "Here everything is fine" ie., instead of "echo" i wrote "eho" but still elif condition is true elif [ 0 -eq 0 ] and all other below commans ran. ?

Code:

[prompt] sh -x sha.sh
+ date +%m%d
xdte=1125
dblist=amos mss
HOME=/usr/home
DBPATH=dbpath/path
+ cd /usr/home
+ [ ! -f sha.txt ]
+ [ 0 -eq 0 ]
+ echo  The output is correct
 The output is correct
+ echo  successfull
 successfull
[prompt] cat /usr/home/progress/log/scr1
eho "every thing is fine here"


TB0ne 11-26-2011 10:16 AM

Quote:

Originally Posted by Sha_unix (Post 4533950)
i tried all permutation.

No you didn't. What I posted above worked with no errors, and a minor change. That change I found on the first Google hit when searching for bash scripting examples to check if a file didn't exist.
Quote:

what i am getting is, in ./scr1 script i just wrote echo " Here everything is fine" all the conditions were running fine. But when i changed that script (scr1) as eho "Here everything is fine" ie., instead of "echo" i wrote "eho" but still elif condition is true elif [ 0 -eq 0 ] and all other below commans ran. ?
So your problem is actually in the scr1 script, not what you posted? And amazingly, if you have errors in the script ("eho" instead of "echo"), the script won't work. Also, see previous statement about the very bad idea of using "./" to specify a path, and about hard-coding names into scripts. One of the MANY bash scripting tutorials you can find with a Google search is:
http://tldp.org/LDP/abs/html/

Lots of examples and advice.

Sha_unix 11-28-2011 03:03 AM

TB0ne,

This is just a testing, i am going to implement after i am successful. So ./scr1 for now is no problem.

Quote:

:
So your problem is actually in the scr1 script, not what you posted? And amazingly, if you have errors in the script ("eho" instead of "echo"), the script won't work.
exactly i want it to come out when some thing is wrong in the script "scr1". not executed other below commands

Code:

      echo " The output is correct"
      echo " successfull"


TB0ne 11-28-2011 08:48 AM

Quote:

Originally Posted by Sha_unix (Post 4536054)
TB0ne,
This is just a testing, i am going to implement after i am successful. So ./scr1 for now is no problem.

exactly i want it to come out when some thing is wrong in the script "scr1". not executed other below commands

Code:

      echo " The output is correct"
      echo " successfull"


Again, you need to go through the bash scripting tutorial you've been directed to numerous times.

Read your script, and think about what it's doing. You're checking for file sha.txt, and if it's not found, it runs scr1, with an elif. And did you read/follow the advice given to you by jhwilliams???

Sha_unix 11-28-2011 08:53 AM

yes, i changed it to $? according to hime.

i am very new to shell scripting trying to explore myself.

i will just try to explain what i want.

if sha.txt is not there then it will run scr1, in scr1 there are certain simple commands are there if it fails i am trying to catch up that thing in elif condition and trying to stop other command to run after elfi. Hope i made it clear.

TB0ne 11-28-2011 10:19 AM

Quote:

Originally Posted by Sha_unix (Post 4536357)
yes, i changed it to $? according to hime.

i am very new to shell scripting trying to explore myself.

i will just try to explain what i want. if sha.txt is not there then it will run scr1, in scr1 there are certain simple commands are there if it fails i am trying to catch up that thing in elif condition and trying to stop other command to run after elfi. Hope i made it clear.

Yes, we understand. Again, go through the scripting tutorials, because that's how you're going to learn. And again, THINK about what you're doing, what the result(s) you want are, then step-by-step, THINK about how to achieve them.

You state your problem...would seem to make sense that you'd just put an echo statement in your script, to see what the return code of scr1 actually IS, so you can diagnose your problem from there. Also, just perform that ONE step, alone, by itself, until you get it to work...THEN add in more logic, so that you know where the problem(s) are as you go.

Sha_unix 11-30-2011 12:55 AM

i tried and did it myself....

it was that i have start one if statment in src1 only not out side.

Code:

eho "every thing is fine here"
if [ $? -eq 0 ];then
  echo "The output executed was right"
else
  exit 1
fi

MAIN SCRIPT

Code:

cd $HOME
if [ ! -f sha.txt ]; then
    ./src1.sh
else
    echo "The output was wrong"
exit
fi



All times are GMT -5. The time now is 03:26 AM.