Browse Source

format shell

master
Cian Butler 3 years ago
parent
commit
3cb3896a88
Signed by: butlerx GPG Key ID: B37CA765BAA89170
11 changed files with 781 additions and 684 deletions
  1. +1
    -0
      .~lock.chrome-bikioccmkafdpakkkcpdbppfkghcmihk-Default.desktop#
  2. +279
    -263
      2048
  3. +14
    -8
      README.md
  4. +59
    -63
      check
  5. +63
    -63
      collectnetworkinfo
  6. +39
    -39
      convertPdf
  7. +2
    -2
      dual-screen
  8. +60
    -54
      duplicated
  9. +16
    -0
      git-fix
  10. +131
    -107
      killrenegade
  11. +117
    -85
      minesweeper

+ 1
- 0
.~lock.chrome-bikioccmkafdpakkkcpdbppfkghcmihk-Default.desktop# View File

@@ -0,0 +1 @@
,butlerx,ares,28.05.2017 11:35,file:///home/butlerx/.config/libreoffice/4;

+ 279
- 263
2048 View File

@@ -1,14 +1,16 @@
#!/usr/bin/env bash

set -e

#important variables
declare -ia board # array that keeps track of game status
declare -i pieces # number of pieces present on board
declare -i score=0 # score variable
declare -i flag_skip # flag that prevents doing more than one operation on
# single field in one step
declare -i moves # stores number of possible moves to determine if player lost
# the game
declare ESC=$'\e' # escape byte
# single field in one step
declare -i moves # stores number of possible moves to determine if player lost
# the game
declare ESC=$'\e' # escape byte
declare header="Bash 2048 v1.1 (https://github.com/mydzor/bash2048)"

start_time=$(date +%s)
@@ -22,81 +24,85 @@ declare config_dir="$HOME/.bash2048"

#for colorizing numbers
declare -a colors
colors[2]=33 # yellow text
colors[4]=32 # green text
colors[8]=34 # blue text
colors[16]=36 # cyan text
colors[32]=35 # purple text
colors[64]="33m\033[7" # yellow background
colors[128]="32m\033[7" # green background
colors[256]="34m\033[7" # blue background
colors[512]="36m\033[7" # cyan background
colors[1024]="35m\033[7" # purple background
colors[2048]="31m\033[7" # red background (won with default target)
colors[2]=33 # yellow text
colors[4]=32 # green text
colors[8]=34 # blue text
colors[16]=36 # cyan text
colors[32]=35 # purple text
colors[64]="33m\\033[7" # yellow background
colors[128]="32m\\033[7" # green background
colors[256]="34m\\033[7" # blue background
colors[512]="36m\\033[7" # cyan background
colors[1024]="35m\\033[7" # purple background
colors[2048]="31m\\033[7" # red background (won with default target)

exec 3>/dev/null # no logging by default
exec 3>/dev/null # no logging by default

trap "end_game 0 1" INT #handle INT signal

#simplified replacement of seq command
function _seq {
local cur=1
local max
local inc=1
case $# in
1) let max=$1;;
2) let cur=$1
let max=$2;;
3) let cur=$1
let inc=$2
let max=$3;;
esac
while test $max -ge $cur; do
printf "%i " "$cur"
let cur+=inc
done
function _seq() {
local cur=1
local max
local inc=1
case $# in
1) ((max = $1)) ;;
2)
((cur = $1))
((max = $2))
;;
3)
((cur = $1))
((inc = $2))
((max = $3))
;;
esac
while test $max -ge $cur; do
printf "%i " "$cur"
((cur += inc))
done
}

# print currect status of the game, last added pieces are marked red
function print_board {
clear
printf "$header pieces=$pieces target=$target score=$score\n"
printf "Board status:\n" >&3
printf "\n"
printf '/------'
for l in $(_seq 1 "$index_max"); do
printf '+------'
done
printf '\\\n'
for l in $(_seq 0 "$index_max"); do
printf '|'
for m in $(_seq 0 "$index_max"); do
if let ${board[l*$board_size+m]}; then
if let '(last_added==(l*board_size+m))|(first_round==(l*board_size+m))'; then
printf '\033[1m\033[31m %4d \033[0m|' "${board[l*$board_size+m]}"
else
printf "\033[1m\033[${colors[${board[l*$board_size+m]}]}m %4d\033[0m |" "${board[l*$board_size+m]}"
fi
printf " %4d |" "${board[l*$board_size+m]}" >&3
else
printf ' |'
printf ' |' >&3
fi
done
let l==$index_max || {
printf '\n|------'
for j in $(_seq 1 "$index_max"); do
printf '+------'
done
printf '|\n'
printf '\n' >&3
}
done
printf '\n\\------'
for l in $(_seq 1 "$index_max"); do
printf '+------'
done
printf '/\n'
function print_board() {
clear
printf "%s pieces=%s target=%s score=%s\\n" "$header" "$pieces" "$target" "$score"
printf "Board status:\\n" >&3
printf "\\n"
printf '/------'
for l in $(_seq 1 "$index_max"); do
printf '+------'
done
printf '\\\n'
for l in $(_seq 0 "$index_max"); do
printf '|'
for m in $(_seq 0 "$index_max"); do
if ((${board[l * $board_size + m]})); then
if ( (last_added=(l*board_size+m)) | (first_round=(l*board_size+m))); then
printf '\033[1m\033[31m %4d \033[0m|' "${board[l * $board_size + m]}"
else
printf "\\033[1m\\033[${colors[${board[l * $board_size + m]}]}m %4d\\033[0m |" "${board[l * $board_size + m]}"
fi
printf " %4d |" "${board[l * $board_size + m]}" >&3
else
printf ' |'
printf ' |' >&3
fi
done
((l == index_max)) || {
printf '\n|------'
for j in $(_seq 1 "$index_max"); do
printf '+------'
done
printf '|\n'
printf '\n' >&3
}
done
printf '\n\\------'
for l in $(_seq 1 "$index_max"); do
printf '+------'
done
printf '/\n'
}

# Generate new piece on the board
@@ -106,18 +112,18 @@ function print_board {
# outputs:
# $board - new state of the game board
# $pieces - new number of pieces
function generate_piece {
while true; do
let pos=RANDOM%fields_total
let board[pos] || {
let value=RANDOM%10?2:4
board[$pos]=$value
last_added=$pos
printf "Generated new piece with value %i at position [%i]\n" "$value" "$pos" >&3
break;
}
done
let pieces++
function generate_piece() {
while true; do
((pos = RANDOM % fields_total))
((board[pos])) || {
((value = RANDOM % 10 ? 2 : 4))
board[$pos]=$value
last_added=$pos
printf "Generated new piece with value %i at position [%i]\\n" "$value" "$pos" >&3
break
}
done
((pieces++))
}

# perform push operation between two pieces
@@ -133,167 +139,166 @@ function generate_piece {
# $change - indicates if the board was changed this round
# $flag_skip - indicates that recipient piece cannot be modified further
# $board - new state of the game board
function push_pieces {
case $4 in
"up")
let "first=$2*$board_size+$1"
let "second=($2+$3)*$board_size+$1"
;;
"down")
let "first=(index_max-$2)*$board_size+$1"
let "second=(index_max-$2-$3)*$board_size+$1"
;;
"left")
let "first=$1*$board_size+$2"
let "second=$1*$board_size+($2+$3)"
;;
"right")
let "first=$1*$board_size+(index_max-$2)"
let "second=$1*$board_size+(index_max-$2-$3)"
;;
esac
let ${board[$first]} || {
let ${board[$second]} && {
if test -z "$5"; then
board[$first]=${board[$second]}
let board["$second"]=0
let change=1
printf "move piece with value ${board[$first]} from [$second] to [$first]\n" >&3
else
let moves++
fi
return
}
return
}
let ${board[$second]} && let flag_skip=1
let "${board[$first]}==${board[second]}" && {
if test -z "$5"; then
let board[$first]*=2
let "board[$first]==$target" && end_game 1
let board[$second]=0
let pieces-=1
let change=1
let score+=${board[$first]}
printf "joined piece from [$second] with [$first], new value=${board[$first]}\n" >&3
else
let moves++
fi
}
function push_pieces() {
case $4 in
"up")
((first = $2 * board_size + $1))
((second = ($2 + $3) * board_size + $1))
;;
"down")
((first = (index_max - $2) * board_size + $1))
((second = (index_max - $2 - $3) * board_size + $1))
;;
"left")
((first = $1 * board_size + $2))
((second = $1 * board_size + ($2 + $3)))
;;
"right")
((first = $1 * board_size + (index_max - $2)))
((second = $1 * board_size + (index_max - $2 - $3)))
;;
esac
((${board[$first]})) || {
((${board[$second]})) && {
if test -z "$5"; then
board[$first]=${board[$second]}
((board["$second"] = 0))
((change = 1))
printf "move piece with value %s from [%s] to [%s]\\n" ${board[$first]} $second $first >&3
else
((moves++))
fi
return
}
return
}
((${board[$second]})) && ((flag_skip = 1))
if ((${board[$first]} == board[second])); then
if test -z "$5"; then
((board[first] *= 2))
((board[first] == target)) && end_game 1
((board[second] = 0))
((pieces -= 1))
((change = 1))
((score += ${board[$first]}))
printf "joined piece from [%s] with [%s], new value=%s\\n" $second $first ${board[$first]} >&3
else
((moves++))
fi
fi
}

function apply_push {
printf "\n\ninput: $1 key\n" >&3
for i in $(_seq 0 $index_max); do
for j in $(_seq 0 $index_max); do
flag_skip=0
let increment_max=index_max-j
for k in $(_seq 1 $increment_max); do
let flag_skip && break
push_pieces $i $j $k $1 $2
done
done
done
function apply_push() {
printf "\\n\\ninput: %s key\\n" "$1" >&3
for i in $(_seq 0 "$index_max"); do
for j in $(_seq 0 "$index_max"); do
flag_skip=0
((increment_max = index_max - j))
for k in $(_seq 1 $increment_max); do
((flag_skip)) && break
push_pieces "$i" "$j" "$k" "$1" "$2"
done
done
done
}

function check_moves {
let moves=0
apply_push up fake
apply_push down fake
apply_push left fake
apply_push right fake
function check_moves() {
((moves = 0))
apply_push up fake
apply_push down fake
apply_push left fake
apply_push right fake
}

function key_react {
let change=0
read -d '' -sn 1
test "$REPLY" = "$ESC" && {
read -d '' -sn 1 -t1
test "$REPLY" = "[" && {
read -d '' -sn 1 -t1
case $REPLY in
A) apply_push up;;
B) apply_push down;;
C) apply_push right;;
D) apply_push left;;
esac
}
} || {
case $REPLY in
k) apply_push up;;
j) apply_push down;;
l) apply_push right;;
h) apply_push left;;
esac
}
function key_react() {
((change = 0))
read -d -r '' -sn 1
if test "$REPLY" = "$ESC"; then
read -r -d '' -sn 1 -t1
test "$REPLY" = "[" && {
read -r -d '' -sn 1 -t1
case $REPLY in
A) apply_push up ;;
B) apply_push down ;;
C) apply_push right ;;
D) apply_push left ;;
esac
}
else
case $REPLY in
k) apply_push up ;;
j) apply_push down ;;
l) apply_push right ;;
h) apply_push left ;;
esac
fi
}

function save_game {
rm -rf "$config_dir"
mkdir "$config_dir"
echo "${board[@]}" > "$config_dir/board"
echo "$board_size" > "$config_dir/board_size"
echo "$pieces" > "$config_dir/pieces"
echo "$target" > "$config_dir/target"
# echo "$log_file" > "$config_dir/log_file"
echo "$score" > "$config_dir/score"
echo "$first_round" > "$config_dir/first_round"
function save_game() {
rm -rf "$config_dir"
mkdir "$config_dir"
echo "${board[@]}" >"$config_dir/board"
echo "$board_size" >"$config_dir/board_size"
echo "$pieces" >"$config_dir/pieces"
echo "$target" >"$config_dir/target"
# echo "$log_file" > "$config_dir/log_file"
echo "$score" >"$config_dir/score"
echo "${first_round[0]}" >"$config_dir/first_round"
}

function reload_game {
printf "Loading saved game...\n" >&3
function reload_game() {
printf "Loading saved game...\\n" >&3

if test ! -d "$config_dir"; then
return
fi
board=(`cat "$config_dir/board"`)
board_size=(`cat "$config_dir/board_size"`)
board=(`cat "$config_dir/board"`)
pieces=(`cat "$config_dir/pieces"`)
first_round=(`cat "$config_dir/first_round"`)
target=(`cat "$config_dir/target"`)
score=(`cat "$config_dir/score"`)
if test ! -d "$config_dir"; then
return
fi
board=$(cat "$config_dir/board")
board_size=($(cat "$config_dir/board_size"))
board=($(cat "$config_dir/board"))
pieces=($(cat "$config_dir/pieces"))
first_round=($(cat "$config_dir/first_round"))
target=($(cat "$config_dir/target"))
score=($(cat "$config_dir/score"))

fields_total=board_size*board_size
index_max=board_size-1
fields_total=$((board_size * board_size))
index_max=$((board_size - 1))
}

function end_game {
# count game duration
end_time=$(date +%s)
let total_time=end_time-start_time
print_board
printf "Your score: $score\n"
printf "This game lasted "
`date --version > /dev/null 2>&1`
if [[ "$?" -eq 0 ]]; then
date -u -d @${total_time} +%T
else
date -u -r ${total_time} +%T
fi
stty echo
let $1 && {
printf "Congratulations you have achieved $target\n"
exit 0
}
let test -z $2 && {
read -n1 -p "Do you want to overwrite saved game? [y|N]: "
test "$REPLY" = "Y" || test "$REPLY" = "y" && {
save_game
printf "\nGame saved. Use -r option next to load this game.\n"
exit 0
}
test "$REPLY" = "" && {
printf "\nGame not saved.\n"
exit 0
}
}
printf "\nYou have lost, better luck next time.\033[0m\n"
exit 0
function end_game() {
# count game duration
end_time=$(date +%s)
((total_time = end_time - start_time))
print_board
printf "Your score: %s\\n" ${score[0]}
printf "This game lasted "
if date --version >/dev/null 2>&1; then
date -u -d @${total_time} +%T
else
date -u -r ${total_time} +%T
fi
stty echo
(($1)) && {
printf "Congratulations you have achieved %s\\n" ${target[0]}
exit 0
}
( (test -z "$2")) && {
read -n1 -p -r "Do you want to overwrite saved game? [y|N]: "
test "$REPLY" = "Y" || test "$REPLY" = "y" && {
save_game
printf "\\nGame saved. Use -r option next to load this game.\\n"
exit 0
}
test "$REPLY" = "" && {
printf "\\nGame not saved.\\n"
exit 0
}
}
printf "\\nYou have lost, better luck next time.\\033[0m\\n"
exit 0
}

function help {
cat <<END_HELP
function help() {
cat <<END_HELP
Usage: $1 [-b INTEGER] [-t INTEGER] [-l FILE] [-r] [-h]

-b specify game board size (sizes 3-9 allowed)
@@ -305,53 +310,64 @@ Usage: $1 [-b INTEGER] [-t INTEGER] [-l FILE] [-r] [-h]
END_HELP
}


#parse commandline options
while getopts "b:t:l:rh" opt; do
case $opt in
b ) board_size="$OPTARG"
let '(board_size>=3)&(board_size<=9)' || {
printf "Invalid board size, please choose size between 3 and 9\n"
exit -1
};;
t ) target="$OPTARG"
printf "obase=2;$target\n" | bc | grep -e '^1[^1]*$'
let $? && {
printf "Invalid target, has to be power of two\n"
exit -1
};;
r ) reload_flag="1";;
h ) help $0
exit 0;;
l ) exec 3>$OPTARG;;
\?) printf "Invalid option: -"$opt", try $0 -h\n" >&2
exit 1;;
: ) printf "Option -"$opt" requires an argument, try $0 -h\n" >&2
exit 1;;
esac
case $opt in
b)
board_size="$OPTARG"
(
(board_size >= 3) &
(board_size <= 9)
) || {
printf "Invalid board size, please choose size between 3 and 9\\n"
exit -1
}
;;
t)
target="$OPTARG"
if printf "obase=2;%s\\n" "${target[0]}" | bc | grep -e '^1[^1]*$'; then
printf "Invalid target, has to be power of two\\n"
exit -1
fi
;;
r) reload_flag="1" ;;
h)
help "$0"
exit 0
;;
l) exec 3>"$OPTARG" ;;
\?)
printf "Invalid option: -%s, try $0 -h\\n" "$opt" >&2
exit 1
;;
:)
printf "Option -%s requires an argument, try $0 -h\\n" "$opt" >&2
exit 1
;;
esac
done

#init board
let fields_total=board_size*board_size
let index_max=board_size-1
((fields_total = board_size * board_size))
((index_max = board_size - 1))
for i in $(_seq 0 $fields_total); do board[$i]="0"; done
let pieces=0
((pieces = 0))
generate_piece
first_round=$last_added
first_round=${last_added[0]}
generate_piece

#load saved game if flag is set
if test $reload_flag = "1"; then
reload_game
reload_game
fi

while true; do
print_board
key_react
let change && generate_piece
first_round=-1
let pieces==fields_total && {
check_moves
let moves==0 && end_game 0 #lose the game
}
print_board
key_react
((change)) && generate_piece
first_round=-1
((pieces == fields_total)) && {
check_moves
((moves == 0)) && end_game 0 #lose the game
}
done

+ 14
- 8
README.md View File

@@ -1,13 +1,19 @@
# Bin Folder

Random shell scripts

1. [Interactive](/blob/master/Interactive): a simple but very much interactive script
1. [Collectnetworkinfo](/blob/master/collectnetworkinfo): gather information related to server
1. [2048](/blob/master/2048): Play 2048
1. [duplicated](/blob/master/duplicated): Create list of duplicate files
1. [killrenegade](/blob/master/killrenegade): Kill renegade processes
1. [Interactive](/blob/master/Interactive): a simple but very much interactive
script
1. [Collectnetworkinfo](/blob/master/collectnetworkinfo): gather information
related to server
1. [2048](/blob/master/2048): Play 2048
1. [duplicated](/blob/master/duplicated): Create list of duplicate files
1. [killrenegade](/blob/master/killrenegade): Kill renegade processes
1. [minesweeper](/blob/master/minesweeper): Play Minesweeper
1. [single-screen](/blob/master/single-screen): Set Output to One Monitor and run startbar
1. [single-screen](/blob/master/single-screen): Set Output to One Monitor and
run startbar
1. [startbar](/blob/master/startbar): Kill all polybar and start main bar
1. [dual-screen](/blob/master/dual-screen): Set output for dual monitor and run startbar2
1. [startbar2](/blob/master/startvar2): Kill all polybar and start main bar and second screen bar
1. [dual-screen](/blob/master/dual-screen): Set output for dual monitor and run
startbar2
1. [startbar2](/blob/master/startvar2): Kill all polybar and start main bar and
second screen bar

+ 59
- 63
check View File

@@ -2,95 +2,91 @@

set -e

function cleanup {
for pid in "${PIDS[@]}"
do
kill -0 "$pid" 2&> /dev/null && kill "$pid"
done
exit 0
function cleanup() {
for pid in "${PIDS[@]}"; do
kill -0 "$pid" 2 &>/dev/null && kill "$pid"
done
exit 0
}

function position {
# based on a script from http://invisible-island.net/xterm/xterm.faq.html
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
# on my system, the following line can be replaced by the line below it
# echo -en "\033[6n" > /dev/tty
tput u7 > /dev/tty # when TERM=xterm (and relatives)
IFS=';' read -r -d R -a pos
stty "$oldstty"
# change from one-based to zero based so they work with: tput cup $row $col
row=$((${pos[0]:2} - 1)) # strip off the esc-[
echo "$row"
function position() {
# based on a script from http://invisible-island.net/xterm/xterm.faq.html
exec </dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
# on my system, the following line can be replaced by the line below it
# echo -en "\033[6n" > /dev/tty
tput u7 >/dev/tty # when TERM=xterm (and relatives)
IFS=';' read -r -d R -a pos
stty "$oldstty"
# change from one-based to zero based so they work with: tput cup $row $col
row=$((${pos[0]:2} - 1)) # strip off the esc-[
echo "$row"
}

function monitor {
local url=$1
local p=$((START_CURSOR+$2+1))

# -w "$GREEN%{http_code}$RESET $url \\033[${p};${TIMING_COL}f%{time_total}s TTFB: %{time_connect} + %{time_starttransfer} (%{size_download})" \

echo -e "\033[${p};0f$(curl "$url" --compress -s -o /dev/null -w \
"%{http_code}|%{time_total}|%{time_pretransfer}|%{time_starttransfer}\n"\
| sed -n '/^/ s///p' \
| while IFS='|' read -r http_code time_total time_pretransfer time_starttransfer;
do
if [[ $http_code -gt 399 ]]; then
http_code="$RED$http_code$RESET"
else
http_code="$GREEN$http_code$RESET"
fi
TTFB=$(echo -e "$time_starttransfer $time_pretransfer" | awk '{ printf "%.3fs", $1 - $2 }')
echo -e "$http_code $url \\033[${p};${TIMING_COL}f${time_total}s (TTFB $TTFB)"
done)\n\033[${LAST_CURSOR};0f" &

PIDS=("${PIDS[@]}" "$!")
function monitor() {
local url=$1
local p=$((START_CURSOR + $2 + 1))

# -w "$GREEN%{http_code}$RESET $url \\033[${p};${TIMING_COL}f%{time_total}s TTFB: %{time_connect} + %{time_starttransfer} (%{size_download})" \

echo -e "\\033[${p};0f$(curl "$url" --compress -s -o /dev/null -w \
"%{http_code}|%{time_total}|%{time_pretransfer}|%{time_starttransfer}\\n" |
sed -n '/^/ s///p' |
while IFS='|' read -r http_code time_total time_pretransfer time_starttransfer; do
if [[ $http_code -gt 399 ]]; then
http_code="$RED$http_code$RESET"
else
http_code="$GREEN$http_code$RESET"
fi
TTFB=$(echo -e "$time_starttransfer $time_pretransfer" | awk '{ printf "%.3fs", $1 - $2 }')
echo -e "$http_code $url \\033[${p};${TIMING_COL}f${time_total}s (TTFB $TTFB)"
done)\\n\\033[${LAST_CURSOR};0f" &

PIDS=("${PIDS[@]}" "$!")
}

declare -a PIDS
RED="\033[31m"
RESET="\033[0m"
GREEN="\033[32m"
RED="\\033[31m"
RESET="\\033[0m"
GREEN="\\033[32m"
START_CURSOR=$(position)
LAST_CURSOR=0
TIMING_COL=0
declare -a URLS

if [ -t 0 ]; then
if [ $# -eq 0 ]
then
echo -e "Usage:\n\n ${RED}check${RESET} <url>\n cat urls.txt | \033[1mcheck${RESET}\n"
exit 1
else
URLS=($1)
fi
if [ $# -eq 0 ]; then
echo -e "Usage:\\n\\n ${RED}check${RESET} <url>\\n cat urls.txt | \\033[1mcheck${RESET}\\n"
exit 1
else
URLS=("$1")
fi
else
while read -r line; do
URLS=("${URLS[@]}" $line)
done
while read -r line; do
URLS=("${URLS[@]}" "$line")
done
fi

trap cleanup EXIT

for url in "${URLS[@]}"; do
length=${#url}
if [[ $length > $TIMING_COL ]]; then TIMING_COL=$length; fi
echo -e "... $url"
length=${#url}
if [[ $length > $TIMING_COL ]]; then TIMING_COL=$length; fi
echo -e "... $url"
done

TIMING_COL=$(( TIMING_COL + 6))
TIMING_COL=$((TIMING_COL + 6))
LAST_CURSOR=$(position)

if [[ $LAST_CURSOR == $(( $(tput lines) - 1 )) ]]
then
START_CURSOR=$(( LAST_CURSOR - ${#URLS[@]}))
if [[ $LAST_CURSOR == $(($(tput lines) - 1)) ]]; then
START_CURSOR=$((LAST_CURSOR - ${#URLS[@]}))
fi

for index in "${!URLS[@]}"; do
monitor "${URLS[index]}" "$index"
monitor "${URLS[index]}" "$index"
done

wait "${PIDS[@]}"

+ 63
- 63
collectnetworkinfo View File

@@ -17,73 +17,73 @@ OUTPUT="network.$(date +'%d-%m-%y').info.txt"
## Email info to?? ##
SUPPORT_ID="your_name@service_provider.com"
chk_root() {
local meid=$(id -u)
if [ $meid -ne 0 ]; then
echo "You must be root user to run this tool"
exit 999
fi
local meid=$(id -u)
if [ $meid -ne 0 ]; then
echo "You must be root user to run this tool"
exit 999
fi
}
write_header() {
echo "---------------------------------------------------" >> $OUTPUT
echo "$@" >> $OUTPUT
echo "---------------------------------------------------" >> $OUTPUT
echo "---------------------------------------------------" >>$OUTPUT
echo "$@" >>$OUTPUT
echo "---------------------------------------------------" >>$OUTPUT
}
dump_info() {
echo "* Hostname: $(hostname)" >$OUTPUT
echo "* Run date and time: $(date)" >>$OUTPUT
write_header "Linux Distro"
echo "Linux kernel: $(uname -mrs)" >>$OUTPUT
$LSB -a >> $OUTPUT
[ -x ${HWINF} ] && write_header "${HWINF}"
[ -x ${HWINF} ] && ${HWINF} >> $OUTPUT
[ -x ${HWINF} ] && write_header "${HWINF}"
[ -x ${HWINF} ] && ${HWINF} >> $OUTPUT
write_header "PCI Devices"
${LSPCI} -v >> $OUTPUT
write_header "$IFCFG Output"
$IFCFG >> $OUTPUT
write_header "Kernel Routing Table"
$ROUTE -n >> $OUTPUT
write_header "Network Card Drivers Configuration $DRVCONF"
[ -f $DRVCONF ] && grep eth $DRVCONF >> $OUTPUT || echo "Error $DRVCONF file not found." >> $OUTPUT
write_header "DNS Client $DNSCLIENT Configuration"
[ -f $DNSCLIENT ] && cat $DNSCLIENT >> $OUTPUT || echo "Error $DNSCLIENT file not found." >> $OUTPUT
write_header "Network Configuration File"
for f in $NETCFC; do
if [ -f $f ]; then
echo "** $f **" >> $OUTPUT
cat $f >> $OUTPUT
else
echo "Error $f not found." >> $OUTPUT
fi
done
write_header "Network Aliase File"
for f in $NETALIASCFC; do
if [ -f $f ]; then
echo "** $f **" >> $OUTPUT
cat $f >> $OUTPUT
else
echo "Error $f not found." >> $OUTPUT
fi
done
write_header "Network Static Routing Configuration"
for f in $NETSTATICROUTECFC; do
if [ -f $f ]; then
echo "** $f **" >> $OUTPUT
cat $f >> $OUTPUT
else
echo "Error $f not found." >> $OUTPUT
fi
done
write_header "IP4 Firewall Configuration"
$IP4FW -L -n >> $OUTPUT
write_header "IP6 Firewall Configuration"
$IP6FW -L -n >> $OUTPUT
write_header "Network Stats"
$NETSTAT -s >> $OUTPUT
write_header "Network Tweaks via $SYSCTL"
[ -f $SYSCTL ] && cat $SYSCTL >> $OUTPUT || echo "Error $SYSCTL not found." >>$OUTPUT
echo "The Network Configuration Info Written To $OUTPUT. Please email this file to $SUPPORT_ID."
echo "* Hostname: $(hostname)" >$OUTPUT
echo "* Run date and time: $(date)" >>$OUTPUT
write_header "Linux Distro"
echo "Linux kernel: $(uname -mrs)" >>$OUTPUT
$LSB -a >>$OUTPUT
[ -x ${HWINF} ] && write_header "${HWINF}"
[ -x ${HWINF} ] && ${HWINF} >>$OUTPUT
[ -x ${HWINF} ] && write_header "${HWINF}"
[ -x ${HWINF} ] && ${HWINF} >>$OUTPUT
write_header "PCI Devices"
${LSPCI} -v >>$OUTPUT
write_header "$IFCFG Output"
$IFCFG >>$OUTPUT
write_header "Kernel Routing Table"
$ROUTE -n >>$OUTPUT
write_header "Network Card Drivers Configuration $DRVCONF"
[ -f $DRVCONF ] && grep eth $DRVCONF >>$OUTPUT || echo "Error $DRVCONF file not found." >>$OUTPUT
write_header "DNS Client $DNSCLIENT Configuration"
[ -f $DNSCLIENT ] && cat $DNSCLIENT >>$OUTPUT || echo "Error $DNSCLIENT file not found." >>$OUTPUT
write_header "Network Configuration File"
for f in $NETCFC; do
if [ -f $f ]; then
echo "** $f **" >>$OUTPUT
cat $f >>$OUTPUT
else
echo "Error $f not found." >>$OUTPUT
fi
done
write_header "Network Aliase File"
for f in $NETALIASCFC; do
if [ -f $f ]; then
echo "** $f **" >>$OUTPUT
cat $f >>$OUTPUT
else
echo "Error $f not found." >>$OUTPUT
fi
done
write_header "Network Static Routing Configuration"
for f in $NETSTATICROUTECFC; do
if [ -f $f ]; then
echo "** $f **" >>$OUTPUT
cat $f >>$OUTPUT
else
echo "Error $f not found." >>$OUTPUT
fi
done
write_header "IP4 Firewall Configuration"
$IP4FW -L -n >>$OUTPUT
write_header "IP6 Firewall Configuration"
$IP6FW -L -n >>$OUTPUT
write_header "Network Stats"
$NETSTAT -s >>$OUTPUT
write_header "Network Tweaks via $SYSCTL"
[ -f $SYSCTL ] && cat $SYSCTL >>$OUTPUT || echo "Error $SYSCTL not found." >>$OUTPUT
echo "The Network Configuration Info Written To $OUTPUT. Please email this file to $SUPPORT_ID."
}
chk_root
dump_info

+ 39
- 39
convertPdf View File

@@ -1,49 +1,49 @@
#!/bin/bash

function convertDir () {
for file in *.pdf; do
convertPdf "$file"
done
function convertDir() {
for file in *.pdf; do
convertPdf "$file"
done
}

function convertPdf () {
file=$1
file="${file%.*}"
mkdir "$file"
convert -density 300 "$file".pdf -quality 100 "$file"/"$file".png
mv "$file".pdf "$file"
mv "$file"/"$file"-0.png "$file"/"$file".png
if [ "$COVER" = true ]; then
rm -rf "$file"/"$file"-*
fi
function convertPdf() {
file=$1
file="${file%.*}"
mkdir "$file"
convert -density 300 "$file".pdf -quality 100 "$file"/"$file".png
mv "$file".pdf "$file"
mv "$file"/"$file"-0.png "$file"/"$file".png
if [ "$COVER" = true ]; then
rm -rf "$file"/"$file"-*
fi
}

function usage () {
echo "Usage: convertPdf [--cover | -c] [-d | --dir] [-f | --file (fileToConvert.pdf)]"
echo "convert the PWD: convertPdf --dir"
echo "convert a file: convertPdf --file fileToConvert.pdf"
echo "--cover flag will only save the cover slide"
function usage() {
echo "Usage: convertPdf [--cover | -c] [-d | --dir] [-f | --file (fileToConvert.pdf)]"
echo "convert the PWD: convertPdf --dir"
echo "convert a file: convertPdf --file fileToConvert.pdf"
echo "--cover flag will only save the cover slide"
}

while [[ $# -gt 1 ]]; do
key="$1"
case $key in
--dir|-d)
convertDir
;;
--cover|-c)
COVER=true
;;
--file|-f)
convertPdf "$2"
shift
;;
--help|-h)
usage
exit
;;
*)
break
;;
esac
key="$1"
case $key in
--dir | -d)
convertDir
;;
--cover | -c)
COVER=true
;;
--file | -f)
convertPdf "$2"
shift
;;
--help | -h)
usage
exit
;;
*)
break
;;
esac
done

+ 2
- 2
dual-screen View File

@@ -1,5 +1,5 @@
#!/usr/bin/env sh

#xrandr --output eDP1 --auto --output DP1 --auto --scale 2x2 --right-of eDP1
xrandr --output eDP1 --auto --output DP1 --auto --left-of eDP1
xrandr --output eDP1 --auto --output DP1 --auto --right-of eDP1
# xrandr --output eDP1 --auto --output DP1 --auto --left-of eDP1
~/bin/startbar2

+ 60
- 54
duplicated View File

@@ -7,14 +7,20 @@ directory=()
sizelimit='0'
algorithm='md5'
missing=''
while (( $# )); do
case "$1" in
'-s') shift; sizelimit="$1" ;;
'--size='*) sizelimit="${1#*=}" ;;
'-a') shift; algorithm="$1" ;;
'--algo='*) algorithm="${1#*=}" ;;
'--help'|'-h'|'-?')
cat <<EOT
while (($#)); do
case "$1" in
'-s')
shift
sizelimit="$1"
;;
'--size='*) sizelimit="${1#*=}" ;;
'-a')
shift
algorithm="$1"
;;
'--algo='*) algorithm="${1#*=}" ;;
'--help' | '-h' | '-?')
cat <<EOT
Syntax :
duplicate.sh [-s size] [-a algorithm] [directory [...]]
Parameters :
@@ -23,102 +29,102 @@ Parameters :
directory - directory with path to include in the search ( . )
All searches are recursive.
EOT
exit
;;
*)
[[ -e "$1" ]] || missing="$missing '$1',"
directory[${#directory[@]}]="$1"
;;
esac
shift
exit
;;
*)
[[ -e "$1" ]] || missing="$missing '$1',"
directory[${#directory[@]}]="$1"
;;
esac
shift
done

algorithm="$( tr '[:upper:]' '[:lower:]' <<< "$algorithm" )"
algorithm="$(tr '[:upper:]' '[:lower:]' <<<"$algorithm")"
summer="${algorithm}sum"

[[ "$missing" ]] && {
echo "error : can not find the following directories :$missing"
exit 1
echo "error : can not find the following directories :$missing"
exit 1
}

[[ "$sizelimit" != +(0|1|2|3|4|5|6|7|8|9) ]] && {
echo "error : can not use invalid size limit : $sizelimit"
exit 1
echo "error : can not use invalid size limit : $sizelimit"
exit 1
}

[[ "$algorithm" != @(md5|sha1) ]] && {
echo "error : can not use unknown algorithm : $algorithm"
exit 1
echo "error : can not use unknown algorithm : $algorithm"
exit 1
}

type "$summer" > /dev/null 2>&1 || {
echo "error : can not find the tool for checksum : $summer"
exit 2
type "$summer" >/dev/null 2>&1 || {
echo "error : can not find the tool for checksum : $summer"
exit 2
}

echo -n 'creating temporary directory... '
work="$( mktemp -d -p '/tmp/' 'dupXXXXXX' )"
work="$(mktemp -d -p '/tmp/' 'dupXXXXXX')"
echo "Ok ( ${work##*/} )"

echo -n 'creating file list... '
find "${directory[@]}" ! -path "$work/*" -type f -printf '%s*\t%p\n' > "$work/1" 2> "$work/1e"
echo "Ok ( $( wc -l < "$work/1" ), error $( wc -l < "$work/1e" ) )"
find "${directory[@]}" ! -path "$work/*" -type f -printf '%s*\t%p\n' >"$work/1" 2>"$work/1e"
echo "Ok ( $(wc -l <"$work/1"), error $(wc -l <"$work/1e") )"

echo -n 'discarding small files... '
dist=''
if sizelimit; then
awk -vs="$sizelimit" '$1+0<s' "$work/1" > "$work/1s"
dist='s'
echo "Ok ( $( wc -l < "$work/1s" ) )"
awk -vs="$sizelimit" '$1+0<s' "$work/1" >"$work/1s"
dist='s'
echo "Ok ( $(wc -l <"$work/1s") )"
else
echo 'not needed'
echo 'not needed'
fi

echo -n 'searching for duplicated file sizes... '
sort -n "$work/1$dist" | cut -d $'\t' -f 1 | uniq -d > "$work/2"
echo "Ok ( $( wc -l < "$work/2" ) )"
sort -n "$work/1$dist" | cut -d $'\t' -f 1 | uniq -d >"$work/2"
echo "Ok ( $(wc -l <"$work/2") )"

echo -n 'creating list of potential duplicated files... '
grep -w -F -f "$work/2" "$work/1" | cut -d $'\t' -f 2- > "$work/3"
echo "Ok ( $( wc -l < "$work/3" ) )"
grep -w -F -f "$work/2" "$work/1" | cut -d $'\t' -f 2- >"$work/3"
echo "Ok ( $(wc -l <"$work/3") )"

echo -n 'collecting checksums... '
tr '\n' '\0' < "$work/3" | xargs -0 "$summer" > "$work/4" 2> "$work/4e"
echo "Ok ( $( wc -l < "$work/4" ), error $( wc -l < "$work/4e" ) )"
tr '\n' '\0' <"$work/3" | xargs -0 "$summer" >"$work/4" 2>"$work/4e"
echo "Ok ( $(wc -l <"$work/4"), error $(wc -l <"$work/4e") )"

echo -n 'searching for duplicated checksums... '
sort -n "$work/4" | cut -d ' ' -f 1 | uniq -d > "$work/5"
echo "Ok ( $( wc -l < "$work/5" ) )"
sort -n "$work/4" | cut -d ' ' -f 1 | uniq -d >"$work/5"
echo "Ok ( $(wc -l <"$work/5") )"

echo -n 'preparing the checksum list for fast search... '
dist=''
read -r line < "$work/4"
read -r line <"$work/4"
if "$line" == *\**; then
echo 'not needed'
echo 'not needed'
else
sed 's/ / */' "$work/4" > "$work/4s"
dist='s'
echo 'Ok'
sed 's/ / */' "$work/4" >"$work/4s"
dist='s'
echo 'Ok'
fi

echo -n 'creating list of duplicated files... '
sed 's/$/ */' "$work/5" | grep -F -f - "$work/4$dist" | sort > "$work/6"
echo "Ok ( $( wc -l < "$work/6" ) )"
sed 's/$/ */' "$work/5" | grep -F -f - "$work/4$dist" | sort >"$work/6"
echo "Ok ( $(wc -l <"$work/6") )"

echo -n 'creating result list... '
awk -F' \\*' -vOFS='' 'l!=$1{print""}{l=$1;$1=""}1' "$work/6" > "duplicated.txt"
awk -F' \\*' -vOFS='' 'l!=$1{print""}{l=$1;$1=""}1' "$work/6" >"duplicated.txt"
echo "Ok ( duplicated.txt )"

echo -n 'cleaning up temporary data... '
size="$( du -h "$work" | cut -d $'\t' -f 1 )"
size="$(du -h "$work" | cut -d $'\t' -f 1)"
#rm -r -f "$work"
echo "Ok ( $size )"

echo -n 'all done in'
sec=$SECONDS
(( sec/60/60 )) && echo -n " $(( sec/60/60 )) hours"
(( sec/60%60 )) && echo -n " $(( sec/60%60 )) minutes"
(( sec%60 )) && echo -n " $(( sec%60 )) seconds"
(( sec )) || echo -n ' no time'
((sec/60/60)) && echo -n " $((sec/60/60)) hours"
((sec/60 % 60)) && echo -n " $((sec/60 % 60)) minutes"
((sec % 60)) && echo -n " $((sec % 60)) seconds"
((sec)) || echo -n ' no time'
echo '.'
cat duplicate.txt

+ 16
- 0
git-fix View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
git filter-branch -f --env-filter '
OLD_EMAIL="$1"
CORRECT_NAME="Cian Butler"
CORRECT_EMAIL="butlerx@notthe.cloud"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

+ 131
- 107
killrenegade View File

@@ -3,22 +3,32 @@

shopt -s extglob

signal=( '' 'SIGHUP' 'SIGINT' 'SIGQUIT' 'SIGILL' 'SIGTRAP' 'SIGABRT' 'SIGBUS' 'SIGFPE' 'SIGKILL' 'SIGUSR1' 'SIGSEGV' 'SIGUSR2' 'SIGPIPE' 'SIGALRM' 'SIGTERM' 'SIGSTKFLT' 'SIGCHLD' 'SIGCONT' 'SIGSTOP' 'SIGTSTP' 'SIGTTIN' 'SIGTTOU' 'SIGURG' 'SIGXCPU' 'SIGXFSZ' 'SIGVTALRM' 'SIGPROF' 'SIGWINCH' 'SIGIO' 'SIGPWR' 'SIGSYS' )
sect=( '' ); comm=(); user=(); uown=(); mcpu=(); mmem=(); sign=(); inte=()
signal=('' 'SIGHUP' 'SIGINT' 'SIGQUIT' 'SIGILL' 'SIGTRAP' 'SIGABRT' 'SIGBUS' 'SIGFPE' 'SIGKILL' 'SIGUSR1' 'SIGSEGV' 'SIGUSR2' 'SIGPIPE' 'SIGALRM' 'SIGTERM' 'SIGSTKFLT' 'SIGCHLD' 'SIGCONT' 'SIGSTOP' 'SIGTSTP' 'SIGTTIN' 'SIGTTOU' 'SIGURG' 'SIGXCPU' 'SIGXFSZ' 'SIGVTALRM' 'SIGPROF' 'SIGWINCH' 'SIGIO' 'SIGPWR' 'SIGSYS')
sect=('')
comm=()
user=()
uown=()
mcpu=()
mmem=()
sign=()
inte=()
sec=''
nr=0
verb=0

echo 'killrenegade version 1.2 august 2008 written by Feherke'

while (( $# )); do
case "$1" in
'-i') shift; inifile="$1" ;;
'--ini='*) inifile="${1#*=}" ;;
'-V'|'--verbose') (( verb++ )) ;;
'-v'|'--version') exit ;;
'-h'|'--help'|'-?')
cat <<ENDOFSYNTAX
while (($#)); do
case "$1" in
'-i')
shift
inifile="$1"
;;
'--ini='*) inifile="${1#*=}" ;;
'-V' | '--verbose') ((verb++)) ;;
'-v' | '--version') exit ;;
'-h' | '--help' | '-?')
cat <<ENDOFSYNTAX
kill the processes with given name which use to much resources
Syntax :
killrenegade.sh [-i inifile] [-V]
@@ -26,71 +36,80 @@ Parameters :
-i inifile | --ini=inifile - settings file to be read ( killrenegade.ini )
-V | --verbose - verbose mode, use twice for more verbose ( no )
ENDOFSYNTAX
exit
;;
esac
shift
exit
;;
esac
shift
done

selfdir="${0%/*}"
: "${inifile=${0%.sh}.ini}"
[[ -f "$inifile" ]] || { echo 'killrenegade: error no settings'; exit 1; }
[[ -f "$inifile" ]] || {
echo 'killrenegade: error no settings'
exit 1
}

echo -n 'reading settings... '

while read -r str; do
[[ "$str" ]] || continue
[[ "${str:0:1}" == '#' ]] && continue
[[ "$str" == "[*]" ]] && {
sec="${str:1:$(( ${#str}-2 ))}"
if [[ "$sec" == 'default' ]]; then
nr=0
else
[[ "$sec" != 'general' ]] && { nr=${#sect[@]}; sect[nr]="$sec"; }
fi
continue
}
[[ "$sec" ]] || continue
[[ "${str#?*=}" != "$str" ]] && {
IFS='=' read -r key val <<< "$str"
if [[ "$sec" == 'general' ]]; then
eval "$key=$val"
else
case "$key" in
'command') comm[nr]="$val" ;;
'user') user[nr]="$val" ;;
'belong') uown[nr]="$val" ;;
'processor') mcpu[nr]="$val" ;;
'memory') mmem[nr]="$val" ;;
'signal') sign[nr]="$val" ;;
'interval') inte[nr]="$val" ;;
esac
fi
}
done < "$inifile"

(( nrsec=${#sect[@]}-1 ))
[[ "$str" ]] || continue
[[ "${str:0:1}" == '#' ]] && continue
[[ "$str" == "[*]" ]] && {
sec="${str:1:$((${#str} - 2))}"
if [[ "$sec" == 'default' ]]; then
nr=0
else
[[ "$sec" != 'general' ]] && {
nr=${#sect[@]}
sect[nr]="$sec"
}
fi
continue
}
[[ "$sec" ]] || continue
[[ "${str#?*=}" != "$str" ]] && {
IFS='=' read -r key val <<<"$str"
if [[ "$sec" == 'general' ]]; then
eval "$key=$val"
else
case "$key" in
'command') comm[nr]="$val" ;;
'user') user[nr]="$val" ;;
'belong') uown[nr]="$val" ;;
'processor') mcpu[nr]="$val" ;;
'memory') mmem[nr]="$val" ;;
'signal') sign[nr]="$val" ;;
'interval') inte[nr]="$val" ;;
esac
fi
}
done <"$inifile"

((nrsec = ${#sect[@]} - 1))

echo "ok, $nrsec sections found"
echo -n 'verifying settings... '

err=0
for ((i=1;i<=nrsec;i++)); do
comm[i]="${comm[i]:-comm[0]}"
user[i]="${user[i]:-${user[0]:-root}}"
uown[i]="${uown[i]:-${uown[0]:-no}}"
mcpu[i]="${mcpu[i]:-${mcpu[0]:-10}}"
mmem[i]="${mmem[i]:-${mmem[0]:-25}}"
sign[i]="${sign[i]:-${sign[0]:-SIGTERM}}"
inte[i]="${inte[i]:-${inte[0]:-60}}"
last[i]=0
[[ "${sign[$i]}" == @([[:alpha:]])* ]] || sign[i]="${signal[sign[i]]}"
[[ "${comm[i]}" ]] || (( err++ ))
for ((i = 1; i <= nrsec; i++)); do
comm[i]="${comm[i]:-comm[0]}"
user[i]="${user[i]:-${user[0]:-root}}"
uown[i]="${uown[i]:-${uown[0]:-no}}"
mcpu[i]="${mcpu[i]:-${mcpu[0]:-10}}"
mmem[i]="${mmem[i]:-${mmem[0]:-25}}"
sign[i]="${sign[i]:-${sign[0]:-SIGTERM}}"
inte[i]="${inte[i]:-${inte[0]:-60}}"
last[i]=0
[[ "${sign[$i]}" == @([[:alpha:]])* ]] || sign[i]="${signal[sign[i]]}"
[[ "${comm[i]}" ]] || ((err++))
done

(( err )) && { echo "killrenegade: $err error(s) in settings"; exit 2; }
((err)) && {
echo "killrenegade: $err error(s) in settings"
exit 2
}

last[0]=$( date +'%s' )
last[0]=$(date +'%s')
[[ "$logfile" && ! -e "$logfile" ]] && touch "$logfile"
[[ "$logfile" && -x "$selfdir/killstatistic.awk" ]] || htmldir=''
[[ "$htmldir" && ! -e "$htmldir" ]] && mkdir -p "$htmldir"
@@ -98,58 +117,63 @@ last[0]=$( date +'%s' )
echo 'ok, defaults used where needed'

[[ "$USER" != 'root' ]] && {
echo "running as $USER, restrictions applied"
for ((i=1;i<=nrsec;i++)); do
user[i]="$USER"
uown[i]='yes'
done
echo "running as $USER, restrictions applied"
for ((i = 1; i <= nrsec; i++)); do
user[i]="$USER"
uown[i]='yes'
done
}

for ((i=0;i<=nrsec;i++)); do echo -e "$i\t${sect[i]}\t${comm[i]}\t${user[i]}\t${uown[i]}\t${mcpu[i]}\t${mmem[i]}\t${sign[i]}\t${inte[i]}"; done
for ((i = 0; i <= nrsec; i++)); do
echo -e "$i\\t${sect[i]}\\t${comm[i]}\\t${user[i]}\\t${uown[i]}\\t${mcpu[i]}\\t${mmem[i]}\\t${sign[i]}\\t${inte[i]}"
done

(( verb )) && echo 'verbose mode on, will announce kills'
((verb)) && echo 'verbose mode on, will announce kills'

echo "started on $( date ), Ctrl-C to stop..."
echo "started on $(date), Ctrl-C to stop..."

while :; do
(( verb>1 )) && date
now=$( date +'%s' )
for ((i=1;i<=nrsec;i++)); do
if (( last[i]+inte[i]<=now )); then

(( verb>1 )) && echo " - ${sect[i]}"

ps h -C "${comm[i]}" -o 'pid user=twentycharacterswide %cpu %mem' | \
while read -r pid user cpu mem; do

[[ "${user[i]}" != '-' ]] && { grep -q -w "$( sed -n '/no/i-v' <<< "${uown[i]}" )" "$user" <<< "${user[i]}" || continue; }

cpui="${cpu%.*}"
memi="${mem%.*}"

(( cpui>mcpu[i] || memi>mmem[i] )) && {
(( verb )) && echo -n "kill ${comm[i]} ( $pid ) "
for sig in ${sign[i]}; do
(( verb )) && echo -n "$sig... "
kill -"$sig" "$pid" && break
done
(( verb )) && echo 'done'
[[ "$logfile" ]] && echo -e "$user\t$( date +'%Y-%m-%d %H:%M:%S' )\t$cpu\t$mem\t${comm[$i]}\t$sig" >> "$logfile"
}
done
last[i]=$now
fi
done

[[ "$htmldir" ]] && (( last[0]+build<=now )) && {
(( verb )) && echo 'build'
"$selfdir/killstatistic.awk" -v htmldir="$htmldir" "$logfile" &
last[0]=$now
}

next=0
for ((i=1;i<=nrsec;i++)); do temp=$(( last[i]+inte[i] )); (( next==0 || next>temp )) && next=$temp; done

now=$( date +'%s' )
(( next>now )) && sleep $(( next-now ))
((verb > 1)) && date
now=$(date +'%s')
for ((i = 1; i <= nrsec; i++)); do
if ((last[i] + inte[i] <= now)); then

((verb > 1)) && echo " - ${sect[i]}"

ps h -C "${comm[i]}" -o 'pid user=twentycharacterswide %cpu %mem' |
while read -r pid user cpu mem; do

[[ "${user[i]}" != '-' ]] && { grep -q -w "$(sed -n '/no/i-v' <<<"${uown[i]}")" "$user" <<<"${user[i]}" || continue; }

cpui="${cpu%.*}"
memi="${mem%.*}"

((cpui > mcpu[i] || memi > mmem[i])) && {
((verb)) && echo -n "kill ${comm[i]} ( $pid ) "
for sig in ${sign[i]}; do
((verb)) && echo -n "$sig... "
kill -"$sig" "$pid" && break
done
((verb)) && echo 'done'
[[ "$logfile" ]] && echo -e "$user\\t$(date +'%Y-%m-%d %H:%M:%S')\\t$cpu\\t$mem\\t${comm[$i]}\\t$sig" >>"$logfile"
}
done
last[i]=$now
fi
done

[[ "$htmldir" ]] && ((last[0] + build <= now)) && {
((verb)) && echo 'build'
"$selfdir/killstatistic.awk" -v htmldir="$htmldir" "$logfile" &
last[0]=$now
}

next=0
for ((i = 1; i <= nrsec; i++)); do
temp=$((last[i] + inte[i]))
((next == 0 || next > temp)) && next=$temp
done

now=$(date +'%s')
((next > now)) && sleep $((next - now))
done

+ 117
- 85
minesweeper View File

@@ -4,108 +4,140 @@
shopt -s extglob
IFS=''

piece=( $'\e[1;30m.' $'\e[1;34m1' $'\e[1;32m2' $'\e[1;35m3' $'\e[1;36m4' $'\e[1;31m5' $'\e[33m6' $'\e[1;37m7' $'\e[0;40;37m8' $'\e[0;40;37m#' $'\e[0;40;31mF' $'\e[0;40;33m?' $'\e[1;31m*' $'\e[0;40;31mx' )
size=( 'S ' 10 10 15 'M ' 15 15 33 'L ' 20 20 60 'XL' 30 20 90 )
piece=($'\e[1;30m.' $'\e[1;34m1' $'\e[1;32m2' $'\e[1;35m3' $'\e[1;36m4' $'\e[1;31m5' $'\e[33m6' $'\e[1;37m7' $'\e[0;40;37m8' $'\e[0;40;37m#' $'\e[0;40;31mF' $'\e[0;40;33m?' $'\e[1;31m*' $'\e[0;40;31mx')
size=('S ' 10 10 15 'M ' 15 15 33 'L ' 20 20 60 'XL' 30 20 90)

function drawboard() {
[[ "$dxt" ]] || { dxt=$mx; dyf=0; dyt=$my; }
tput 'cup' $(( dyf+2 )) 0
echo -n $'\e[40m'
for ((j=dyf;j<dyt;j++)); do for ((i=0;i<dxt;i++)); do echo -n " ${piece[board[j*mx+i]]}"; done; echo ' '; done
echo -n $'\e[0m'
dxt=''
[[ "$dxt" ]] || {
dxt=$mx
dyf=0
dyt=$my
}
tput 'cup' $((dyf + 2)) 0
echo -n $'\e[40m'
for ((j = dyf; j < dyt; j++)); do
for ((i = 0; i < dxt; i++)); do echo -n " ${piece[board[j * mx + i]]}"; done
echo ' '
done
echo -n $'\e[0m'
dxt=''
}

function newgame() {
n='nNmM'; n="${n%$1*}"; n=${#n}
mx=${size[n*4+1]}; my=${size[n*4+2]}; mb=${size[n*4+3]}; mf=0
echo -n $'\e[0m'
clear
echo 'Mine Sweeper version 1.1 august 2008 written by Feherke'
echo "board : ${size[n*4]} size : $mx*$my mine : $mb flag : $mf "$'\e[43;30m:)\e[0m'
for ((i=0;i<mx*my;i++)); do bomb[i]=0; board[i]=9; done
for ((i=0;i<mb;i++)); do while :; do r=$(( RANDOM%(mx*my) )); (( bomb[r] )) || break; done; bomb[r]=1; done
drawboard
echo $'<\e[1mh\e[0m/\e[1mj\e[0m/\e[1mk\e[0m/\e[1ml\e[0m> Move <\e[1mg\e[0m> Step <\e[1mf\e[0m> Flag <\e[1mn\e[0m/\e[1mN\e[0m/\e[1mm\e[0m/\e[1mM\e[0m> New <\e[1mq\e[0m> Quit'
cx=0; cy=0
status=1
n='nNmM'
n="${n%$1*}"
n=${#n}
mx=${size[n * 4 + 1]}
my=${size[n * 4 + 2]}
mb=${size[n * 4 + 3]}
mf=0
echo -n $'\e[0m'
clear
echo 'Mine Sweeper version 1.1 august 2008 written by Feherke'
echo "board : ${size[n * 4]} size : $mx*$my mine : $mb flag : $mf "$'\e[43;30m:)\e[0m'
for ((i = 0; i < mx * my; i++)); do
bomb[i]=0
board[i]=9
done
for ((i = 0; i < mb; i++)); do
while :; do
r=$((RANDOM % (mx * my)))
((bomb[r])) || break
done
bomb[r]=1
done
drawboard
echo $'<\e[1mh\e[0m/\e[1mj\e[0m/\e[1mk\e[0m/\e[1ml\e[0m> Move <\e[1mg\e[0m> Step <\e[1mf\e[0m> Flag <\e[1mn\e[0m/\e[1mN\e[0m/\e[1mm\e[0m/\e[1mM\e[0m> New <\e[1mq\e[0m> Quit'
cx=0
cy=0
status=1
}

function gameover() {
for ((i=0;i<mx;i++)); do for ((j=0;j<my;j++)); do
(( bomb[j*mx+i]==1 && board[j*mx+i]==9 )) && board[j*mx+i]=12
(( bomb[j*mx+i]==0 && board[j*mx+i]==10 )) && board[j*mx+i]=13
done; done
drawboard
tput 'cup' 1 52
echo -n $'\e[43;30m:(\e[0m'
status=0
for ((i = 0; i < mx; i++)); do for ((j = 0; j < my; j++)); do
((bomb[j * mx + i] == 1 && board[j * mx + i] == 9)) && board[j * mx + i]=12
((bomb[j * mx + i] == 0 && board[j * mx + i] == 10)) && board[j * mx + i]=13
done; done
drawboard
tput 'cup' 1 52
echo -n $'\e[43;30m:(\e[0m'
status=0
}

function makestep() {
local i j
local sx=${1:-$cx} sy=${2:-$cy}
[[ "${board[sy*mx+sx]}" != @(9|10|11) ]] && return
(( bomb[cy*mx+cx]==1 )) && { gameover; return; }
[[ "$1" ]] || {
dxt=$sx; dyf=$sy; dyt=$sy
tput 'cup' 1 52
echo -n $'\e[43;30m:o\e[0m'
}
(( dxt=dxt>sx?dxt:sx+1 )); (( dyf=dyf<sy?dyf:sy )); (( dyt=dyt>sy?dyt:sy+1 ))
n=0
for ((i=-1;i<=1;i++)); do for ((j=-1;j<=1;j++)); do
(( (i!=0 || j!=0) && sx+i>=0 && sx+i<mx && sy+j>=0 && sy+j<my )) && (( bomb[(sy+j)*mx+(sx+i)]==1 )) && (( n++ ))
done; done
board[sy*mx+sx]=$n
(( n )) || {
for ((i=-1;i<=1;i++)); do for ((j=-1;j<=1;j++)); do
(( (i!=0 || j!=0) && sx+i>=0 && sx+i<mx && sy+j>=0 && sy+j<my )) && makestep $(( sx+i )) $(( sy+j ))
done; done
}
[[ "$1" ]] || {
drawboard
tput 'cup' 1 52
echo -n $'\e[43;30m:)\e[0m'
}
local i j
local sx=${1:-$cx} sy=${2:-$cy}
[[ "${board[sy * mx + sx]}" != @(9|10|11) ]] && return
((bomb[cy * mx + cx] == 1)) && {
gameover
return
}
[[ "$1" ]] || {
dxt=$sx
dyf=$sy
dyt=$sy
tput 'cup' 1 52
echo -n $'\e[43;30m:o\e[0m'
}
((dxt = dxt > sx ? dxt : sx + 1))
((dyf = dyf < sy ? dyf : sy))
((dyt = dyt > sy ? dyt : sy + 1))
n=0
for ((i = -1; i <= 1; i++)); do for ((j = -1; j <= 1; j++)); do
(((i != 0 || j != 0) && sx + i >= 0 && sx + i < mx && sy + j >= 0 && sy + j < my)) && ((bomb[(sy + j) * mx + (sx + i)] == 1)) && ((n++))
done; done
board[sy * mx + sx]=$n
((n)) || {
for ((i = -1; i <= 1; i++)); do for ((j = -1; j <= 1; j++)); do
(((i != 0 || j != 0) && sx + i >= 0 && sx + i < mx && sy + j >= 0 && sy + j < my)) && makestep $((sx + i)) $((sy + j))
done; done
}
[[ "$1" ]] || {
drawboard
tput 'cup' 1 52
echo -n $'\e[43;30m:)\e[0m'
}
}

function putflag() {
[[ ${board[cy*mx+cx]} != @(9|10|11) ]] && return
board[cy*mx+cx]=$(( (board[cy*mx+cx]-9+1)%3+9 ))
(( board[cy*mx+cx]==10 )) && (( mf++ ))
(( board[cy*mx+cx]==11 )) && (( mf-- ))
(( mf==mb )) && {
n=0
for ((i=0;i<mx;i++)); do for ((j=0;j<my;j++)); do
(( bomb[j*mx+i]==1 && board[j*mx+i]==10 )) && (( n++ ))
done; done
tput 'cup' 1 52
echo -n $'\e[43;30mB)\e[0m'
status=0
}
tput 'cup' 1 47
echo -en "\e[0m$mf "
[[ ${board[cy * mx + cx]} != @(9|10|11) ]] && return
board[cy * mx + cx]=$(((board[cy * mx + cx] - 9 + 1) % 3 + 9))
((board[cy * mx + cx] == 10)) && ((mf++))
((board[cy * mx + cx] == 11)) && ((mf--))
((mf == mb)) && {
n=0
for ((i = 0; i < mx; i++)); do for ((j = 0; j < my; j++)); do
((bomb[j * mx + i] == 1 && board[j * mx + i] == 10)) && ((n++))
done; done
tput 'cup' 1 52
echo -n $'\e[43;30mB)\e[0m'
status=0
}
tput 'cup' 1 47
echo -en "\\e[0m$mf "
}
newgame 'n'
while :; do
tput 'cup' $(( cy+2 )) $(( cx*2 ))
echo -en "\e[1;40;37m[${piece[board[cy*mx+cx]]}\e[1;37m]\b\b"
read -r -s -n 1 a
[[ "$a" == '' ]] && { read -r -s -n 1 a; [[ "$a" == '[' ]] && read -r -s -n 1 a; }
echo -en "\b ${piece[board[cy*mx+cx]]} \b\b"
(( status!=1 )) && [[ "$a" != [nNmMrq] ]] && continue
case "$a" in
'h'|'a'|'D'|'4') (( cx>0?cx--:0 )) ;;
'j'|'s'|'B'|'2') (( cy<my-1?cy++:0 )) ;;
'k'|'w'|'A'|'8') (( cy>0?cy--:0 )) ;;
'l'|'d'|'C'|'6') (( cx<mx-1?cx++:0 )) ;;
'g'|' '|'') makestep ;;
'f'|'0') putflag ;;
'n'|'N'|'m'|'M') newgame "$a" ;;
'r') drawboard ;;
'q') break ;;
esac
tput 'cup' $((cy + 2)) $((cx * 2))
echo -en "\\e[1;40;37m[${piece[board[cy * mx + cx]]}\\e[1;37m]\\b\\b"
read -r -s -n 1 a
[[ "$a" == '' ]] && {
read -r -s -n 1 a
[[ "$a" == '[' ]] && read -r -s -n 1 a
}
echo -en "\\b ${piece[board[cy * mx + cx]]} \\b\\b"
((status != 1)) && [[ "$a" != [nNmMrq] ]] && continue
case "$a" in
'h' | 'a' | 'D' | '4') ((cx > 0 ? cx-- : 0)) ;;
'j' | 's' | 'B' | '2') ((cy < my - 1 ? cy++ : 0)) ;;
'k' | 'w' | 'A' | '8') ((cy > 0 ? cy-- : 0)) ;;
'l' | 'd' | 'C' | '6') ((cx < mx - 1 ? cx++ : 0)) ;;
'g' | ' ' | '') makestep ;;
'f' | '0') putflag ;;
'n' | 'N' | 'm' | 'M') newgame "$a" ;;
'r') drawboard ;;
'q') break ;;
esac
done
echo -n $'\e[0m'
clear

Loading…
Cancel
Save