lesnofar.blogg.se

Bash script example for running java classes mac
Bash script example for running java classes mac










bash script example for running java classes mac

It is also possible to have an if statement inside of another if statement.Below is a simple JSch example program to run the “ls -ltr” command on the server. You may have as many if statements as necessary inside your script. Here's a perfect example of when it makes life easier for you.

#Bash script example for running java classes mac code

I would highly recommend you do indent your code however (especially as your scripts get larger) otherwise you will find it increasingly difficult to see the structure in your scripts. There aren't any rules regarding indenting in Bash so you may indent or not indent however you like and your scripts will still run exactly the same. The aim is to improve readability and make it harder for us to make simple, silly mistakes. This is referred to as indenting and is an important part of writing good, clean code (in any language, not just Bash scripts). You'll notice that in the if statement above we indented the commands that were run if the statement was true.

  • Line 12 - Test the size of myfile again.
  • Line 11 - Redirect some content into myfile so it's size is greater than zero.
  • Line 8 - Is the size of myfile greater than zero?.
  • bash script example for running java classes mac

  • Line 7 - Create a new blank file myfile (assuming that myfile doesn't already exist).
  • Line 4 - This time we are performing a numerical comparison.
  • Line 2 - The variable $? holds the exit status of the previously run command (in this case test).
  • Test doesn't print the result so instead we check it's exit status which is what we will do on the next line.
  • Line 1 - Perform a string based comparison.
  • Because is just a reference to the command test we may experiment and trouble shoot with test on the command line to make sure our understanding of its behaviour is correct.
  • Remember that a path may be absolute or relative and may refer to a file or a directory.
  • When we refer to FILE above we are actually meaning a path.
  • character for character the same) whereas -eq does a numerical comparison meaning will return true. will return false as = does a string comparison (ie. it is not empty).įILE exists and the write permission is granted.įILE exists and the execute permission is granted. INTEGER1 is numerically less than INTEGER2įILE exists and the read permission is granted.įILE exists and it's size is greater than zero (ie. INTEGER1 is numerically greater than INTEGER2 INTEGER1 is numerically equal to INTEGER2 The lengh of STRING is zero (ie it is empty). The length of STRING is greater than zero. Look up the man page for test to see all of the possible operators (there are quite a few) but some of the more common ones are listed below. This means that all of the operators that test allows may be used here as well. The square brackets ( ) in the if statement above are actually a reference to the command test. It is always good practice to test your scripts with input that covers the different scenarios that are possible. If follows the format below:Īnything between then and fi (if backwards) will be executed only if the test (between the square brackets) is true. If it is not true then don't perform those actions. Basic If StatementsĪ basic if statement effectively says, if a particular test is true, then perform a given set of actions. Like what we have looked at in previous sections, their syntax is very specific so stay on top of all the little details. If statements, combined with loops (which we'll look at in the next section) allow us to make much more complex scripts which may solve larger tasks.

    bash script example for running java classes mac

    They allow us to decide whether or not to run a piece of code based upon conditions that we may set. If statements (and, closely related, case statements) allow us to make decisions in our Bash scripts. In this section of our Bash Scripting Tutorial you will learn the ways you may use if statements in your Bash scripts to help automate tasks.












    Bash script example for running java classes mac