Advertisement
fant0men

runnet_sender.sh

Nov 1st, 2019 (edited)
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.89 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script is meant to send commands over the network to my laptop,
  4. # which will then run those commands. The output will be displayed on
  5. # screen.
  6.  
  7. #
  8. # dir1='/run/user/1000/gvfs/smb-share:server=buddha-hp-compaq-nw8440-rn039aw-ak8.local,share=buddha'
  9. dir1="${HOME}/buddha"
  10. dir2="${dir1}/run"
  11. bn='run_commands'
  12. list="${dir2}/${bn}_list.txt"
  13. stdout="${dir2}/${bn}_stdout.log"
  14. switch_f="${dir2}/${bn}_switch.txt"
  15.  
  16. count=0
  17. run=0
  18.  
  19. declare -a commands
  20.  
  21. # trap ctrl-c and call ctrl_c()
  22. trap ctrl_c INT
  23.  
  24. ctrl_c () {
  25.     echo "** Trapped CTRL-C"
  26.     exit
  27. }
  28.  
  29. # Creates the 'switch' function, which will wait if $switch_f is
  30. # set to '1'.
  31. switch () {
  32.     switch=$(cat "$switch_f")
  33.  
  34.     while [[ $switch -eq 1 ]]; do
  35.         sleep 1
  36.  
  37.         switch=$(cat "$switch_f")
  38.     done
  39. }
  40.  
  41. # Creates a function called 'spin', which will show a simple animation,
  42. # while waiting for the command output.
  43. spin () {
  44.     spinner=( '   ' '.  ' '.. ' '...' )
  45.  
  46.     while [[ 1 ]]; do
  47.         for s in "${spinner[@]}"; do
  48.             echo -ne "\rWait${s}"
  49.             sleep 0.5
  50.         done
  51.     done
  52. }
  53.  
  54. if [[ ! -f $list ]]; then
  55.     echo "Receiver list doesn't yet exist!"
  56.     exit
  57. fi
  58.  
  59. while [[ 1 ]]; do
  60.     echo -e "\nStart typing commands, and finish with 'run'!\n"
  61.  
  62.     until [[ $run -eq 1 ]]; do
  63.         read tmp
  64.  
  65.         if [[ $tmp != 'run' ]]; then
  66.             commands[${count}]="$tmp"
  67.         else
  68.             run=1
  69.         fi
  70.  
  71.         let count++
  72.     done
  73.  
  74.     count=0
  75.     run=0
  76.  
  77.     for (( i = 0; i < ${#commands[@]}; i++ )); do
  78.         command="${commands[${i}]}"
  79.  
  80.         if [[ -z $command ]]; then
  81.             continue
  82.         fi
  83.  
  84. # stdbuf -o 0 echo "$command" >&3
  85.         echo "$command" >> "$list"
  86.  
  87.         clear
  88.  
  89.         spin &
  90.         spin_pid="$!"
  91.  
  92. # Waiting for the receiver script to get a chance to
  93. # flip the switch variable. Otherwise, output will be displayed
  94. # too quickly.
  95.         sleep 0.1
  96.  
  97.         switch
  98.  
  99.         sync
  100.  
  101.         kill "$spin_pid"
  102.  
  103.         clear
  104.  
  105.         less +G "$stdout"
  106.  
  107.         unset -v command
  108.     done
  109.  
  110.     unset -v commands
  111. done
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement