bash menu and functions

How to integrate menus and functions in your Bash script. #!/bin/bash function show_logs { ls -l /var/log/ } function show_users { cat /etc/passwd } echo '--------' echo 'My Menu' echo '--------' PS3='Choose an option: ' commands=("Show logs" "Show users" "Quit") COLUMNS=0 select cmd in "${commands[@]}"; do case $cmd in "Show logs") echo "Showing logs ... " show_logs ;; "Show users") echo "Showing users ... " show_users ;; "Quit") echo "Exiting ... " exit ;; *) echo "invalid option $REPLY";; esac done Result ...

September 22, 2022 · 1 min · ulysses

saving and restoring iptables

iptables can be saved or loaded from a file. Here’s a way to do it. Saving to a file # Debian/Ubuntu iptables-save > /etc/iptables/rules.v4 # Redhat/Centos/Rocky iptables-save > /etc/sysconfig/iptables Loading from a file # Debian/Ubuntu iptables-restore > /etc/iptables/rules.v4 # Redhat/Centos/Rocky iptables-restore > /etc/sysconfig/iptables Listing rules iptables -L

September 22, 2022 · 1 min · ulysses

gcp list of bms servers

Here’s how to list Bare Metal Servers in Google Cloud Platform via gcloud. gcloud bms instances list --region REGION --project PROJECT Result NAME ID PROJECT REGION MACHINE_TYPE CLIENT_IPS PRIVATE_IPS STATE server-001 at-xxxxxxx-svr001 project-01 us-central1 o2-ultramem-896-metal 10.0.0.1 192.168.253.1,192.168.252.1 RUNNING server-002 at-xxxxxxx-svr002 project-02 us-central1 o2-ultramem-896-metal 10.0.0.2 192.168.253.2,192.168.252.2 RUNNING Obviously you can see the same from GCP’s Console.

September 15, 2022 · 1 min · ulysses

running top by process

Here’s how to isolate a process via the top command. top -c -p $(pgrep -f process_name | head -20 | tr "\\n" "," | sed 's/,$//')

September 11, 2022 · 1 min · ulysses

hide all windows except current

Less clutter is good. There are times when there’s too many windows in our desktop. Having less clutter leads to better focus. Here’s how to hide all Windows on a Mac OS except for the current or active one. Command + Option + h I wished there was a command to bring them all back, but I have not found one yet.

September 10, 2022 · 1 min · ulysses

history without line numbers

Here’s how to display history without the line numbers. history -w /dev/stdout Result cd /etc/httpd/conf view httpd.conf vim wiki.conf vim ssl.conf

September 8, 2022 · 1 min · ulysses

cloudwatch notifications

How to give someone access to enable/disable CloudWatch notifications. { "Version": "2012-10-17", "Statement": [ { "Action": [ "cloudwatch:DescribeAlarms", "cloudwatch:EnableAlarmActions", "cloudwatch:DisableAlarmActions" ], "Effect": "Allow", "Resource": "*" } ] }

September 7, 2022 · 1 min · ulysses

df command takes too long

Here’s a workaround if the df command takes too long to run. This limits listing to local file systems only. df -l

September 7, 2022 · 1 min · ulysses

gcp role policy binding

How to display the policy binding. gcloud compute instances get-iam-policy SERVER --project=PROJECT_ID --zone=ZONE Result # There is no binding policy etag: ACAB # There is a binding policy bindings: - members: - serviceAccount:SERVICEACCOUNT role: organizations/xxxxxxxxxxxx/roles/ROLE etag: xxxxxxxxxxx= version: 1 Add a role binding policy gcloud compute instances add-iam-policy-binding SERVER \ --project=PROJECT_ID \ --zone=ZONE \ --member=serviceAccount:SERVICEACCOUNT \ --role="organizations/xxxxxxxxxxxx/roles/ROLE" Remove a role binding policy gcloud compute instances remove-iam-policy-binding SERVER \ --project=PROJECT_ID \ --zone=ZONE \ --member=serviceAccount:SERVICEACCOUNT \ --role="organizations/xxxxxxxxxxxx/roles/ROLE"

September 6, 2022 · 1 min · ulysses

jekyll on docker container

Here’s how to run Jekyll in a Docker container. mkdir blog cd blog docker run -v $(pwd):/site bretfisher/jekyll new . Start jekyll. docker run -p 4000:4000 -v $(pwd):/site bretfisher/jekyll-serve Another option is to use docker compose. Just create a docker-compose.yml file. version: 3.5 services: jekyll: image: bretfisher/jekyll-serve volumes: - .:/site ports: - '4000:4000' Start Jekyll by using docker-compose up. -d means detach or run in the background. cd blog docker-compose up -d Stop Jekyll. ...

August 31, 2022 · 1 min · ulysses