Skip to content

Instantly share code, notes, and snippets.

@marshki
Last active October 24, 2020 15:35
Show Gist options
  • Select an option

  • Save marshki/b6c1d46dbd9d33311310eaf4b1160198 to your computer and use it in GitHub Desktop.

Select an option

Save marshki/b6c1d46dbd9d33311310eaf4b1160198 to your computer and use it in GitHub Desktop.
Create (small) dummy data in Bash.
#!/usr/bin/env bash
#
# generate_stuff
#
# Create dummy data. Used as a sandbox for testing:
# https://gist.github.com/marshki/693ad8682cd14cc11f8ff4babeb47ca7
#
# Author: M. Krinitz <mjk235 [at] nyu [dot] edu>
# Date: 2020.03.01
# License: MIT
#
#===============================================================
# Step 1: Create files/dirs in: /tmp/sigma.
# Step 2: Create random output to nth item in: /tmp/sigma/alpha.
# Step 3: Duplicate nth files/dirs to: /tmp/sigma/bravo.
#===============================================================
SANDBOX="/tmp/sigma"
# Check if '/tmp/sigma' directory exists. Exit if yes.
check_for_directory () {
if [ -d "$SANDBOX" ]; then
printf "%s\\n" "Directory $SANDBOX already exists. Exiting."
exit 0
fi
}
# Create /tmp/sigma dir with subdirs.
make_directory () {
printf "%s\\n" "Creating 'sigma' dir & subdirs in /tmp..."
mkdir -p $SANDBOX/{alpha,bravo}/{charlie,delta,echo}
}
# Populate dirs in /tmp/sigma/alpha with files.
make_files () {
printf "%s\\n" "Creating files in /tmp/sigma/alpha..."
find $SANDBOX/alpha -type d -exec touch {}/foxtrot{0..9} \;
}
# Generate 1024 KBs of random data to nth file.
random_data_nth_files () {
for item in $(find "$SANDBOX"/alpha -type f |awk 'NR % 2 == 0'); do
printf "%s\\n" "Writing random data to $item."
head -c 1024 </dev/urandom > "$item"
done
}
# Duplicate every nth file or dir in /tmp/sigma/alpha to /tmp/sigma/bravo/.
copy_nth_files_dirs () {
for item in $(find $SANDBOX/alpha -type f -or -type d |awk 'NR % 3 == 0'); do
printf "%s\\n" "Copying $item to $SANDBOX/bravo."
cp -rp "$item" $SANDBOX/bravo/
done
}
# Exit status check.
exit_status () {
if [[ $retVal -ne 0 ]]; then
printf "%s\\n" "Something went wrong, homie..."
else
printf "%s\\n" "Done."
fi
}
# Wrapper.
main () {
check_for_directory
make_directory
make_files
random_data_nth_files
copy_nth_files_dirs
}
main "$@"
retVal=$?
exit_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment