Site Tools


marc:linux:bash_snippets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
marc:linux:bash_snippets [2021/09/30 08:41] – external edit 127.0.0.1marc: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: ======
  
-=== Checking if file exists: ===+:!: If you declare variables, use lower case names! 
 + 
 +===== Using math ===== 
 +<code> 
 +expr 30 + 10           # 30 + 10 
 +expr 30 - 10           # 30 - 10 
 +expr 30 / 10           # 30 / 10 
 +expr 30 \* 4           # 30 x 4, needs escape! 
 +</code> 
 + 
 + 
 +===== Checking if file exists: =====
 <code> <code>
 if [[ -f ~/.ssh/id_rsa ]] if [[ -f ~/.ssh/id_rsa ]]
Line 11: Line 22:
 </code> </code>
  
-=== Ask for variable input: ===+===== Checking if directory exists: ===== 
 +<code> 
 +if [[ -d ~/.ssh/ ]] 
 +then 
 +        echo "directory .ssh exists" 
 +else 
 +        echo "directory .ssh does not exist" 
 +fi 
 +</code> 
 + 
 +===== Checking if variable equals 200 ===== 
 +<code> 
 +num = 200 
 +if [[ $num -eq 200 ]] 
 +then 
 +        echo "$num equals 200" 
 +else 
 +        echo "$num does not equal 200" 
 +fi 
 +</code> 
 + 
 +===== Ask for variable input: =====
 <code> <code>
 # Read Password # Read Password
Line 21: Line 53:
 </code> </code>
  
-=== Ecnrypting / Decrypting file: ===+===== Ecnrypting / Decrypting file: =====
 A useful tool for encrypting/decrypting files is //ccrypt//. Using this you can make use of //git// containing things like SSH keys: A useful tool for encrypting/decrypting files is //ccrypt//. Using this you can make use of //git// containing things like SSH keys:
  
Line 32: Line 64:
 </code> </code>
  
-=== Install a list of programs: ===+===== Install a list of programs: =====
 <code> <code>
 progs=( progs=(
Line 50: Line 82:
 </code> </code>
  
 +===== A simple menu (ssh): =====
 +<code>
  
 +#!/bin/bash
 +clear
 +
 +echo """
 +
 +##################################################################################################
 +#                                                                                                #
 +#                               Welcome to sshmenu                                               #
 +#                                                                                                #
 +#                        Which server would you like to connect to?                              #
 +#                                                                                                #
 +#                           1) wekan (kanban)                                                    #
 +#                           2) wiki                                                              #
 +#                           3) nextcloud                                                         #
 +#                           4) dockervm1                                                         #
 +#                           5) dockertestvm                                                      #
 +#                                                                                                #
 +##################################################################################################
 +
 +input number: 
 +"""
 +read -s server
 +echo $server
 +sleep 1
 +if [ $server = "1" ]; then
 +    clear
 +    echo "Setting up connection to wekan server..."
 +    sleep 2
 +    ssh 192.168.1.118
 +elif [ $server = "2" ]; then
 +    clear
 +    echo "Setting up connection to wiki server..."
 +    sleep 2
 +    ssh 192.168.1.117
 +elif [ $server = "3" ]; then
 +    clear
 +    echo "Setting up connection to nextcloud server..."
 +    sleep 2
 +    ssh 192.168.1.119
 +elif [ $server = "4" ]; then
 +    clear
 +    echo "Setting up connection to dockervm1 server..."
 +    sleep 2
 +    ssh 192.168.1.116
 +elif [ $server = "5" ]; then
 +    clear
 +    echo "Setting up connection to dockertestvm server..."
 +    sleep 2
 +    ssh 192.168.1.120
 +else
 +    echo "Incorrect choice, programm will now terminate..."
 +    sleep 3
 +    clear
 +    exit
 +fi
 +clear
 +exit</code>
 +
 +===== Conditional while loop: =====
 +
 +<code>
 +if [ ! -f /usr/bin/zsh ]
 +then
 + while true; do
 + read -p "Would you like to install 'zsh'? (y/n) " yn
 + case $yn in
 + [Yy]* ) echo "OK, installing zsh...";
 + while read program; do
 + sudo apt install -y "$program"
 + done <zsh
 + break;;
 +        [Nn]* ) echo "ZSH will not be installed"; break;;
 +        * ) echo "Please answer yes or no.";;
 +    esac
 + done
 +fi</code>
 +
 +
 +===== Replace all files in a directory with empty files with the same name =====
 +<code>
 +#!/bin/bash
 +
 +# Get the directory path
 +echo "Enter the path to the directory: "
 +read dir_path
 +
 +# Check if the directory exists
 +if [ ! -d "$dir_path" ]; then
 +  echo "Error: Directory does not exist."
 +  exit 1
 +fi
 +
 +# Replace all files in the directory with empty files
 +for file in "$dir_path"/*; do
 +  if [ -f "$file" ]; then
 +    > "$file"
 +  fi
 +done
 +
 +echo "All files in the directory have been replaced with empty files."
 +</code>
 +
 +===== 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://macappstore.org/sshpass/ (for macOS).
 +
 +<code>
 +#!/bin/bash
 +
 +servers_file="servers"
 +users_file="users"
 +passwords_file="passwords"
 +logins_file="logins.txt"
 +
 +# Check if the input files exist
 +if [ ! -f "$servers_file" ] || [ ! -f "$users_file" ] || [ ! -f "$passwords_file" ]; then
 +  echo "Error: One or more of the input files do not exist."
 +  exit 1
 +fi
 +
 +# Read the servers, users, and passwords into arrays
 +servers=($(cat "$servers_file"))
 +users=($(cat "$users_file"))
 +passwords=($(cat "$passwords_file"))
 +
 +# Loop through all combinations of servers, users, and passwords
 +for server in "${servers[@]}"; do
 +  for user in "${users[@]}"; do
 +    for password in "${passwords[@]}"; do
 +      # Try to ssh into the server with the current combination of user and password
 +      ssh_output=$(sshpass -p "$password" ssh "$user"@"$server" -o StrictHostKeyChecking=no -o ConnectTimeout=3 2>&1)
 +
 +      # Check if the ssh login was successful
 +      if [ "$?" -eq 0 ]; then
 +        echo "Successful ssh login to $server using account $user and password $password"
 +        echo "$server $user $password" >> "$logins_file"
 +      else
 +        echo "Failed ssh login to $server using account $user and password $password"
 +      fi
 +    done
 +  done
 +done
 +</code>
 +
 +===== Remove duplicate and empty lines from file =====
 +<code>
 +#!/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 "^$" temp.txt > url.txt
 +
 +# Remove temp file
 +rm temp.txt
 +</code>
 +
 +===== Download files based on URLs in a file =====
 +<code>
 +#!/bin/bash
 +
 +# read list of URLs from file
 +while read url; do
 +  # download file using wget
 +  wget "$url"
 +done < url.txt
 +</code>
 +
 +Do the same but check if file exists. If it does compare sizes and ask what to do.
 +
 +<code>#!/bin/bash
 +
 +# Read URLs from the url.txt file and download the files
 +while read url; do
 +    filename=$(basename "$url")
 +    if [ -e "$filename" ]; then
 +        local_size=$(wc -c <"$filename")
 +        remote_size=$(curl -sI "$url" | awk '/Content-Length/ {sub("\r",""); print $2}')
 +        if [ "$local_size" = "$remote_size" ]; then
 +            echo "$filename already exists and has the same file size. Skipping download."
 +            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 [ "$response" = "y" ]; then
 +                read -p "Enter the new file name: " new_filename
 +                curl -L -o "$new_filename" "$url"
 +                echo "File saved as $new_filename."
 +            else
 +                echo "Skipping download of $filename."
 +            fi
 +        fi
 +    else
 +        curl -L -o "$filename" "$url"
 +        echo "Downloaded $filename."
 +    fi
 +done < url.txt
 +</code>
marc/linux/bash_snippets.1632984084.txt.gz · Last modified: (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki