• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

bash

Bash Reformat Numbers

February 27, 2022 by Ulysses

How to reformat numbers in Bash.

$ n=1023555
$ m=$(numfmt --to=si n)
$ echo $m
1.1M

$ n=1023555 $ m=$(numfmt --to=si n) $ echo $m 1.1M

Other options

--to=none     # no scaling. Output: 1023555
--to=si       # International System of Units (SI) standard. Output 1.1M
--to=iec      # International Electrotechnical Commission (IEC) standard. Output: 1.1Mi 
--to=iec-i    # International Electrotechnical Commission (IEC) standard. Output: 1.1M
--to=auto     # ‘auto’ can only be used with --from.

--to=none # no scaling. Output: 1023555 --to=si # International System of Units (SI) standard. Output 1.1M --to=iec # International Electrotechnical Commission (IEC) standard. Output: 1.1Mi --to=iec-i # International Electrotechnical Commission (IEC) standard. Output: 1.1M --to=auto # ‘auto’ can only be used with --from.

fmtnum

Filed Under: Linux Tagged With: bash, convert, fmtnum, format, numbers

Bash Script on Startup

December 23, 2021 by Ulysses

How to add bash scripts on startup.

update-rc.d

sudo cp /path/to/yourscript.sh /etc/init.d/yourscript.sh
sudo update-rc.d /etc/init.d/yourscript.sh defaults
chmod +x /etc/init.d/yourscript.sh

sudo cp /path/to/yourscript.sh /etc/init.d/yourscript.sh sudo update-rc.d /etc/init.d/yourscript.sh defaults chmod +x /etc/init.d/yourscript.sh

Root Crontab

sudo crontab -e
@reboot /path/to/yourscript.sh

sudo crontab -e @reboot /path/to/yourscript.sh

Filed Under: Linux Tagged With: bash, crontab, script, startup, update-rc.d

Bash input executes functions

December 1, 2021 by Ulysses

How to execute functions in Bash based on argument.

There are 2 functions: build and terminate.

#!/bin/bash
build () {
 echo 'Building services'
}
terminate () {
 echo 'Terminating services'
}
if [ $1 == 'build' ]; then build
elif [ $1 == 'terminate' ]; then terminate
else echo 'Valid options are "build" or "terminate"'
fi

#!/bin/bash build () { echo 'Building services' } terminate () { echo 'Terminating services' } if [ $1 == 'build' ]; then build elif [ $1 == 'terminate' ]; then terminate else echo 'Valid options are "build" or "terminate"' fi

Execute a function based on argument.

$ bash test.sh build
Building services
 
$ bash test.sh terminate
Terminating services
 
$ bash test.sh anyother
Valid options are "build" or "terminate"

$ bash test.sh build Building services $ bash test.sh terminate Terminating services $ bash test.sh anyother Valid options are "build" or "terminate"

Filed Under: Linux Tagged With: argument, bash, execute, function

Bash Read File

December 1, 2021 by Ulysses

How to read a file line by line in Bash.

#!/bin/bash
file=test.txt
while IFS= read -r line; do
  echo $line
done < $file

#!/bin/bash file=test.txt while IFS= read -r line; do echo $line done < $file

Contents of test.txt.

one
two
three

one two three

Results when running test.sh.

$ bash test.sh
one
two
three

$ bash test.sh one two three

To read multiple strings line by line.

#!/bin/bash
file=test.txt
while read -r a b; do
  echo $b
done < $file

#!/bin/bash file=test.txt while read -r a b; do echo $b done < $file

Modified contents of test.txt.

one 1
two 2
three 3

one 1 two 2 three 3

New results.

$ bash test.sh
1
2
3

$ bash test.sh 1 2 3

Filed Under: Linux Tagged With: bash, file, loop, output, read

Rocky Linux in a Container

November 7, 2021 by Ulysses

Here’s how to run Rocky Linux in a Docker container.

Download the image.

docker pull rockylinux/rockylinux

docker pull rockylinux/rockylinux

Create a Rocky Linux container called bullwinkle.

docker run -it --name bullwinkle -d rockylinux/rockylinux

docker run -it --name bullwinkle -d rockylinux/rockylinux

Login to the container using bash.

docker exec -it --user root bullwinkle /bin/bash

docker exec -it --user root bullwinkle /bin/bash

When done, stop and delete the container.

docker ps -a
docker stop container_ID
docker rm container_ID

docker ps -a docker stop container_ID docker rm container_ID

Filed Under: Linux Tagged With: bash, container, docker, linux, pull, rocky

Bash Variable Empty

August 6, 2021 by Ulysses

Here’s how to check in Bash if a variable is empty or unset.

if [ -z "${VAR}" ]; 
then
  echo 'do something'
else
  echo 'do another'
fi

if [ -z "${VAR}" ]; then echo 'do something' else echo 'do another' fi

One liner

if [ -z "${VAR}" ]; then echo 'do something'; else echo 'do another'; fi

if [ -z "${VAR}" ]; then echo 'do something'; else echo 'do another'; fi

The inverse

if [ ! -z "${VAR}" ]; 
then
  echo 'do something'
else
  echo 'do another'
fi

if [ ! -z "${VAR}" ]; then echo 'do something' else echo 'do another' fi

if [ ! -z "${VAR}" ]; then echo 'do something'; else echo 'do another'; fi

if [ ! -z "${VAR}" ]; then echo 'do something'; else echo 'do another'; fi

Filed Under: Linux Tagged With: bash, check, empty, unset, variable

Docker Exec

April 9, 2021 by Ulysses

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 by Ulysses

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

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2012–2022