• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

bash

Docker Exec

April 9, 2021

If you want to SSH to a Docker container, use the Docker exec command.

docker exec -it container_name bash

docker exec -it container_name bash

I’m using a Git Bash terminal, so I have to prefix the docker command with winpty.

winpty docker exec -it container_name bash

winpty docker exec -it container_name bash

Result

root@681bbe3f65f1:/var/www/html#

root@681bbe3f65f1:/var/www/html#

Ctrl-D to exit.

Filed Under: Linux Tagged With: bash, container, docker, exec, git bash

Count String Characters

March 3, 2021

Here’s a simple little script that displays the number of characters in a string.

#!/bin/bash
if [[ $# -eq 0 ]] ; then echo 'Missing arguments'; exit 0; fi
var=$1
size=${#var}
echo $size

#!/bin/bash if [[ $# -eq 0 ]] ; then echo 'Missing arguments'; exit 0; fi var=$1 size=${#var} echo $size

Command:

./length.sh abcdefghijklmnopqrstuvwxyz

./length.sh abcdefghijklmnopqrstuvwxyz

Output:

26

26

Filed Under: Linux Tagged With: bash, characaters, number, string

Logging Commands

February 22, 2021

I decided to log my commands for good record keeping. Here’s my script.

#!/bin/bash
if [[ $# -eq 0 ]] ; then echo 'No arguments. Format: ./go.sh script.sh'; exit 0; fi
log=log.txt
echo '-------------------------------' >> $log
date >> $log
echo '---' >> $log
cat $1 >> $log
echo '---' >> $log
/bin/bash $1 2>&1 | tee -a $log

#!/bin/bash if [[ $# -eq 0 ]] ; then echo 'No arguments. Format: ./go.sh script.sh'; exit 0; fi log=log.txt echo '-------------------------------' >> $log date >> $log echo '---' >> $log cat $1 >> $log echo '---' >> $log /bin/bash $1 2>&1 | tee -a $log

To run.

./go.sh script.sh

./go.sh script.sh

Line by line explanation.

  1. Checks for script to run. Exits if there’s no argument.
  2. Sets the log file.
  3. Prints dashes.
  4. Prints the date.
  5. Print 3 dashes.
  6. Prints the commands to the log file.
  7. Print 3 dashes.
  8. Run the command. Send error and standard out to log file.

Filed Under: Linux Tagged With: bash, commands, log, tee

Reboot Instance Script

December 21, 2020

Here’s a new script to reboot a Lightsail instance based on input.

#!/bin/bash
echo 'Choose a server to reboot ...'
echo '1) server-one'
echo '2) server-two'
echo '3) server-three'
echo '4) server-four'
echo '5) sever-five'
echo 'q) Quit'
read -p 'Choose a server to reboot: ' server
case $server in 
	1)
		echo 'Rebooting server-one ...'
		aws lightsail reboot-instance --instance-name server-one
		echo 'Done'
		;;
	2)
	        echo 'Rebooting server-two ...'
		aws lightsail reboot-instance --instance-name server-two
		echo 'Done'
		;;
	3)
		echo 'Rebooting server-three ...'
		aws lightsail reboot-instance --instance-name server-three
		echo 'Done'
		;;
	4)
		echo 'Rebooting server-four ...'
		aws lightsail reboot-instance --instance-name server-four
		echo 'Done'
		;;
	5)
		echo 'Rebooting server-five ...'
		aws lightsail reboot-instance --instance-name server-five
		echo 'Done'
		;;
	q)
		echo 'Quit'
		;;
	*)
		echo 'Invalid option' $server
		;;
esac

#!/bin/bash echo 'Choose a server to reboot ...' echo '1) server-one' echo '2) server-two' echo '3) server-three' echo '4) server-four' echo '5) sever-five' echo 'q) Quit' read -p 'Choose a server to reboot: ' server case $server in 1) echo 'Rebooting server-one ...' aws lightsail reboot-instance --instance-name server-one echo 'Done' ;; 2) echo 'Rebooting server-two ...' aws lightsail reboot-instance --instance-name server-two echo 'Done' ;; 3) echo 'Rebooting server-three ...' aws lightsail reboot-instance --instance-name server-three echo 'Done' ;; 4) echo 'Rebooting server-four ...' aws lightsail reboot-instance --instance-name server-four echo 'Done' ;; 5) echo 'Rebooting server-five ...' aws lightsail reboot-instance --instance-name server-five echo 'Done' ;; q) echo 'Quit' ;; *) echo 'Invalid option' $server ;; esac

