Shell/Bash scripting cheatsheet

Shell Bash Scripting Cheatsheet

Examples

$ NAME="Interview Bubble"$ echo $NAMEInterview Bubble
# Each command starts on a new line, or after semicolonecho 'This is the first line'; echo 'This is the second line'

This is the first line
This is the second line

Shell Variables

$variable ="Hello"echo $variable

Hello

Steps in creating a Shell Script

1. Create a file using a vi editor(or any other editor).  Name  script file with extension .sh

$ vi helloWorld.sh

2. Start the script with #! /bin/sh

3. Write some code.

4. Save the script file as filename.sh

#! /bin/sh# This is a commentecho "Hello World"  # This ia also a comment

5. For executing the script type bash filename.sh

$ bash helloWorld.sh Hello World

Lombok @EqualsAndHashCode Annotation

Giving the class an @EqualsAndHashCode annotation properly overrides the equals and hashCode methods of the target class depending on the members in the class.@EqualsAndHashCode without specifying a parameter will generate processing of equals, hashCode using all members in the class

@EqualsAndHashCode public class PersonLombok {	String firstName;	String lastName;	LocalDate dateOfBirth;}

Lombok @ToString Annotation

@ToString annotation provides a toString() implementation. Let’s see this:

By default, it’ll print your class name, along with each field, in order, separated by commas

@ToStringpublic class PersonLombok {	String firstName;	String lastName;	LocalDate dateOfBirth;}

Lombok @Data Annotation

By annotating the class with @Data , you get the same effect as setting all the following annotations.

  • @ToString
  • @Getter
  • @Setter
  • @RequiredArgsConstructor
  • @EqualsAndHashCode
import lombok.Data;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entity@Datapublic class Todo {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private String summary;    private String description;}

Project Lombok @Log Annotation

You put the variant of @Log on your class (whichever one applies to the logging system you use); you then have a static final log field, initialized to the name of your class, which you can then use to write log statements.

@Log: creates

private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());

@Log4j: creates

private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);

@Log4j2: creates

private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);

@Slf4j: Creates

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);

Lombok @Synchronized Annotations

@Synchronized is a safer variant of the synchronized method modifier. Like synchronized, the annotation can be used on static and instance methods only. It operates similarly to the synchronized keyword, but it locks on different objects. The keyword locks on this, but the annotation locks on a field named $lock, which is private.
If the field does not exist, it is created for you. If you annotate a static method, the annotation locks on a static field named $LOCK instead.

import lombok.Synchronized;public class SynchronizedExample {  private final Object readLock = new Object();   @Synchronized  public static void hello() {    System.out.println("world");  }    @Synchronized  public int answerToLife() {    return 42;  }  @Synchronized("readLock")  public void foo() {    System.out.println("bar");  }}

Lombok @Builder annotation

Lombok provides an annotation for implementing the Builder pattern on your classes.

@Builder can be placed on a class, or on a constructor, or on a method. While the “on a class” and “on a constructor”

@Getter@EqualsAndHashCode@AllArgsConstructor@Builderpublic class Person {  private final String firstname;  private final String lastname;  private final String email;}

Lombok @SneakyThrows Annotation

@SneakyThrows can be used to sneakily throw checked exceptions without actually declaring this in your method’s throws clause.

import lombok.SneakyThrows;public class SneakyClone implements Cloneable {    @SneakyThrows    @Override    protected Object clone() {        return super.clone();    }}

What happened behind:

public class SneakyClone implements Cloneable {    @Override    protected Object clone() {        try {            return super.clone();        } catch (final java.lang.Throwable $ex) {            throw lombok.Lombok.sneakyThrow($ex);        }    }}

Lombok @NonNull Annotation

You can use @NonNull on the parameter of a method or constructor to have lombok generate a null-check statement for you.

class User {    @Getter @Setter @NonNull private String name;}

The above annotated code is equivalent to the following java code –

class User {    @Getter @Setter @NonNull private String name;    @NonNull    public String getName() {        return name;    }    public void setName(@NonNull final String name) {        if(name == null) throw new NullPointerException("name");        this.name = name;    }}