Understanding Question Mark and Colon Operators in Java Programming

Today I was having issues understanding syntactically how ? and : operators work in Java on some legacy code I was reading. The issue was that all definitions I found through google practically sucked until I found this one by “cricket_007” on StackOverflow.

Either way, I got it figured out and am documenting this and raising awareness for other developers in the future.

Thanks so much “cricket_007” and here is the link if anyone is interested:

Custom SendTo Execution Shortcuts – Windows

Start out with doing “Windows Key + R” and type “shell:sendto”

Next, this folder will pop up:

Create a new Batch File in this directory – I’ve called mine “Launch with Bash.bat” – below is some example code:

@ECHO OFF
SETLOCAL

SET filedrive=%~d1
SET filepath=%~p1
SET filedrive_and_filepath=%filedrive%%filepath%

SET filename=%~n1
SET fileextension=%~x1
SET filename_and_fileextension=%filename%%fileextension%

cd /d %filedrive_and_filepath%
"C:\Program Files\Git\bin\bash.exe" -c "sh ""%filename_and_fileextension%"" "

Note a few things – Batch files can take a starting argument like methods in programming so basically when you right click and do “SendTo” on Windows, it sends the filepath of what you are “Sending” to whatever the destination is.

In a batch file, these parameters are accessed through %1 %2 %3 etc for each positional parameter that is being passed to the “method”

This prewritten file will allow you to create a custom process that easily allows you to launch something with a programmatic process. AKA custom builds, processes, launch parameters, etc.

Quick Docker Import/Export Utilizing gzip for Compression

IMAGE_NAME="repository:image_tag"
OUTPUT_FILE="/path/to/imageOutput.tar.gz"
#docker load < $OUTPUT_FILE
#docker save $IMAGE_NAME | gzip -c &gt; $OUTPUT_FILE

The above snippet keeps replacing the docker save line with the escaped version as &gt instead of >

AKA ->

IMAGE_NAME=”repository:image_tag”
OUTPUT_FILE=”/path/to/imageOutput.tar.gz”
#docker load < $OUTPUT_FILE
#docker save $IMAGE_NAME | gzip -c > $OUTPUT_FILE

Bash Script for Automatic Creation and Generation of Public Private Key for Linux Server Login

Wrote this quick blurb so I never have to do this process again.

The general idea is to take the output that is created on the screen, copy and paste it to a file somewhere on your computer, and either use it as is with your environment ssh configuration or convert it to a *.ppk for Putty.

What the script does is the following:

  1. Create the .ssh directory as the user you are currently logged in as (if it doesn’t already exist)
  2. Set necessary permissions on the directory (if it isn’t already set)
  3. Create the “authorized_keys” file (if it doesn’t already exist)
  4. Generate the Public/Private Pair
  5. Append the thumbprint to the “authorized_keys” file
  6. Output the private key for copying to Standard Out
  7. Delete the generated files for the Public/Private pair

After copying the key from the terminal screen you’ll paste it into a text file and either use it as is or convert it to a *.ppk for usage in Putty.

KEY_OWNER="Cody Garrett"
KEY_NAME="automatic.key"
KEY_NAME_PUB="$KEY_NAME.pub"
mkdir ~/.ssh
chmod 0700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 0644 ~/.ssh/authorized_keys
ssh-keygen -t rsa -b 4096 -C "$KEY_OWNER" -f "$KEY_NAME" -P ""
cat "$KEY_NAME.pub" >> ~/.ssh/authorized_keys
echo "Very Important!"
echo "The next screen that will come up will display the key needed for login."
echo "You must copy this in it's entirety in order to login using what was setup in the previous commands."
echo ""
echo "Press enter to continue..."
read
clear
echo ""
echo "##################"
echo "## Private Key: ##"
echo "##################"
echo ""
cat $KEY_NAME
echo ""
echo "###########################"
echo "## Authorized Key Entry: ##"
echo "###########################"
echo ""
cat $KEY_NAME_PUB
echo ""
rm -f "$KEY_NAME" "$KEY_NAME_PUB"

Automated Email Script in Bash

Rewrote my “Free Ice Cream” script in bash because I needed to be able to flexibly send emails without going through 20 UAC prompts due to restrictions and how many people are scared of PowerShell.

Below is my new implementation, instead of relying on Outlook (like before), this version is platform independent and will work on any OS with any Mail Client like Thunderbird or Gmail.

rawurlencode() {
  local string="${1}"
  local strlen=${#string}
  local encoded=""
  local pos c o
 
  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  echo "${encoded}"
}

function generateParticipants() {
	PARTICIPANT_LIST=("$@")
	
	PARTICIPANT_STRING_TO_RETURN=""

	SEPARATOR=";"

	for PARTICIPANT in "${PARTICIPANT_LIST[@]}";
	do
		PARTICIPANT_STRING_TO_RETURN+="$PARTICIPANT"
		PARTICIPANT_STRING_TO_RETURN+="$SEPARATOR"
	done

	echo "$PARTICIPANT_STRING_TO_RETURN"
}
 
function createEmail() {
	SUBJECT="$1" # Save first argument in a variable
	shift
	BODY="$1" # Save second argument in a variable
	shift # Shift all arguments to the left (original $1 gets lost)
	TO_ARRAY=("$@")

	TO=$(generateParticipants "${TO_ARRAY[@]}")

	SUBJECT_ENCODED=$(rawurlencode "$SUBJECT")
	BODY_ENCODED=$(rawurlencode "$BODY")

	MESSAGE_OBJECT="mailto:$TO&amp;subject=$SUBJECT_ENCODED&amp;body=$BODY_ENCODED"
	
	echo "########################"
	echo "#### EMAIL PREVIEW: ####"
	echo "########################"
	echo ""
	echo "To: $TO"
	echo "Subject: $SUBJECT"
	echo "Body: $BODY"
	echo ""
	echo "Sending to Email Client..."
	echo ""
	echo start "" "$MESSAGE_OBJECT"
	start "" "$MESSAGE_OBJECT"
}
 
EMAIL_SUBJECT="Free Ice Cream!"
EMAIL_BODY="I'm Buying."

PARTICIPANT_LIST+=("person1@company.com")
PARTICIPANT_LIST+=("person2@company.com")
PARTICIPANT_LIST+=("person3@company.com")

createEmail "$EMAIL_SUBJECT" "$EMAIL_BODY" "${PARTICIPANT_LIST[@]}"

Simple Selenium – For Faster Test Development

Wanted to share a pretty fun “Plain Old Java App” I wrote today. It’s an extremely helpful implementation of Selenium for those who are just starting out and want a base to build on and expand their knowledge.

I originally wrote this implementation in Python but encountered that more people expect the usage of a traditional programming language.

Below is the README.md from my Github:

# Simple Selenium
An implementation of Selenium that extrapolates some of the more complex features of Selenium Development for development ease.

---

**Problem:**

- Selenium can be overly complicated sometimes
- Selenium does not build in functions like scrolling the screen or simple xpath searches 

**Solution:**
- By extrapolating some of the more complicated functionality up a layer we accomplish:
   - Faster Test Case Development Time
   - More readable code

---

# Instructions

**Step 1:**
Clone

**Step 2:**
Ensure your Chrome driver version matches the version of chrome you currently have installed, if not acquire from official Chrome Selenium Site

**Step 3:**
Run "build.sh" file in Git-Bash or Terminal

**Step 4:**
Run "run.sh" file in Git-Bash or Terminal

**Step 5:**
Dive into the code in either Eclipse or your favorite IDE for further learning and customization to your specific needs

---

## Authors

* **Cody Garrett** - [cody@garrett.ms](mailto:cody@garrett.ms)