-1

I just wanna to implement simple if & thenstatement in my Script, but in two part. If the first one (primary) became successful then only the second one will be get execute, else it will be get Exit.

Like -

# script_1.sh 
function() 
{ 
    sub_function() 
    {
        #cmd
    }
 }

#script_2.sh source script_1.sh function sub_function

How it could be possible ?

builder-7000
  • 7,131
  • 3
  • 19
  • 43
  • 3
    From the description, it's very difficult to understand what you're trying to do and where you're having trouble. Can you please add some code to your question? Best would be if you could create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – ghoti Feb 14 '19 at 03:29
  • #script_1.sh function() { sub_function() { #cmd } } #script_2.sh source script_1.sh function sub_function – Shankar Majumder Feb 14 '19 at 03:30
  • 2
    Heh, if it's relevant to your question, please put it [in your question](https://stackoverflow.com/posts/54682768/edit). – ghoti Feb 14 '19 at 03:30
  • If my answer doesn't solve the problem that you're looking to fix, then you'll probably need to provide a better example in your question as it's still hard to understand. Give us an example of the commands you'd type into the terminal and the results you would like to get out. – Crypteya Feb 14 '19 at 03:44

1 Answers1

-1

I might not have understood your question correctly but you might be able to use &&

Putting && between two commands means that the second command will only be executed if the first command succeeds.

e.g.

[root@box ~]# echo 'first command' && echo "I am printed because the first command succeeded"
first command
I am printed because the first command succeeded
[root@box ~]# veafdasvea && echo "This string is not printed because the first command failed"
bash: veafdasvea: command not found
Crypteya
  • 147
  • 11
  • Thanks, but how could I use the `&&` Operator, while executing a two part Script ? – Shankar Majumder Feb 14 '19 at 03:46
  • `./script1.sh && ./script2.sh` might do what you want. script2.sh will only run if script1.sh completes successfully. Does that make sense? – Crypteya Feb 14 '19 at 03:49
  • You're a simply Genius. – Shankar Majumder Feb 14 '19 at 03:51
  • Glad I could help. I've also added some info about if/else statements in case you're interested. Feel free to upvote my answer if it helped you. – Crypteya Feb 14 '19 at 03:54
  • Lot of Thanks for the update, actually I was planning to execute more than two or three sub-script in same manner, but while using `else` you can go up to just one Level only. – Shankar Majumder Feb 14 '19 at 04:01
  • So .. your `if [[ $(...) ]]` example is a problem. The stuff inside `[[ ... ]]` needs to follow what's documented in the **CONDITIONAL EXPRESSIONS** section of the bash man page. It's based on `/bin/test`, and one should NOT rely on the output of `command_that_could_whatever` to produce appropriate expressions. If you want to test the result of a command, simply use: `if command; then`. – ghoti Feb 14 '19 at 04:10
  • Thanks @ghoti , I've removed the offending example as the user wasn't after if statements anyway. But I see what I did wrong (using the output of the command rather than `if command; then` which uses the return code instead of the output). – Crypteya Feb 14 '19 at 04:13
  • Is it possible to adding a new statement of `if & then` into the `then` of Previous one ? I mean in that way also we can achieve the same objective as well. – Shankar Majumder Feb 14 '19 at 04:26
  • Kind of. You can do `if, elif, elif, elif, then` with as many or as few `elif`s as you like. If the first condition is false, the second one is checked, if that one is false, the third is checked. If any condition is passed, then the code block below it is executed. If no code blocks pass, then the code under the `else` block is applied. – Crypteya Feb 14 '19 at 05:40
  • For an example of an if, elif, else format, see this answer: https://stackoverflow.com/a/16034851/3465014 – Crypteya Feb 14 '19 at 05:42