Assuming awscli is working and correct permission is granted to user.

Filed Under: Linux Tagged With: bash, instance, lightsail, reboot

Remove Quotes From Strings

July 26, 2020

Here’s the command to remove quotes from a string in Bash by using the tr -d command. I’m using jq tool to extract the access key from a json file. Bash returns a string surrounded by quotes. To remove the quotes, just pipe the string to the tr -d command.

jq .AccessKey.AccessKeyId key.json

jq .AccessKey.AccessKeyId key.json

Result:

"ASDFSDFSDFASDFSDFSDFGHGDF"

"ASDFSDFSDFASDFSDFSDFGHGDF"

Using tr -d.

jq .AccessKey.AccessKeyId key.json | tr -d \"

jq .AccessKey.AccessKeyId key.json | tr -d \"

Result:

ASDFSDFSDFASDFSDFSDFGHGDF

ASDFSDFSDFASDFSDFSDFGHGDF

Filed Under: Linux Tagged With: bash, jq, quotes, remove, tr, variable

Bash Variables

June 27, 2020

Here’s how to read Bash variables from another file.

Contents of config.sh

#!/bin/bash
var1='thisisvariableone'
var2='thisisvariabletwo'

#!/bin/bash var1='thisisvariableone' var2='thisisvariabletwo'

Call config.sh from another script.

#!/bin/bash
source config.sh
echo var1
echo var2

#!/bin/bash source config.sh echo var1 echo var2

This is extremely helpful if you have multiple scripts using a common variable. You just to update one file instead of multiple files. All the other scripts will read and pick up the same variables from a config file. You can use this to setup your global variables.

Filed Under: Linux Tagged With: another, bash, file, source, variable

GCE Agent Bash

May 11, 2020

This is a Bash script to restart a service.

#!/bin/bash
 
SERVICE=google_osconfig_agent
AGENT=google-osconfig-agent
 
if (( $(ps -ef | grep -v grep | grep $SERVICE | wc -l) > 0 ))
then
    echo "$AGENT service running, everything is fine"
else
    echo "$AGENT is not running"
    echo "Restarting service"
    systemctl start $AGENT
    sleep 5s
    if (( $(ps -ef | grep -v grep | grep $SERVICE | wc -l) > 0 ))
    then
      sleep 2s
      echo "$AGENT service is now running"
    fi
fi

#!/bin/bash SERVICE=google_osconfig_agent AGENT=google-osconfig-agent if (( $(ps -ef | grep -v grep | grep $SERVICE | wc -l) > 0 )) then echo "$AGENT service running, everything is fine" else echo "$AGENT is not running" echo "Restarting service" systemctl start $AGENT sleep 5s if (( $(ps -ef | grep -v grep | grep $SERVICE | wc -l) > 0 )) then sleep 2s echo "$AGENT service is now running" fi fi

Filed Under: Cloud, Linux Tagged With: agent, bash, gce, restart

Bash Convert Bytes

January 26, 2020

Here’s a function to convert bytes into kilo, mega, giga, tera up to zeta bytes.

function convert_bytes {
    bytes=$credits
    b=${bytes:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}B)
    while ((b > 1024)); do
        d="$(printf ".%02d" $((b % 1000 * 100 / 1000)))"
        b=$((b / 1000))
        let s++
    done
    echo "$b$d ${S[$s]}"
}
credits=2308974418330
echo $credits 
convert_bytes $credits

function convert_bytes { bytes=$credits b=${bytes:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}B) while ((b > 1024)); do d="$(printf ".%02d" $((b % 1000 * 100 / 1000)))" b=$((b / 1000)) let s++ done echo "$b$d ${S[$s]}" } credits=2308974418330 echo $credits convert_bytes $credits

Result is:

2308974418330
2.30 TB

2308974418330 2.30 TB

Filed Under: Linux Tagged With: bash, bytes, conversion, convert, function

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to page 5
  • Go to page 6
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2023