How do I pass password to sudo commands?

If you want to run a sudo command without being prompted to input the password you can do the following command.

echo password | sudo -S rm -rf /opt/jetty/

In the command above we are trying to remove the /opt/jetty directory using the rm -rf command. The -S (stdin) option allow the sudo command to read password from a standard input instead of a terminal device.

If you want to store the password in a file you can use the cat command instead of echo like the following example.

cat password.txt | sudo -S rm -rf /opt/jetty/

How do I get file separator symbol?

Creating a program to be run on more than one platform such as Windows and Linux our program need to understand the difference between both platform. The simplest thing for instance is the file separator. Windows use "\" (back slash) while Linux use "/" (forward slash).

To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator key.

package org.kodejava.example.lang;

public class FileSeparatorExample {
    public static void main(String[] args) {
        // file.separator system property return the correct file 
        // separator for each different platform (Windows = \), 
        // (Linux = /)
        String dataFolder = System.getProperty("user.dir") +
            System.getProperty("file.separator") + "data";

        System.out.println("Data Folder = " + dataFolder);
    }
}