Bash learnings

Hierarchy:

Categories

Tags:

Published:

Updated June 13, 2022

things I always need to look up.

Snippets

If/then/else

if  ! $(which jq)
then
    echo "Looks like you don't have jq installed. Installing jq..."
    brew install jq
    echo "...jq installed!"
else 
    echo "You've already installed jq. Cool!"
fi

For loop

for site in  1202859 1202862 1201051 1207663 1228567 1213504 1224728 1229567 1239206
do
    echo "iteration for $site"
done
  • When using a $variable, you have to use “, not ‘ !
  • Variable assignment from the results of another command: name=$(command)
  • Array assignment from the results of another command: `array_name=($(command))

curl

  • -o for saving to a file

jq

jq – command line JSON processor

  • use -r to remove quotes around the data. raw.
  • Use | @sh for transforming arrays into space separated strings
# Select all children of the data object and extract the value of the id keys
| jq '.data[].id'

# Select the first element of the data object and return the value of the filesystemBackupId key, raw/no quotes
| jq -r '.data[0].filesystemBackupId'

tr

Transformations! Replacements, changing case, https://linuxhint.com/bash_tr_command/

Use | tr '\n' ' ') to replace new lines with spaces.

nohup

nohup (No Hang Up) is a command in Linux systems that runs the process even after logging out from the shell/terminal. 

nohup command [command-argument ...]

wget

wget can download recursively!

wget -r -1 0 https://site.com/….

Set the maximum number of subdirectories that Wget will recurse into to depth. In order to prevent one from accidentally downloading very large websites when using recursion this is limited to a depth of 5 by default, i.e., it will traverse at most 5 directories deep starting from the provided URL. Set ‘-l 0’ or ‘-l inf’ for infinite recursion depth.

https://www.gnu.org/software/wget/manual/wget.html#Recursive-Retrieval-Options

Related Posts