Nono.MA

JULY 20, 2023

If you're trying to run a Bash script and get a Permission Denied error, it's probably because you don't have the rights to execute it.

Let's check that's true.

# Get the current file permissions.
stat -f %A script.sh
# 644

With 644, the user owner can read and write but not execute.1

Set the permissions to 755 to fix the issue.

chmod 755 script.sh

  1. Chmod 644. CHMOD Calculator. 

FEBRUARY 13, 2023

Here's how to randomize a list of strings in bash.

On macOS, you can use Terminal or iTerm2.

The shuf command shuffles a list that is "piped" to it.

Shuffling the contents of a directory

An easy way to do that is to list a directory's contents with ls and then shuffle them.

ls ~/Desktop | shuf

Shuffling a list of strings

The easiest way to shuffle a set of strings is to define an array in bash and shuffle it with shuf.

WORDS=('Milk' 'Bread' 'Eggs'); shuf -e ${WORDS[@]}

You can use pbcopy to copy the shuffled list to your clipboard.

WORDS=('Milk' 'Bread' 'Eggs' ); shuf -e ${WORDS[@]} | pbcopy

Shuffling lines from a text file

Another way to randomize a list of strings from bash is to create a text file, in this case named words.txt, with a string value per line.

Bread
Milk
Chicken
Turkey
Eggs

You can create this file manually or from the command-line with the following command.

echo "Bread\nMilk\nChicken\nTurkey\nEggs" > words.txt

Then, we cat the contents of words.txt and shuffle order of the lines with shuf.

cat words.txt | shuf
# Eggs
# Milk
# Chicken
# Turkey
# Bread

Again, you can save the result to the clipboard with pbcopy.

cat words.txt | shuf | pbcopy

If you found this useful, let me know!

DECEMBER 28, 2022

Iterating through a list.

for i in 1 2 3
do
  echo $i
done
# 1
# 2
# 3

Iterating through a list generated with a sequence.

for i in $(seq 1 2 10)
do
  echo $i
done
# 1
# 3
# 5
# 7
# 9

seq 1 2 10 creates a list of numbers from 1 to 10 in steps of 2.

AUGUST 27, 2022

Here's how to print the current date and time with bash.

date +%y%m%d_%H%M%S
# 220715_124140

You can store the timestamp on a variable.

DATE_NOW=$(date '+%y%m%d_%H%M%S')

And make use of it later on your command for clarity.

echo "Today is $DATE_NOW."
# Today is 220715_124805.

Note that you can customize the format of your date or timestamp by adjusting the formatting template.

Here's how the command works.

date +{formatting_code}

And here's a full list of options.1

# Gives name of the weekday as Mon, Sun, Fri
date +%a

# Gives name of the weekday as Monday, Sunday, Friday
date +%A

# Gives name of the month as Jan, Feb, Mar
date +%b

# Gives name of the month as January, February, March
date +%B

# Displays day of the month as 05
date +%d

# Displays current date MM/DD/YY format as 11-01-21
date +%D

# Shows date in YYYY-MM-DD format as 2021-11-01
date +%F

# Shows hour in 24-hour format as 22
date +%H

# Shows hour in 12-hour format as 11
date +%I

# Displays the day of the year as 001–366
date +%j

# Displays the number of the month as 01–12
date +%m

# Displays minutes as 00-59
date +%M

# Displays seconds as 00-59
date +%S

# Displays in Nanoseconds
date +%N

# Displays time as HH:MM:SS in 24-hour format
date +%T

# Day of the week as 1-7; 1 is Monday, 6 is Saturday
date +%u

# Shows week number of the year as 00-53
date +%U

# Displays year YYYY as 2021
date +%Y

# Displays time zone
date +%Z

To learn more about what you can do with the date command, run man date to print the manual.

JUNE 9, 2022

"The following command runs an ubuntu container, attaches interactively to your local command-line session, and runs /bin/bash," reads the official Docker starter guide.

docker run -it ubuntu /bin/bash

Inspecting the Linux virtual machine

docker run -it ubuntu /bin/bash

# List files inside of the Docker container
root@642064598df6:/ ls
# bin   dev  home  lib32  libx32  mnt  proc  run   srv  tmp  var
# boot  etc  lib   lib64  media   opt  root  sbin  sys  usr

# Print the current directory
root@642064598df6:/ pwd
# /

# Exit the instance
root@642064598df6:/ exit
# exit

Behind the scenes

Here's a summary from Docker's docs.

When you run this command, the following happens (assuming you are using the default registry configuration):

  • If you don't have the ubuntu image locally, Docker pulls it from your configured registry (as if you had run docker pull ubuntu).
  • Docker creates a new container (as if you had run a docker container create command manually).
  • Docker allocates a read-write filesystem to the container as its final layer. This allows a running container to create or modify files and directories in its local filesystem.
  • Docker creates a network interface to connect the container to the default network, since you did not specify any networking options. This includes assigning an IP address to the container. By default, containers can connect to external networks using the host machine's network connection.
  • Docker starts the container and executes /bin/bash. Because the container is running interactively and attached to your terminal (due to the -i and -t flags), you can provide input using your keyboard while the output is logged to your terminal.

When you type exit to terminate the /bin/bash command, the container stops but is not removed. You can start it again or remove it.

Read the Docker overview guide.

Remove the container on exit

If you don't want your container to persist after you exit, you should use the --rm flag.

docker run -it --rm ubuntu /bin/bash

A sample use-case: TensorFlow

Here, you can see how you'd use a Docker container to run TensorFlow without having to install dependencies on your local machine.

SEPTEMBER 9, 2019

Today I've automated the backup of the configuration, database, and static files of all the websites I manage. Two hours and a half that will save me a lot of time in the coming future, and remove stress when weird things happen. The backup—of six websites in three different servers running Laravel—downloads a copy of the database, the .env files, and the static files (a zip with the contents of the public folder) of each site.

I'll probably open source these scripts in the near future.

One new thing I learned was creating bash functions, like this one.

# create a variable with current date, formatted as yymmdd_HHMMSS
DATE_NOW=$(date '+%y%m%d_%H%M%S') 

# function that zips something and removes it
zip_and_remove() { cd $1 && zip "$2.zip" $2 && rm $2 && cd .. }

# function that downloads a file via ssh then calls the previous one
download_zip_remove() { scp $1 $2/$3 && zip_and_remove $2 $3  }

# a function call
download_zip_remove root@192.168.1.2:/var/www/site.com/folio/.env $DESTINATION $(echo $DATE_NOW)_$SITENAME.env

Want to see older publications? Visit the archive.

Listen to Getting Simple .