LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2011, 04:22 PM   #1
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Rep: Reputation: Disabled
Question 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
 
Old 11-25-2011, 04:39 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
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
 
1 members found this post helpful.
Old 11-25-2011, 04:49 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,935

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by Sha_unix View Post
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
 
Old 11-25-2011, 04:50 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,935

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by Sha_unix View Post
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.
 
Old 11-25-2011, 04:57 PM   #5
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
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"
 
0 members found this post helpful.
Old 11-26-2011, 11:16 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,935

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by Sha_unix View Post
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.
 
Old 11-28-2011, 04:03 AM   #7
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
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"
 
Old 11-28-2011, 09:48 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,935

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by Sha_unix View Post
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???
 
Old 11-28-2011, 09:53 AM   #9
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-28-2011, 11:19 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,935

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by Sha_unix View Post
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.
 
Old 11-30-2011, 01:55 AM   #11
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 06:00 PM
Why For loop in C runs infinitely when only 'equal to' operator is given in condition rmnature Programming 7 02-24-2009 09:28 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 04:50 AM


All times are GMT -5. The time now is 12:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration