Random words from array

Here’s how to to generate random words from an array in Bash using shuf. FOOD=('Milk' 'Bread' 'Eggs' 'Sausage' 'Grits' 'Banana' 'Oatmeal') shuf -e ${FOOD[@]} Milk Grits Sausage Oatmeal Bread Banana Eggs You can limit output to 3 by piping output using head -n 3. FOOD=('Milk' 'Bread' 'Eggs' 'Sausage' 'Grits' 'Banana' 'Oatmeal') shuf -e ${FOOD[@]} | head -n 3 Bread Sausage Eggs

December 24, 2023 · 1 min · ulysses

Search replace lowercase

Here’s a script I use to edit over 1000 files to convert my post titles to all lowercase. #!/bin/bash cd ~/Code/docker/ulyme/_posts searchReplace() { for filename in 20*.md; do sed -i -e 's/title:.*/\L&/g' $filename done } searchReplace It took a mere 2-3 seconds to convert all titles on over 1000 files.

December 21, 2023 · 1 min · ulysses

gcp restore all disks

Here’s my latest script on how to restore all disks of a VM in GCP. #!/bin/bash # project="projectid" # instance="hostname" # zone="us-central1-c" # policy="hourly" # snapback=3 # Format (requires all 5 arguments): ./script.sh project host zone policy snapback # If no arguments are provided or less than 5 arguments, you will be prompted to enter your options. if [ -z "$project" ] || [ -z "$instance" ] || [ -z "$zone" ] || [ -z "$policy" ] || [ -z "$snapback" ]; then if [ "$#" -eq 5 ]; then project=$1 instance=$2 zone=$3 policy=$4 snapback=$5 else read -p "projectId : " project read -p "instance : " instance read -p "zone : " zone read -p "policy : " policy read -p "snapback : " snapback fi fi echo $project $instance $zone $policy $snapback checkLogin() { user=$(gcloud config list account --format "value(core.account)") user=$(echo $user | cut -d@ -f1) user=${user/./-} if [ -z "$user" ]; then echo "You're not logged in to Google SDK." exit fi } displayHeader() { echo "######################################################" echo "Starting the restore process for $instance " echo "######################################################" } getDisks() { disks=$(gcloud compute disks list \ --filter="users:$instance" \ --format="value(name)" \ --project="$project") echo "Listing disks for $instance ... " for disk in $disks; do echo $disk done } getSnaps() { echo "Listing snapshots to restore for $instance ... " for disk in $disks; do snap=$(gcloud compute snapshots list \ --filter="sourceSnapshotSchedulePolicy~$policy AND sourceDisk~$disk" \ --format="value(name)" \ --project="$project" \ --sort-by="~creationTimestamp" | head -n $snapback | tail -n 1) echo "$disk: $snap" done } stopServer() { echo "Stopping server ..." gcloud compute instances stop $instance \ --zone="$zone" \ --project="$project" } detachDisks() { echo "Detaching disks ..." for disk in $disks; do gcloud compute instances detach-disk $instance \ --disk="$disk" \ --zone="$zone" \ --project="$project" done } deleteDisks() { echo "Deleting disks ..." for disk in $disks; do gcloud compute disks delete $disk \ --zone="$zone" \ --project="$project" done } createDisks() { echo "Creating disks ..." for disk in $disks; do snap=$(gcloud compute snapshots list \ --filter="sourceDisk~$disk" \ --format="value(name)" \ --project="$project" \ --sort-by="~creationTimestamp" | head -n $snapback | tail -n 1) gcloud compute disks create $disk \ --source-snapshot="$snap" \ --zone="$zone" \ --project="$project" done } attachDisks() { echo "Attaching disks ..." count=1 for disk in $disks; do if [ $count == 1 ]; then gcloud compute instances attach-disk $instance \ --disk="$disk" \ --device-name="$disk" \ --zone="$zone" \ --project="$project" \ --boot else gcloud compute instances attach-disk $instance \ --disk="$disk" \ --device-name="$disk" \ --zone="$zone" \ --project="$project" fi count=$((count+1)) done } setAutodelete() { echo "Setting autodelete on disks ..." for disk in $disks; do gcloud compute instances set-disk-auto-delete $instance \ --auto-delete \ --disk="$disk" \ --zone="$zone" \ --project="$project" done } startServer() { echo "Starting server ..." gcloud compute instances start $instance \ --zone="$zone" \ --project="$project" } main() { checkLogin displayHeader getDisks getSnaps read -p "Do you want to proceed with restore? Are you sure? " -n 1 -r echo # (optional) move to a new line if [[ $REPLY =~ ^[Yy]$ ]] then stopServer detachDisks deleteDisks createDisks attachDisks setAutodelete startServer fi } main

December 20, 2023 · 3 min · ulysses

gcp restore boot disk

Here’s my latest script to restore the boot disk of a VM in GCP. #!/bin/bash # project="projectid" # instance="hostname" # zone="us-central1-c" # policy="hourly" # snapback=3 # Format (requires all 5 arguments): ./script.sh project host zone policy snapback # If no arguments are provided or if less than 5 arguments, then you will be prompted to enter options. if [ -z "$project" ] || [ -z "$instance" ] || [ -z "$zone" ] || [ -z "$policy" ] || [ -z "$snapback" ]; then if [ "$#" -eq 5 ]; then project=$1 instance=$2 zone=$3 policy=$4 snapback=$5 else read -p "projectId : " project read -p "instance : " instance read -p "zone : " zone read -p "policy : " policy read -p "snapback : " snapback fi fi echo $project $instance $zone $policy $snapback checkLogin() { user=$(gcloud config list account --format "value(core.account)") user=$(echo $user | cut -d@ -f1) user=${user/./-} if [ -z "$user" ]; then echo "You're not logged in to Google SDK." exit fi } displayHeader() { echo "######################################################" echo "Starting the restore process for $instance " echo "######################################################" } getDisks() { disks=$(gcloud compute instances describe $instance \ --project="$project" \ --zone="$zone" \ --format="get(disks[0].source)") echo "Listing disks for $instance ... " disk=$(echo ${disks##*/}) echo $disk } getSnaps() { echo "Listing snapshots to restore for $instance ... " snap=$(gcloud compute snapshots list \ --filter="sourceSnapshotSchedulePolicy~$policy AND sourceDisk~$disk" \ --format="value(name)" \ --project="$project" \ --sort-by="~creationTimestamp" | head -n $snapback | tail -n 1) echo "$disk: $snap" } stopServer() { echo "Stopping server ..." gcloud compute instances stop $instance \ --zone="$zone" \ --project="$project" } detachDisks() { echo "Detaching disks ..." gcloud compute instances detach-disk $instance \ --disk="$disk" \ --zone="$zone" \ --project="$project" } deleteDisks() { echo "Deleting disks ..." gcloud compute disks delete $disk \ --zone="$zone" \ --project="$project" } createDisks() { echo "Creating disks ..." snap=$(gcloud compute snapshots list \ --filter="sourceDisk~$disk" \ --format="value(name)" \ --project="$project" \ --sort-by="~creationTimestamp" | head -n $snapback | tail -n 1) gcloud compute disks create $disk \ --source-snapshot="$snap" \ --zone="$zone" \ --project="$project" } attachDisks() { echo "Attaching disks ..." gcloud compute instances attach-disk $instance \ --disk="$disk" \ --device-name="$disk" \ --zone="$zone" \ --project="$project" \ --boot } setAutodelete() { echo "Setting autodelete on disks ..." gcloud compute instances set-disk-auto-delete $instance \ --auto-delete \ --disk="$disk" \ --zone="$zone" \ --project="$project" } startServer() { echo "Starting server ..." gcloud compute instances start $instance \ --zone="$zone" \ --project="$project" } main() { checkLogin displayHeader getDisks getSnaps read -p "Do you want to proceed with restore? Are you sure? " -n 1 -r echo # (optional) move to a new line if [[ $REPLY =~ ^[Yy]$ ]] then stopServer detachDisks deleteDisks createDisks attachDisks setAutodelete startServer fi } main

December 20, 2023 · 3 min · ulysses

gcp vm list all snapshots

Here’s how to list all the snapshots for a particular VM in GCP. #!/bin/bash if [ "$#" -ne 3 ] then echo "Usage: ./gcp-list-all-snapshots.sh project server policy" exit fi # Set the name of the snapshot schedule policy and the disk name to filter by PROJECT=$1 # PROJECT="projectId" SERVER=$2 # DISK_NAME="hostname" POLICY=$3 # POLICY_NAME="hourly or daily" disks=$(gcloud compute disks list --project $PROJECT --filter="name~$SERVER" --format="value(name)") for disk in $disks; do # List all snapshots created by the snapshot schedule policy gcloud compute snapshots list \ --filter="sourceSnapshotSchedulePolicy ~ $POLICY AND sourceDisk~$disk$" \ --format="table(sourceDisk.basename(),name,creationTimestamp,sourceSnapshotSchedulePolicy.basename())" \ --sort-by "~creationTimestamp" \ --project="$PROJECT" done The script requires three arguments: projectId, hostname and policy.

December 19, 2023 · 1 min · ulysses

jekyll new post script

Here’s my bash script that creates a new Jekyll post template. Once created, use vi to edit the post. #!/bin/bash if [ "$#" -ne 1 ] then echo "Missing argument. Usage: ./jekyllNewPost.sh new-post-title" exit fi cd ~/Code/docker/ulyme/_posts/ ts=$(date +%Y-%m-%d) title=$1 filename=($ts-$title.md) pretitle=$(echo "${title^}") posttitle=$(echo $pretitle | tr "-" " ") cat << EOF > $filename --- layout: post author: ulysses title: '$posttitle' --- EOF echo "vi ~/Code/docker/ulyme/_posts/$filename"

December 19, 2023 · 1 min · ulysses

strip new lines on variables

I had problems echoing four variables into one line. There seem to be some carriage return or new line somewhere that’s not visible. So, here’s a line of code that will strip any new line or carriage return in the variable, and then use printf to display the variables on one line. Strip the new lines. efs="${efs//$'\n'/ }" name="${name//$'\n'/}" total="${total//$'\n'/ }" mesg="${mesg//$'\n'/}" Now print them using printf on one line. printf "%-30s %-40s %-20s %-30s\n" "$efs" "$new" "$total" "$mesg" With that code, I’m now able to format my report nicely without the extra new lines.

December 18, 2023 · 1 min · ulysses

multiple github accounts

I have two Github accounts. The first one was cloned using HTTPS. When it was time to add a second one, I really didn’t want to mess around with multiple Github accounts and dealing with multiple credentials. I’ve decided to just clone the second account using SSH. I decided to do this mainly to simplify setup and to avoid the headaches with setting up multiple accounts in the config. First repo - was cloned using HTTPS Second repo - was cloned using SSH The first repo which uses HTTPS requires a github key which will need to be generated on Github. The first time a repo is cloned, you’ll be prompted to supply the key. This key is kept in the ~/.git-credentials file on the local desktop. The second repo which uses SSH requires a ssh key to be generated locally on your desktop, which you will then need to upload to github to allow authentication. ...

December 17, 2023 · 1 min · ulysses

using internal links

Here’s how to use internal links within Jekyll. Use this format. {% raw %} [Some Link]({% post_url 2010-07-21-name-of-post %}) {% endraw %} I had to convert code to HTML characters to render properly.

December 17, 2023 · 1 min · ulysses

editing multiple files

Editing 1000+ files is not fun. Here’s a script that cycles through certain files in a directory and performs a search and replace. #!/bin/bash for filename in 20*.md; do sed -i -e 's/author: Ulysses/author: ulysses/g' $filename done In this particular script, it looks for files starting with 20 with an extension of .md. The script uses the sed command to perform search and replace.

December 16, 2023 · 1 min · ulysses