- Arrays
- Command Outputs
- Files
- For Loop
- Functions
- If Statements
- Importing functions
- Replace a String
- Repeating Commands
- Prompt
- Sleep
node1_ip="172.25.0.102"
node2_ip="172.25.0.103"
node3_ip="172.25.0.104"
ips=($node1_ip, $node2_ip, $node3_ip)
ips=($node1_ip, $node2_ip, $node3_ip)
for ip in ${ips[*]}; do echo $ip; done
When you nee to mute the output of a command.
command > /dev/null
: redirects the output of stoud to null2>&1
: redirectsstderr
tostdout
source venv/bin/activate > /dev/null 2>&1
echo "something" >> some-file.txt
Can be run on the command line and in a bash script.
for i in {1..3}; do <some-command>; done
function some_func() {...}
function some_func() {
echo "Hello, World!"
}
if [ some-statement ]; then
do-stuff
fi
if [ -d "some-folder" ]; then
do-stuff
fi
Folder doesn't exist:
if [ ! -d "some-folder" ]; then
do-stuff
fi
if ! grep -q $1 docker-compose.yaml; then
if [ -z $var]; then echo "var is empty" fi
Imports functions from another bash script.
. ./common.sh --source-only
Mac OSX:
sed -i "" "s/<old_string>/<replacement_string/<global>" <file_name>
sed -i "" "s/container_name: node/container_name: $1/g" docker-compose.yaml
Ubuntu:
sed -i "s/container_name: node/container_name: $1/g" docker-compose.yaml
More of a recipe than anything, but fi you are working on something and need to repeatedly use the same command.
Create an alias on the command line so it only lasts for the session.
alias r="python run-me.py"
some-command reset <<< "y"
or
echo "something" | ./some_script.sh
sleep 1