How Do You Troubleshoot “command not found” in a Bash Script?

Problem scenario
You have a function defined in your Bash script.   You call the function you created.  But you get "command not found".  What should you do?

Solution
The Bash script is parsed from the top of the file to the bottom of the file.  You cannot invoke the function before it has been defined.  Place the function above the line where it is called.  The function should probably go near the top despite the lack of readability.

How Do You Troubleshoot the Oracle and Java Message “java.sql.sqlexception ora-01003 no statement parsed”?

Problem scenario
You run a Java program that runs a SQL statement in an Oracle database.  You get this error: "java.sql.sqlexception ora-01003 no statement parsed".  What do you do to fix this?

Solution
Run the Java program a second time.  Does the problem remain?  It could be that there was a successful DDL command, and this error could be ignorable.  

You may want to try these links for further info:
https://stackoverflow.com/questions/23866710/ora-01003-no-statement-parsed
https://community.oracle.com/thread/736289 (this link used to work)

How Do You Use Encapsulation in Python?

Problem scenario
You know about encapsulation.  You know about private methods that are only accessible from inside a class -- not outside.  You want to encapsulate a method in a class in Python.  How do you write such a program?

Solution
"Programmers are most effective if shielded from, not exposed to, the innards of modules not their own." (This was taken from page 272 of The Mythical Man-Month.)

Here is an example; you can call the file encapsulate.py.  If you uncomment the "good_widget.__makeone()", you will see the program fail; you will see the private method __makeone() work in this program if you run it exactly how it appears below (using Python 3):

#!/usr/bin/env python

class WidgetFactory:

  def __init__(self):
    self.__makeone()

  def funtest(self):  # This is a public method.
    print('This widget is working!')

  def __makeone(self):   # This __ syntax tells Python it is a private method.
    print('creating one widget; please wait...')

good_widget = WidgetFactory()
good_widget.funtest()
#good_widget.__makeone()  #  If you uncomment this line, this line will cause the program to throw an error.

For further information:
"Python doesn't have real private methods, so one underline in the beginning of a method or attribute means you shouldn't access this method, because it's not part of the API." (This quote was taken from an external website.) "Other programming languages have protected class methods too, but Python does not." (This quote was taken from https://pythonspot.com/encapsulation/.)

How Do You Troubleshoot JIRA Not Working due to Not Finding a Suitable Driver?

Problem scenario
Atlassian JIRA is not working.  The web UI does not have a login page.  On the back-end in the logs, you see an error about not finding a suitable driver.  (You may get an error about a port number instead.  This problem may require a similar solution to the one proposed below for an error about a suitable driver.)  You know you have the ojdbc.jar file (or variation thereof) in the correct place.  What should you do?

Solution
Verify the dbconfig.xml file is configured properly.  The individual stanzas should each be verified for accuracy.  If the database hostname was entered incorrectly (and this will not necessarily be obvious), the error you get could misleadingly refer to a database driver.  The real problem could be a typo in the hostname for the database.  An incorrect database server or network disconnect between the JIRA server and the database server could be the real problem.  

If the ojdbc.jar file became corrupt, the error may not have been misleading after all.  You may actually need a new .jar file from Oracle's website.  For more information, you may want to see this link.

With awk, What Do You Do to Get a Colon To Be Recognized as a Separator of Fields?

Problem scenario
You have Awk 4.1.3 installed.  The default field separator in awk is a space.  You want to assign it to a different character -- specifically a colon ":".  You have a string with a colon assigned to the variable "y".  You have tried these things:

echo $y | awk -F ":" '/1/ {print $NF}'  # this prints nothing
echo $y | awk '{FS=":"; print $1}'  # this prints y
echo $y | awk 'BEGIN { FS=":" } /1/ { print $2}' # this prints nothing

The above commands do not recognize the ":" as the field separator.  You think regular letters would have worked as a string, but you find the colon ":" to behave differently.  What do you do to get a colon to be recognized as a separator of fields?

Solution
In some ways, this question for those Awk experts reading, is what is the functional difference between awk's "--field-separator" contrasted with Awk's "FS" reserved flag?

To find the version of awk use this command: awk -W version.  Be sure you have a new version (e.g., around 4.1.3).  To upgrade on a Debian/Ubuntu server, run this:  sudo apt-get -y upgrade gawk

For reasons we do not understand, use the unabbreviated "--field-separator" to assign the non-default character to.  The behavior is different.  We find the [more verbose] "--field-separator" works more reliably and predictably.  Here is an example:

echo $y | awk --field-separator=":" '{print $1}'

(We know the awk command is technically a programming language that was an antecedent to Perl.  Today awk is thought of chiefly as a Bash command or utility.)

How Do You Write a Java Program to Accept a Number for the Command Terminal and Print It Back Out?

Problem scenario
You want to write a basic Java program to test it out.  How do you write a program to accept user input, specifically a number, and print it back to the screen (using the integer data type)?

Solution
1.  Install the Java Development Kit if you have not installed it yet.  If you are not sure you have it, run javac -version.  If it says that the command is not found, you need to install the Java Development Kit.  If you do not know how, see this posting.

2.  Create this file named acceptNum.java:

import java.util.Scanner;

public class acceptNum {

public static void main(String[] args) {
   System.out.println("Please enter an integer, then press enter: ");
   Scanner in = new Scanner(System.in);
   int num = in.nextInt();
   System.out.println(num);
  }
 }

3.  Run this command: javac acceptNum.java

4.  Run this command: java acceptNum

If you want to purchase a book on Java, click here. 

How Do You Use an USB-Connected Local Camera with Your Desktop Computer?

Problem Scenario
You purchased a camera that connects to your computer (e.g., a laptop) with a USB cable.  An example may be an SQ9* camera. You now want to use it.  The device connects to your laptop via a USB mini to USB A connector.  How do you view images on your desktop that are captured with the camera lense?

Solution
(This problem assumes that the camera is not plug-and-play.  In our experience, the SQ9* is not plug-and-play.) We have a solution for Windows as well as Linux users.

Solution for Microsoft Windows Users
1.  Go to Control Panel -> Hardware and Sound -> Devices and Printers.  In "Devices" you should see a camera icon. It may say "GENERAL - UVc".  If you do not see it press the "On/Off" button on the camera.  (An SD card is not necessary for basic functionality.)

2.  Install this on your Windows desktop.

3.  Open this application (e.g., WebcamViewerV1.0.exe).  Use the drop down box to find your new camera.  Then click "Connect".

Solution for Raspberry Pi (or Debian/Ubuntu Linux) Users
1.  Open a command terminal.

2.  Enter these commands:

sudo apt-get -y update
sudo apt-get -y install vlc browser-plugin-vlc

3.  Run this command from the terminal to open the GUI application: vlc

4.  In the VLC media player application, go to Media -> Open Capture Device.

5.  In the Open Media sub-window, choose the option (usually the only option) for Video device name that corresponds to your camera.  choose the option (usually the only option) for Audio device name that corresponds to your camera.  

6.  Click "Play".

* Please note that we do not warrant that the night vision aspect of this camera will necessarily work.

How Do You Access the SonarQube Web UI When It Is Only Listening on the 127.0.0.1 IP Address?

Problem scenario
SonarQube is listening on the loopback IP address on port 9000.  SonarQube is not listening on the external IP address.   You want to access the web UI for SonarQube. What should you do?

Solution
Install a web server and configure it to connect to port 9000.  Here are the details for using Apache web server on a Red Hat derivative (e.g., CentOS/RHEL/Fedora).

1.  Run this command:  sudo yum -y install httpd

2.a  Run this command, but replace x.x.x.x with the external IP address of your server (or the URL that you will present with this server):  

sudo vi /etc/httpd/conf.d/sonar.x.x.x.x.conf

2.b.  Place these lines in the file (where the top and bottom lines have "VirtualHost" in them):

<VirtualHost *:80>
    ServerName sonar.x.x.x.x
    ServerAdmin admin@continualintegration.com
    ProxyPreserveHost On
    ProxyPass / http://localhost:9000/
    ProxyPassReverse / http://localhost:9000/
    TransferLog /var/log/httpd/sonar.x.x.x.x_access.log
    ErrorLog /var/log/httpd/sonar.x.x.x.x_error.log
</VirtualHost>

2.c.  Replace x.x.x.x with the IP address or domain name of your server.

2.d.  Replace "admin@continualintegration.com" with an email address of yours.

3.  Run these two commands:

sudo systemctl start httpd
sudo systemctl enable httpd

How Do You Create a .ppk File from a .pem File?

Problem Scenario
You want to use a key kair to log into AWS instances. You created an AWS EC-2 instance (aka a virtual machine or server) running Linux.  You want to connect to this Linux server (a VM).  You created a key pair when you created this VM.  You downloaded the .pem file.  You need a .ppk file to log in.  What should you do?

Solution
1.  Download a puttygen.exe file from this location. (If you have a 32 bit version of Windows, use this link.  If you have a 64 bit version of Windows, use this file.)

2.  Open puttygen.exe (as nothing needs to be installed).

3.  In PuTTY Key Generator, go to File -> Load Private Key.  Find the .pem file that you have.

4.  You should get a "PuTTYgen Notice."  Click "OK" to that.

5.  Enter a Key Passphrase in the field for "Key passphrase". Remember this password and click "Confirm passphrase".

6.  Click "Save Private Key."  Give it a name that you want.  

7.  Open the regular putty.exe.  Expand Connection -> SSH -> Auth.  Near the text field for "Private key file for authentication" field, click the "Browse" button.  Open the .ppk file you just created (in step #6 above).

8.  In Putty go to the "Session" field.  Then click on "Host Name."  Enter the IP address of the EC2 instance you just created.  Click "Open."

9.  The username will be ec2-user if the server is running RHEL or SUSE.  If the server is running Ubuntu, the username will be ubuntu.  The password will be the one you entered in the step that created the .ppk file.

How Do You Troubleshoot “permission denied” when Trying to Clone a Git Repository That You Should Have Access to?

Problem scenario
You configured the source repository (in GitHub, GitLab, in BitBucket or elsewhere) to allow your user to have permissions to read the repository.  You run git clone but you get a "permission denied" error.  What should you do?  

Possible solutions
The Linux server you are on may not give your Linux user permissions to write to the destination directory.  Can you clone the repository to the /tmp/ directory?  This should help you isolate the problem if it works.  See if other users can clone the repository to other servers.  Does the repository path (e.g., ssh:// or http://) have problems in it?  Could a character or two be incorrect and happen to match another repository accidentally?