<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>script on</title><link>https://notes.freaks.dev/tags/script/</link><description>Recent content in script on</description><generator>Hugo -- gohugo.io</generator><lastBuildDate>Tue, 15 Nov 2022 00:00:00 +0000</lastBuildDate><atom:link href="https://notes.freaks.dev/tags/script/index.xml" rel="self" type="application/rss+xml"/><item><title>Bash Spin Function</title><link>https://notes.freaks.dev/scripts/bash_spin/</link><pubDate>Tue, 15 Nov 2022 00:00:00 +0000</pubDate><guid>https://notes.freaks.dev/scripts/bash_spin/</guid><description>Code spin() { local i=0 local sp=&amp;#34;/-\|&amp;#34; local n=${#sp} printf &amp;#34; &amp;#34; sleep 0.2 while true; do printf &amp;#34;\b%s&amp;#34; &amp;#34;${sp:i++%n:1}&amp;#34; sleep 0.2 done } Usage #!/bin/bash spin() { . . . } printf &amp;#34;Hacking NASA &amp;#34; spin &amp;amp; spinpid=$! trap &amp;#39;kill $spinpid&amp;#39; SIGTERM SIGKILL { # You can run all the command you want here. I will only run sleep command. sleep 5 } &amp;amp;&amp;gt;/dev/null kill $spinpid printf &amp;#34;\b \t[done]\n&amp;#34; printf &amp;#34;NASA has been successfully hacked!</description></item><item><title>Multiple Select Option Function</title><link>https://notes.freaks.dev/scripts/multi_select_option/</link><pubDate>Tue, 15 Nov 2022 00:00:00 +0000</pubDate><guid>https://notes.freaks.dev/scripts/multi_select_option/</guid><description>Code multi_select_option() { # little helpers for terminal print control and key input ESC=$( printf &amp;#34;\033&amp;#34;) cursor_blink_on() { printf &amp;#34;%s[?25h&amp;#34; &amp;#34;$ESC&amp;#34;; } cursor_blink_off() { printf &amp;#34;%s[?25l&amp;#34; &amp;#34;$ESC&amp;#34;; } cursor_to() { printf &amp;#34;%s[%s;%sH&amp;#34; &amp;#34;$ESC&amp;#34; &amp;#34;$1&amp;#34; &amp;#34;${2:-1}&amp;#34;; } print_inactive() { printf &amp;#34;%s %s &amp;#34; &amp;#34;$2&amp;#34; &amp;#34;$1&amp;#34;; } print_active() { printf &amp;#34;%s %s[7m %s %s[27m&amp;#34; &amp;#34;$2&amp;#34; &amp;#34;$ESC&amp;#34; &amp;#34;$1&amp;#34; &amp;#34;$ESC&amp;#34;; } get_cursor_row() { IFS=&amp;#39;;&amp;#39; read -sdR -p $&amp;#39;\E[6n&amp;#39; ROW COL; echo &amp;#34;${ROW#*[}&amp;#34;; } get_cursor_col() { IFS=&amp;#39;;&amp;#39; read -sdR -p $&amp;#39;\E[6n&amp;#39; ROW COL; echo &amp;#34;${COL#*[}&amp;#34;; } # local lines=$(tput lines) local cols=$(tput cols) local return_value=$1 local colmax=$2 local offset=$(( cols / colmax )) local -n options=$3 # local -n defaults=$4 local defaults=() local selected=() for ((i=0; i&amp;lt;${#options[@]}; i++)); do if [[ ${defaults[i]} = &amp;#34;true&amp;#34; ]]; then selected+=(&amp;#34;true&amp;#34;) else selected+=(&amp;#34;false&amp;#34;) fi printf &amp;#34;\n&amp;#34; done # determine current screen position for overwriting the options local lastrow=$(get_cursor_row) # local lastcol=$(get_cursor_col) local startrow=$(( lastrow - ${#options[@]} + 1 )) # local startcol=1 # ensure cursor and input echoing back on upon a ctrl+c during read -s trap &amp;#34;cursor_blink_on; stty echo; printf &amp;#39;\n&amp;#39;; exit&amp;#34; 2 cursor_blink_off key_input() { local key IFS= read -rsn1 key 2&amp;gt;/dev/null &amp;gt;&amp;amp;2 if [[ $key = &amp;#34;&amp;#34; ]]; then echo enter; fi; if [[ $key = $&amp;#39;\x20&amp;#39; ]]; then echo space; fi; if [[ $key = &amp;#34;k&amp;#34; ]]; then echo up; fi; if [[ $key = &amp;#34;j&amp;#34; ]]; then echo down; fi; if [[ $key = &amp;#34;h&amp;#34; ]]; then echo left; fi; if [[ $key = &amp;#34;l&amp;#34; ]]; then echo right; fi; if [[ $key = &amp;#34;a&amp;#34; ]]; then echo all; fi; if [[ $key = &amp;#34;n&amp;#34; ]]; then echo none; fi; if [[ $key = $&amp;#39;\x1b&amp;#39; ]]; then read -rsn2 key if [[ $key = [A || $key = k ]]; then echo up; fi; if [[ $key = [B || $key = j ]]; then echo down; fi; if [[ $key = [C || $key = l ]]; then echo right; fi; if [[ $key = [D || $key = h ]]; then echo left; fi; fi } toggle_option() { local option=$1 if [[ ${selected[option]} == true ]]; then selected[option]=false else selected[option]=true fi } toggle_option_multicol() { local option_row=$1 local option_col=$2 if [[ $option_row -eq -10 ]] &amp;amp;&amp;amp; [[ $option_row -eq -10 ]]; then for ((option=0;option&amp;lt;${#selected[@]};option++)); do selected[option]=true done else if [[ $option_row -eq -100 ]] &amp;amp;&amp;amp; [[ $option_row -eq -100 ]]; then for ((option=0;option&amp;lt;${#selected[@]};option++)); do selected[option]=false done else option=$(( option_col + option_row * colmax )) if [[ ${selected[option]} == true ]]; then selected[option]=false else selected[option]=true fi fi fi } print_options_multicol() { # print options by overwriting the last lines local curr_col=$1 local curr_row=$2 local curr_idx=0 local idx=0 local row=0 local col=0 curr_idx=$(( curr_col + curr_row * colmax )) for option in &amp;#34;${options[@]}&amp;#34;; do local prefix=&amp;#34;[ ]&amp;#34; if [[ ${selected[idx]} == &amp;#34;true&amp;#34; ]]; then prefix=&amp;#34;[✓]&amp;#34; fi row=$(( idx / colmax )) col=$(( idx - row * colmax )) cursor_to $(( startrow + row + 1)) $(( offset * col + 1)) if [ $idx -eq $curr_idx ]; then print_active &amp;#34;$option&amp;#34; &amp;#34;$prefix&amp;#34; else print_inactive &amp;#34;$option&amp;#34; &amp;#34;$prefix&amp;#34; fi ((idx++)) done } local active_row=0 local active_col=0 while true; do print_options_multicol $active_col $active_row # user key control case $(key_input) in space) toggle_option_multicol $active_row $active_col;; enter) print_options_multicol -1 -1; break;; up) (( active_row-- )); if [[ $active_row -lt 0 ]]; then active_row=0; fi;; down) (( active_row++ )); if [[ $(( ${#options[@]} % colmax )) -ne 0 ]]; then if [[ &amp;#34;$active_row&amp;#34; -ge $(( ${#options[@]} / colmax )) ]]; then active_row=$(( ${#options[@]} / colmax )); fi else if [[ &amp;#34;$active_row&amp;#34; -ge $(( ${#options[@]} / colmax -1 )) ]]; then active_row=$(( ${#options[@]} / colmax -1 )); fi fi;; left) (( active_col = active_col - 1 )); if [[ $active_col -lt 0 ]]; then active_col=0; fi;; right) (( active_col = active_col + 1 )); if [[ $active_col -ge $colmax ]]; then active_col=$(( colmax - 1 )) ; fi;; all) toggle_option_multicol -10 -10 ;; none) toggle_option_multicol -100 -100 ;; esac done # cursor position back to normal cursor_to $(( startrow + ( ${#options[@]} / colmax ) + 2 )) printf &amp;#34;\n&amp;#34; cursor_blink_on eval &amp;#34;$return_value&amp;#34;=&amp;#39;(&amp;#34;${selected[@]}&amp;#34;)&amp;#39; } Usage #!</description></item><item><title>POSIX Spin Function</title><link>https://notes.freaks.dev/scripts/posix_spin/</link><pubDate>Tue, 15 Nov 2022 00:00:00 +0000</pubDate><guid>https://notes.freaks.dev/scripts/posix_spin/</guid><description>Code spin() { sp=&amp;#34;/-\|&amp;#34; printf &amp;#34; &amp;#34; sleep 0.2 while true; do printf &amp;#34;\b%.1s&amp;#34; &amp;#34;$sp&amp;#34; sp=${sp#?}${sp%???} sleep 0.2 done } Usage #!/bin/sh spin() { . . . } printf &amp;#34;Hacking NASA &amp;#34; spin &amp;amp; spinpid=$! trap &amp;#39;kill $spinpid&amp;#39; SIGTERM SIGKILL { # You can run all the command you want here. I will only run sleep command. sleep 5 } &amp;gt;/dev/null 2&amp;gt;&amp;amp;1 kill $spinpid printf &amp;#34;\b \t[done]\n&amp;#34; printf &amp;#34;NASA has been successfully hacked!</description></item><item><title>Run Step Function</title><link>https://notes.freaks.dev/scripts/run_step/</link><pubDate>Tue, 15 Nov 2022 00:00:00 +0000</pubDate><guid>https://notes.freaks.dev/scripts/run_step/</guid><description>Code setup_log=&amp;#34;$(mktemp -t setup_logXXX)&amp;#34; red=&amp;#34;\033[1;31m&amp;#34; green=&amp;#34;\033[1;32m&amp;#34; cyan=&amp;#34;\033[0;36m&amp;#34; normal=&amp;#34;\033[0m&amp;#34; spin() { local i=0 local sp=&amp;#34;/-\|&amp;#34; local n=${#sp} printf &amp;#34; &amp;#34; sleep 0.2 while true; do printf &amp;#34;\b${cyan}%s${normal}&amp;#34; &amp;#34;${sp:i++%n:1}&amp;#34; sleep 0.2 done } log() { exec 3&amp;gt;&amp;amp;1 4&amp;gt;&amp;amp;2 trap &amp;#39;exec 2&amp;gt;&amp;amp;4 1&amp;gt;&amp;amp;3&amp;#39; 0 1 2 3 exec 1&amp;gt;&amp;gt;&amp;#34;$setup_log&amp;#34; 2&amp;gt;&amp;amp;1 echo -e &amp;#34;\n${cyan}${1}${normal}\n&amp;#34; } run_step() { local msg=&amp;#34;$1&amp;#34; local func=&amp;#34;$2&amp;#34; local pos IFS=&amp;#39;[;&amp;#39; read -p $&amp;#39;\e[6n&amp;#39; -d R -a pos -rs local current_row=${pos[1]} local current_col=${pos[2]} printf &amp;#34;${cyan}${msg}${normal}\033[${current_row};50H&amp;#34; spin &amp;amp; spinpid=$!</description></item><item><title>Select Option Function</title><link>https://notes.freaks.dev/scripts/select_option/</link><pubDate>Tue, 15 Nov 2022 00:00:00 +0000</pubDate><guid>https://notes.freaks.dev/scripts/select_option/</guid><description>Code select_option() { # little helpers for terminal print control and key input ESC=$( printf &amp;#34;\033&amp;#34;) cursor_blink_on() { printf &amp;#34;%s[?25h&amp;#34; &amp;#34;$ESC&amp;#34;; } cursor_blink_off() { printf &amp;#34;%s[?25l&amp;#34; &amp;#34;$ESC&amp;#34;; } cursor_to() { printf &amp;#34;%s[%s;%sH&amp;#34; &amp;#34;$ESC&amp;#34; &amp;#34;$1&amp;#34; &amp;#34;${2:-1}&amp;#34;; } print_option() { printf &amp;#34;%s %s &amp;#34; &amp;#34;$2&amp;#34; &amp;#34;$1&amp;#34;; } print_selected() { printf &amp;#34;%s %s[7m %s %s[27m&amp;#34; &amp;#34;$2&amp;#34; &amp;#34;$ESC&amp;#34; &amp;#34;$1&amp;#34; &amp;#34;$ESC&amp;#34;; } get_cursor_row() { IFS=&amp;#39;;&amp;#39; read -sdR -p $&amp;#39;\E[6n&amp;#39; ROW COL; echo &amp;#34;${ROW#*[}&amp;#34;; } get_cursor_col() { IFS=&amp;#39;;&amp;#39; read -sdR -p $&amp;#39;\E[6n&amp;#39; ROW COL; echo &amp;#34;${COL#*[}&amp;#34;; } key_input() { local key IFS= read -rsn1 key 2&amp;gt;/dev/null &amp;gt;&amp;amp;2 if [[ $key = &amp;#34;&amp;#34; ]]; then echo enter; fi if [[ $key = $&amp;#39;\x20&amp;#39; ]]; then echo space; fi if [[ $key = &amp;#34;k&amp;#34; ]]; then echo up; fi if [[ $key = &amp;#34;j&amp;#34; ]]; then echo down; fi if [[ $key = &amp;#34;h&amp;#34; ]]; then echo left; fi if [[ $key = &amp;#34;l&amp;#34; ]]; then echo right; fi if [[ $key = &amp;#34;a&amp;#34; ]]; then echo all; fi if [[ $key = &amp;#34;n&amp;#34; ]]; then echo none; fi if [[ $key = $&amp;#39;\x1b&amp;#39; ]]; then read -rsn2 key if [[ $key = [A || $key = k ]]; then echo up; fi if [[ $key = [B || $key = j ]]; then echo down; fi if [[ $key = [C || $key = l ]]; then echo right; fi if [[ $key = [D || $key = h ]]; then echo left; fi fi } print_options_multicol() { # print options by overwriting the last lines local curr_col=$1 local curr_row=$2 local curr_idx=0 local idx=0 local row=0 local col=0 curr_idx=$(( curr_col + curr_row * colmax )) for option in &amp;#34;${options[@]}&amp;#34;; do row=$(( idx / colmax )) col=$(( idx - row * colmax )) cursor_to $(( startrow + row + 1 )) $(( offset * col + 1 )) if [[ $idx -eq $curr_idx ]]; then print_selected &amp;#34;$option&amp;#34; else print_option &amp;#34;$option&amp;#34; fi ((idx++)) done } # initially print empty new lines (scroll down if at bottom of screen) for opt; do printf &amp;#34;\n&amp;#34;; done # determine current screen position for overwriting the options # local return_value=$1 local lastrow=$(get_cursor_row) # local lastcol=$(get_cursor_col) local startrow=$(( lastrow - $# )) # local startcol=1 # local lines=$( tput lines ) local cols=$(tput cols) local colmax=$2 local offset=$(( cols / colmax )) # local size=$4 shift 4 # ensure cursor and input echoing back on upon a ctrl+c during read -s trap &amp;#34;cursor_blink_on; stty echo; printf &amp;#39;\n&amp;#39;; exit&amp;#34; 2 cursor_blink_off local active_row=0 local active_col=0 while true; do print_options_multicol $active_col $active_row # user key control case $(key_input) in enter) break;; up) (( active_row-- )); if [[ &amp;#34;$active_row&amp;#34; -lt 0 ]]; then active_row=0; fi;; down) (( active_row++ )); if [[ $(( ${#options[@]} % colmax )) -ne 0 ]]; then if [[ &amp;#34;$active_row&amp;#34; -ge $(( ${#options[@]} / colmax )) ]]; then active_row=$(( ${#options[@]} / colmax )); fi else if [[ &amp;#34;$active_row&amp;#34; -ge $(( ${#options[@]} / colmax -1 )) ]]; then active_row=$(( ${#options[@]} / colmax -1 )); fi fi;; left) (( active_col = active_col - 1 )); if [[ &amp;#34;$active_col&amp;#34; -lt 0 ]]; then active_col=0; fi;; right) (( active_col = active_col + 1 )); if [[ &amp;#34;$active_col&amp;#34; -ge &amp;#34;$colmax&amp;#34; ]]; then active_col=$(( colmax - 1 )) ; fi;; esac done # cursor position back to normal cursor_to $(( lastrow - $# + ( $# / colmax ) - 1 )) printf &amp;#34;\n&amp;#34; cursor_blink_on return $(( active_col + active_row * colmax )) } Usage #!</description></item></channel></rss>