Advertisement
Valentin_2023
Feb 6th, 2023
96
0
Never
This is comment for paste BASH Shuffle and Match up Lists
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/bash
  2. #usage team_up.sh FILE
  3.  
  4. [[ $# -eq  0 ]]  && printf "\nERROR: You must provide a file for the script to work.\n\n" 1>&2 && exit 1
  5.  
  6. readarray -t names < <(sort -R $1) #create an array with each line of the file; -t strips the new line character
  7. max=0 #used for proper formatting later in the script
  8.  
  9. for ((i=0; i<${#names[@]}; i+=2)) #i as in index; this way we get the even line numbers.
  10. do
  11.     if ((${#names[i]} > max)); then
  12.         max=${#names[i]}
  13.     else
  14.         max=$max #get the string lenghth of the longest name. Usage later in printf.
  15.     fi
  16. done
  17.  
  18. for ((i=0; i<${#names[@]}; i+=2)); do
  19.     printf -- "%-${max}s %s\n" "${names[i]}" "${names[i+1]}" #i+1, this way we get the odd line numbers
  20. done
  21.  
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement