Skip to content

Instantly share code, notes, and snippets.

@UnderGrounder96
Last active July 22, 2023 17:50
Show Gist options
  • Save UnderGrounder96/140f4484e5b42ea13d39da3d69a387ab to your computer and use it in GitHub Desktop.
Save UnderGrounder96/140f4484e5b42ea13d39da3d69a387ab to your computer and use it in GitHub Desktop.
Topics of my Linux, Bash, Git, Jenkins, Virtualisation and Docker
Use case:
http://gitlab.com/UnderGrounder96/awx_install.git
Reference Bash:
https://tutorialkart.com/bash-shell-scripting/bash-tutorial/
shebang: #!/bin/bash
1 - Variables
SYSTEM VARIABLES vs USER VARIABLES
env command
$PWD, $USER, $HOME, $PS1
.env - https://github.com/UnderGrounder96/RentAcar/blob/master/.env.default
echo -e "$USER\n FooBar" # -e escapes newline
USER_VAR="HELLO" # defines USER_VAR as "hello"
USER_COM_VAR=`echo 'x'` # defines USER_VAR as "x", from command echo
USER_COM_VAR=$(echo 'x') # same as above
# this is a comment
echo $1, $2... ${10}
echo $@ # print args arr
echo $? # last result
echo $# # length
count=10 # -le, -lt, -eq, -ge, -gt
if [ $count -le 4 ]; then
echo "$count is less or equal to 4"
elif [[ $count -gt 4 && $count -lt 10 ]]; then # &&, ||
echo "$count is bewteen 5 and 10"
else
echo "$count has to be >=10"
fi
# if condition is false
if [ "hello" == "bye" ]; then # !=
echo "hello equals bye"
fi
if [ -f /file/path ]; then
echo "/file/path is a file"
fi
if [ -d /dir/path ]; then
echo "/dir/path is a dir"
fi
if [ -z “$hello” ]; then
echo "$hello is not empty"
fi
if [ $count -le 4 ]; then
echo "count is less or equal to 4 : $count"
elif [ $count -ge 5 ] && [ $count -lt 9 ]; then # (( $count >= 5 && $count <= 9 ))
echo "count is between to 5 and 9 : $count"
else
echo "count is greater than 9 : $count"
fi
case $variable in
A)
echo "A selected"
;;
B)
echo "B selected"
;;
C)
echo "C selected"
;;
*)
echo "ERROR! Please select between 1..3"
esac
#loops
while true; do
echo "hello" && break
done
a=1
b=2
count=10
while [[ $a -lt $count && $b -lt 5 ]]; do
a=$(( a + 1 ))
b=$(( b + 1 ))
done
# for (( i=0; i<5; i++ ))
for i in {0..5..2}; do #start..end..increment
echo "$i"
done
function main(){
# more code here
:
}
#!/usr/env bash
# great website - https://devhints.io/bash
VM_ADDRESS=$(ip a | grep 192.168 | awk '{print $2}') # gets IP/mask
VM_ADDRESS=${VM_ADDRESS%\/*} # removes everything after /
VERSION=$(curl -ks https://releases.hashicorp.com/vagrant/ | grep -Eom1 '_[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}') #
VERSION=${VERSION/_/} # slices "_"
echo $RANDOM
echo $(( (RANDOM % 10 ) + 1)) # echoes random number from 1 to 9 (10 is excluded)
> file # empty file
# search and destroy!
find . -iname '*.file_type' -print -delete
find . -name "node_modules" -type d -print | xargs rm -rf
ip a # shows ip
ifconfig -a
# shows open ports
sudo lsof -i -P -n | grep LISTEN
netstat -lntp
# reset firewalld
sudo rm -rf /etc/firewalld/zones/
iptables -Z; iptables -F; iptables -X
systemctl restart firewalld
# reads and executes each line as commands
cat file | while read line || [[ -n $line ]]; do command $line; done
# When RedHat screws up their repo infrastructure
while true; do
sudo dnf install dnf-utils
if [ $? -eq 0 ]; then echo "Yeah boiii"; break; fi
sleep 1m;
done
for file in *.webarchive; do
mv -- "$file" "${file%%.webarchive}" # removes a particular extension from files
done
find -type d -empty -print -delete -o -type f -empty -print -delete # deletes empty files/dirs
curl -sL https://raw.githubusercontent.com/thedevsaddam/docgen/v3/install.sh | sudo bash -s # execute remote script
Reference Docker:
https://docs.docker.com/
https://www.tutorialspoint.com/docker/index.htm
Docker ps # shows running containers
Docker ps -a # shows all containers
Docker pull # pulls image from dockerhub.com
docker run hello-world # executes (and pulls) image hello-world
Docker start [container] # starts (up) the container
Docker stop [container] # stops running container
Docker logs [container] # showcases logs of container
Docker exec -it container_name bash # enters inside the container to perform bash commands
Docker rm container # removes (stopped) container
Docker rmi image # removes image
Reference Git:
https://www.tutorialspoint.com/git/index.htm
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
ssh-keygen && cat ~/.ssh/*.pub # generate SSH key and prints public key
git config --list
git config --global user.name "Lucio Afonso"
git config --global user.email "lucioafonso@icloud.com"
git config --global core.eol lf
git config --global color.ui "true"
git config --global core.editor "vim"
git config --global core.autocrlf input
git config --global credential.helper "store"
git clone https://github.com/git/git # clones git
git checkout -b LA/branch_name # creates and switches to branch LA/branch_name
git pull origin master # fetches and merges current branch with master
git add -A # stages (adds) files to git
git commit -m "F" # commits with the message "F"
git init # Initializing a repository
#Branching
git branch # shows local branches
git branch -a # shows local and remote branches
git branch -D la/extras # deletes branch la/extras
# Switching branches
git checkout branch_name # switches to branch_name
# Showcases status
git status
# Staging files
git add file1.js # Stages a single file
git add file1.js file2.js # Stages multiple files
git add -A # all files, new and modified
git add -u # all modified and staged files, not new files
# Committing the staged files
git commit -m "Message" # Commits with a one-line message
git commit # Opens the default editor to type a long message
# Adds remote origin as http://link/to/repo
git remote add origin http://link/to/repo
# Pushes branch to origin
git push origin branch
git push -d origin LA/branch_name # deletes LA/branch_name from origin
# Fetch vs pull
git fetch # updates all branches
git pull # same as above but merges
# Removes changes
git checkout -- file
git restore file
# Viewing the history
git log # Full history
git log -n 4 # Showcases last 4 from history
git stash # stashes all current changes
git reset --hard origin/master # resets HARD with master (aka sync)
git stash pop # applies last stashed changes
Reference Unix:
https://tutorialspoint.com/unix/index.htm
1 - Introduction:
Win - versions, File structure
Unix, AppleOS, Linux dist (Debian, Ubuntu, RedHat)
2 - Linux file structure
#1: Everything is a file
/bin, /boot...
/editable_text_config - extra configuration for commands
/media - auto mounted devices
/mnt - manual mounted devices
/opt - optional software, Dropbox is installed here.
/run - tmp folder used during boot
/var - variables files, /var/log is where log files are stored
3 - Full path vs relative path
/; ~ ; .. ; . # root filesystem
# hidden folder and files
.env/ # hidden folder
~/.bashrc # relative path of .bashrc
4 - Commands
# Intro: command [-flags] [args]
whoami, man vs --help, which, uname -a, yes
5 - File/Dir commands
pwd, cd, ls -lh, mkdir -p, rmdir -p
touch, cat, head -n 6, tail -n 3, more, less,
vi/nano, grep -nir, find / -name, cp, mv
clear, ctrl + L
rm -rvf dir
6 - Wildcards and Regular expression - *
Sdhasd\ dssd = "Sdhasd dssd" # they are equal
"Ds / ?sad"
rm -vf dir/* # erases everything from dir/
find . -name "*.txt"
7 - Multiple commands: Piping, and or grep
mkdir -p myFolder/dir && mv myFolder/ /tmp/
cd /tmp/myFolder/dir; hjkkjg; echo "No problem"
cat file.txt | grep tellus | less
8 - Redirects
cat /dev/null > file.txt # eliminates the content of a file
echo "Hasdad asd ad a" >> file.txt # appends what's in echo inside the file
find / -name dev_tools_default.img 2>/dev/null # hides errors
cat > /tmp/myFile.txt # type then ctrl+d, creates file
cat < /tmp/myFile.txt > /tmp/myFile2.txt # copies file
sdadas >file.log 2>&1 # redirects STDERR to STDOUT
bash script.sh 1>file.log 2>file.err # runs script.sh and creates 2 files, one for log and other for err
8- sudo/ su -/ sudo -i
chmod
systemctl
shutdown
reboot
apt install
9 - ping google.com -c 4 # pings 4 times
Reference Jenkins:
https://www.tutorialspoint.com/jenkins/index.htm
Reference Virtualisation:
https://www.tutorialspoint.com/virtualization2.0/index.htm
Reference Vagrant:
https://www.vagrantup.com/docs
Vagrant up
Vagrant ssh
Vagrant halt
Vagrant destroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment