marc:linux:bash_snippets
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| marc:linux:bash_snippets [2022/05/17 16:52] – marcv | marc:linux:bash_snippets [2023/02/16 14:23] (current) – [Download files based on URLs in a file] marcv | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Some useful bash snippets: ====== | ====== Some useful bash snippets: ====== | ||
| + | |||
| + | :!: If you declare variables, use lower case names! | ||
| + | |||
| + | ===== Using math ===== | ||
| + | < | ||
| + | expr 30 + 10 # 30 + 10 | ||
| + | expr 30 - 10 # 30 - 10 | ||
| + | expr 30 / 10 # 30 / 10 | ||
| + | expr 30 \* 4 # 30 x 4, needs escape! | ||
| + | </ | ||
| + | |||
| ===== Checking if file exists: ===== | ===== Checking if file exists: ===== | ||
| Line 11: | Line 22: | ||
| </ | </ | ||
| - | === Ask for variable input: === | + | ===== Checking if directory exists: ===== |
| + | < | ||
| + | if [[ -d ~/.ssh/ ]] | ||
| + | then | ||
| + | echo " | ||
| + | else | ||
| + | echo " | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | ===== Checking if variable equals 200 ===== | ||
| + | < | ||
| + | num = 200 | ||
| + | if [[ $num -eq 200 ]] | ||
| + | then | ||
| + | echo "$num equals 200" | ||
| + | else | ||
| + | echo "$num does not equal 200" | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | ===== Ask for variable input: | ||
| < | < | ||
| # Read Password | # Read Password | ||
| Line 21: | Line 53: | ||
| </ | </ | ||
| - | === Ecnrypting / Decrypting file: === | + | ===== Ecnrypting / Decrypting file: ===== |
| A useful tool for encrypting/ | A useful tool for encrypting/ | ||
| Line 32: | Line 64: | ||
| </ | </ | ||
| - | === Install a list of programs: === | + | ===== Install a list of programs: |
| < | < | ||
| progs=( | progs=( | ||
| Line 50: | Line 82: | ||
| </ | </ | ||
| - | === A simple menu (ssh): === | + | ===== A simple menu (ssh): |
| < | < | ||
| Line 110: | Line 142: | ||
| clear | clear | ||
| exit</ | exit</ | ||
| + | |||
| + | ===== Conditional while loop: ===== | ||
| + | |||
| + | < | ||
| + | if [ ! -f / | ||
| + | then | ||
| + | while true; do | ||
| + | read -p "Would you like to install ' | ||
| + | case $yn in | ||
| + | [Yy]* ) echo "OK, installing zsh..."; | ||
| + | while read program; do | ||
| + | sudo apt install -y " | ||
| + | done <zsh | ||
| + | break;; | ||
| + | [Nn]* ) echo "ZSH will not be installed"; | ||
| + | * ) echo " | ||
| + | esac | ||
| + | done | ||
| + | fi</ | ||
| + | |||
| + | |||
| + | ===== Replace all files in a directory with empty files with the same name ===== | ||
| + | < | ||
| + | #!/bin/bash | ||
| + | |||
| + | # Get the directory path | ||
| + | echo "Enter the path to the directory: " | ||
| + | read dir_path | ||
| + | |||
| + | # Check if the directory exists | ||
| + | if [ ! -d " | ||
| + | echo " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | # Replace all files in the directory with empty files | ||
| + | for file in " | ||
| + | if [ -f " | ||
| + | > " | ||
| + | fi | ||
| + | done | ||
| + | |||
| + | echo "All files in the directory have been replaced with empty files." | ||
| + | </ | ||
| + | |||
| + | ===== Find SSH logins ===== | ||
| + | |||
| + | :!: This script assumes that you have the sshpass package installed on your system. You can install it with the following command: sudo apt-get install sshpass (for Ubuntu) or brew install http:// | ||
| + | |||
| + | < | ||
| + | #!/bin/bash | ||
| + | |||
| + | servers_file=" | ||
| + | users_file=" | ||
| + | passwords_file=" | ||
| + | logins_file=" | ||
| + | |||
| + | # Check if the input files exist | ||
| + | if [ ! -f " | ||
| + | echo " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | # Read the servers, users, and passwords into arrays | ||
| + | servers=($(cat " | ||
| + | users=($(cat " | ||
| + | passwords=($(cat " | ||
| + | |||
| + | # Loop through all combinations of servers, users, and passwords | ||
| + | for server in " | ||
| + | for user in " | ||
| + | for password in " | ||
| + | # Try to ssh into the server with the current combination of user and password | ||
| + | ssh_output=$(sshpass -p " | ||
| + | |||
| + | # Check if the ssh login was successful | ||
| + | if [ " | ||
| + | echo " | ||
| + | echo " | ||
| + | else | ||
| + | echo " | ||
| + | fi | ||
| + | done | ||
| + | done | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | ===== Remove duplicate and empty lines from file ===== | ||
| + | < | ||
| + | #!/bin/bash | ||
| + | |||
| + | # Remove duplicate lines from url.txt and save to temp file | ||
| + | sort -u url.txt > temp.txt | ||
| + | |||
| + | # Remove empty lines from temp file and save back to url.txt | ||
| + | grep -v " | ||
| + | |||
| + | # Remove temp file | ||
| + | rm temp.txt | ||
| + | </ | ||
| + | |||
| + | ===== Download files based on URLs in a file ===== | ||
| + | < | ||
| + | #!/bin/bash | ||
| + | |||
| + | # read list of URLs from file | ||
| + | while read url; do | ||
| + | # download file using wget | ||
| + | wget " | ||
| + | done < url.txt | ||
| + | </ | ||
| + | |||
| + | Do the same but check if file exists. If it does compare sizes and ask what to do. | ||
| + | |||
| + | < | ||
| + | |||
| + | # Read URLs from the url.txt file and download the files | ||
| + | while read url; do | ||
| + | filename=$(basename " | ||
| + | if [ -e " | ||
| + | local_size=$(wc -c <" | ||
| + | remote_size=$(curl -sI " | ||
| + | if [ " | ||
| + | echo " | ||
| + | continue | ||
| + | else | ||
| + | read -p "A file with the name $filename already exists. Do you want to save the new file with a different name? (y/n) " response | ||
| + | if [ " | ||
| + | read -p "Enter the new file name: " new_filename | ||
| + | curl -L -o " | ||
| + | echo "File saved as $new_filename." | ||
| + | else | ||
| + | echo " | ||
| + | fi | ||
| + | fi | ||
| + | else | ||
| + | curl -L -o " | ||
| + | echo " | ||
| + | fi | ||
| + | done < url.txt | ||
| + | </ | ||
marc/linux/bash_snippets.1652799143.txt.gz · Last modified: (external edit)
