• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

bash

Bash Spacebar Break

August 16, 2022

Here’s how to add breaks in your Bash scripts. The function is called spacebar.

function spacebar {
  echo 'Press spacebar to continue. Press Ctrl-C to exit.'
  stty -echo > /dev/null
  until read -r -n 1 -t 0.001 && [ "$REPLY" = ' ' ]; do abc=''; done
  stty echo > /dev/null
}
 
spacebar

function spacebar { echo 'Press spacebar to continue. Press Ctrl-C to exit.' stty -echo > /dev/null until read -r -n 1 -t 0.001 && [ "$REPLY" = ' ' ]; do abc=''; done stty echo > /dev/null } spacebar

You can call it multiple times in your script since it’s a function.

Filed Under: Linux Tagged With: bash, continue, hit, spacebar

Press spacebar to continue

August 5, 2022

Here’s a little snippet of Bash code that waits for spacebar to be pressed to continue.

function spacebar {
  echo 'Press spacebar to continue. Press Ctrl-C to exit.'
  stty -echo > /dev/null
  until read -r -n 1 -t 0.001 && [ "$REPLY" = ' ' ]; do abc=''; done
  stty echo > /dev/null
}
echo 'Starting upgrade'
spacebar

function spacebar { echo 'Press spacebar to continue. Press Ctrl-C to exit.' stty -echo > /dev/null until read -r -n 1 -t 0.001 && [ "$REPLY" = ' ' ]; do abc=''; done stty echo > /dev/null } echo 'Starting upgrade' spacebar

Output

Starting upgrade
Press spacebar to continue. Or Ctrl-C to exit.

Starting upgrade Press spacebar to continue. Or Ctrl-C to exit.

If you press spacebar it will continue. Ctrl-C exits the program.

Filed Under: Linux Tagged With: bash, continue, spacebar, wait

Bash Reformat Numbers

February 27, 2022

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

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

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

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

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

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

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Interim pages omitted …
  • Go to page 6
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2023