How Do You Get JavaScript to Present in a Web Browser?

Problem scenario
You have a .js file on your desktop or on a web server. You want to have the code execute and present itself nicely (not just see the raw code) in a web browser. How do you do this?

Solution
You must use the .html extension. You must have

<html> </html> 

tags. Here is an example. Name this file contint.html and place it on your desktop, and then open it with a web browser.

<!DOCTYPE html>
<html>
<body>

<h2>Use JavaScript to Change Text</h2>
<p>This example writes "Hello in JavaScript from Continualintegration.com!" into an HTML element with id="demo":</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello in JavaScript from Continualintegration.com!";
</script> 

</body>
</html>

How Do You Use awk for String Replacement of Lines where the Word to Be Replaced Is in the Same Position?

Problem scenario
You want to substitute the nth word of a given line. How do you use awk to accomplish this?

Solution
Let's assume the lines that you want to modify are in goodfile.txt. To replace the fourth item in the string with "new string", run this command:

cat goodfile.txt | awk '{ print $1 " " $2 " " $3 " new string " $5 " " $6 }'

How Do You Troubleshoot a Bash Script Copied from the Internet?

You have one of the following two problems:

Problem scenario #1
Your GCP startup-script is not working. You do not know why. What should you do?

OR

Problem scenario #2
A bash script is not working. It stops executing after a certain line. There are few clues as to what is wrong.

Solution
Did you copy the script from a webpage? If so the root cause could be the web page you copied it from used a slightly different character for the double quotes. Left double quotation or right double quotation marks will cause a problem. Make sure the quotes are not or . You want just regular quotes like this ".

Notepad, Notepad++, and vi support different double quotes (left, right and straight). Be careful to not use or unless you know what you are doing in programming. You normally want to use ".

See also this posting.

Why Did Apple Call One of Their Computers Macintosh?

Question
Why did Apple call one of their computers the Macintosh?

Answer
It was based on a type of apple that you eat (e.g., golden delicious, Fuji, etc.). It would have been called the McIntosh, but there were legal reasons for adding the letter "a". To read more about how the name was chosen, see this page.

There is no connection between the Macintosh name and the term MAC sublayer of the OSI networking model or the historic MAC Project at MIT. (The word "daemon" in computers seems to have originated from this MIT project.)

How Do You Delete Files from a Directory That Are Smaller Than 10 KB with a Bash Command?

Problem scenario
You want to delete all the files (but not subdirectories) that are less than 10 KB in Linux's /tmp/ directory. How do you do this?

Solution
Run this command: find /tmp/ -maxdepth 1 -type f -size -10k -ls | awk '{print $NF}' | rm -rf

How Do You Get the “Up” Arrow to Show a Previous Command in Linux?

Problem scenario
When you press the up arrow on the keyboard you see " ^[[A". You want to see the previous command that was entered. How do you enable history at the command line?

Solution
Root cause: The /etc/passwd file has an entry like this:

cooluser:x:1001:1002::/home/cooluser:

(This could be caused if you created the user quickly with a useradd command.)

Procedures

Run this command: sudo cat /etc/passwd | grep $(whoami)

Do you see a /bin/bash at the end? If not you need to modify the /etc/passwd file to have a /bin/bash at the end of the stanza for your user having the problem. This is how it should look when you are done:

cooluser:x:1001:1002::/home/cooluser:/bin/bash

# replace "cooluser" with the name of the user you want to change the behavior of the up arrow for.

You have to log out and log back in for the changes to take effect.

How Do You Zero out a /var/log/mail File?

Problem scenario
You backed up /var/log/mail to a different file. You want the /var/log/mail file to start empty so you can review it without looking at old activity. What do you do?

Solution
Run these four commands:

sudo su -
cd /var/log
> mail
exit

Now you /var/log/mail file will still exist and record activity as normal. It will not have anything in it before the time you ran the above "> mail" command.

What Does “__” Mean in Python?

Problem scenario
You see two underscores or two underbars before a function in Python. What does this syntax signify?

Possible Solution #1
The answer is best explained by running this program once with no modification and a second time with a modification.

class ContintClass():
     def __init__(self):
             self.__completelyprivate = "1111111111"
             self._semiprivate = "2222222"


foo = ContintClass()
#print(foo.__completelyprivate)

print("Above is an attempt to print a completely private data member of an object")

print("Below is an attempt to print a semi-private data member of an object")
print(foo._semiprivate)

Before you run the program the second time, uncomment out the line "print(foo.__completelyprivate)" by removing the "#". The first time you will see this:

Above is an attempt to print a completely private data member of an object
Below is an attempt to print a semi-private data member of an object
2222222

The second time you run the program (after you uncomment out that line), you will see this:

Traceback (most recent call last):
File "foobar.py", line 8, in
print(foo.__completelyprivate)
AttributeError: 'ContintClass' object has no attribute '__completelyprivate'

Two underscores refer to an object that is completely private. One or fewer underscores relating to a class will make the data member accessible outside of the class via the function itself. Two or more underscores in a data member's variable name of a class will make the data member never be accessible outside of the class via the function itself.

According to Expert Python Programming (on page 191), "[n]o user-defined method should use this convention unless it explicitly has to implement one of the Python object protocols."

Possible Solution #2
When the __ appears in the __init__, the underscores are part of a reserved word. To instantiate a class, or to create an object in a Python class, the __init__ word must be present. If you try a different word (unlike the use of self in object-oriented programming in Python), it will not work. The underscores are necessary; you can try it for yourself by modifying the above program's __init__ keyword.

Possible Solution #3
It could refer to __init__.py. This is a reference to a package in Python called a "namespace package" (as contradistinguished from a regular package). These packages are composed of smaller packages. To learn more about this, see this external link or this one.

Possible Solution #4

Every module in Python has a "built-in __name__ variable."  When the program is run by itself, the variable is "__main__".  But when the program is invoked via an "import" statement of a different program, the "__name__" variable is not "__main__".

(These above three sentences are quoted and paraphrased from page 84 of Programming in Python.)

Possible Solution #5
The term dunders is a portmanteau of "double underscores". This syntax denotes a magic method in Python (with two prefix and two suffix underbars). To read more, see https://www.geeksforgeeks.org/dunder-magic-methods-python/ or https://www.python.org/dev/peps/pep-0008/#module-level-dunder-names According to Expert Python Programming (page 191) the term to describe this syntax is "special methods" (with the term "dunder methods" being obsolete); the 4th edition can be found here.