🖥️while

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the while command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                ██╗    ██╗██╗  ██╗██╗██╗     ███████╗
#                ██║    ██║██║  ██║██║██║     ██╔════╝
#                ██║ █╗ ██║███████║██║██║     █████╗  
#                ██║███╗██║██╔══██║██║██║     ██╔══╝  
#                ╚███╔███╔╝██║  ██║██║███████╗███████╗
#                 ╚══╝╚══╝ ╚═╝  ╚═╝╚═╝╚══════╝╚══════╝
                                                     
                                                     

while sleep 30; do curl -s 192.168.0.106 |jq '.temp'|awk '$1>80'|ts ;done
# Print temperature value from sensor w/ time when temp is over 80

while sleep 30; do curl -s 192.168.0.106 |egrep "temp: 8[0-9]" | ts ;done
# Output json data from sensor w/ time when temp reaches over 80.

while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &
# Just execute the below command on you prompt and see the date and time on the above right corner of terminal.

while :;do rec -t raw /dev/null rate 32k silence 1 0.1 2% 1 0.0 2% && ping -c 1 home;sleep 1; done
# Ping home when mic "hears" something.

while read -s; do date +%H:%M; done |uniq -c
# Press enter every time you hear child cough to determine if per min rate is going down.

while read line; do echo "line length is ${#line}"; done
# While loops are good for reading data from stdin and running processes against it

while :; do echo beeww beeeeooooowwwww dun dun dun dun brtbrtbrt | espeak --stdout | play - pitch -400 bend .3,-600,.3 ; done
#

while ! curl -m 10 http://www.example\.com/ ; do echo still down ; sleep 1m ; done ; play alarm.wav
# Play alarm.wav once site is back.

while :; do echo wub wub wub | espeak --stdout | play - pitch -400 bend .3,-600,.3 ; done
# CLI generated dubstep. 

while :;do curl -Ls "X\.com"|md5sum;sleep 5m;done|awk '{if(NR>1&&l!=$1){system("echo the site changed|mail -s NOTIFY you@isp\.net");};l=$1}'

while true; do for i in 02E{{9..5},{6..8}}; do printf "\u${i}O=o>";sleep 0.09;printf "\b\b\b\b\b";done;printf "_";done
# Pave the road.

while true;do N=$(($RANDOM % $COLUMNS));for i in $( seq 1 $N );do echo -n " ";done;echo \*;done
# Snow storm in the terminal

while :;do wget http://example\.com/exposed -O exposed-$(date +%Y%m%d%H%M%S)||break;sleep 1h;done
# Get data and timestamp til 40X status

while true;do for i in ~/Pictures/*.jpg ;do cp "$i" ~/slideshow.jpg ;sleep 5;done; done
# Poor mans auto updating slideshow image.

while :;do iwlist wlan0 scan |awk -F\" '/ESSID/{print $2}' |espeak;done
# Your Linux laptop says ESSID names while you drive around.

while sleep 30; do curl -s 192.168.0.106 |jq '.temp'|awk '$1>80'|ts ;done
# Print temperature value from sensor w/ time when temp is over 80

while sleep 30; do curl -s 192.168.0.106 |egrep "temp: 8[0-9]" | ts ;done
# Output json data from sensor w/ time when temp reaches over 80.

while :;do rec -t raw /dev/null rate 32k silence 1 0.1 2% 1 0.0 2% && ping -c 1 home;sleep 1; done
# Ping home when mic "hears" something.

while read -s; do date +%H:%M; done |uniq -c
# Press enter every time you hear child cough to determine if per min rate is going down.

while read line; do echo "line length is ${#line}"; done
# While loops are good for reading data from stdin and running processes against it

while true; do for i in 02E{{9..5},{6..8}}; do printf "\u${i}O=o>";sleep 0.09;printf "\b\b\b\b\b";done;printf "_";done
# Pave the road.

while :;do wget http://example\.com/exposed -O exposed-$(date +%Y%m%d%H%M%S)||break;sleep 1h;done
# Get data and timestamp til 40X status

while :; do echo beeww beeeeooooowwwww dun dun dun dun brtbrtbrt | espeak --stdout | play - pitch -400 bend .3,-600,.3 ; done
#

while ! curl -m 10 http://www.example\.com/ ; do echo still down ; sleep 1m ; done ; play alarm.wav
# Play alarm.wav once site is back.

while :; do echo wub wub wub | espeak --stdout | play - pitch -400 bend .3,-600,.3 ; done
# CLI generated dubstep. 

while true;do for i in ~/Pictures/*.jpg ;do cp "$i" ~/slideshow.jpg ;sleep 5;done; done
# Poor mans auto updating slideshow image.

while :;do curl -Ls "X\.com"|md5sum;sleep 5m;done|awk '{if(NR>1&&l!=$1){system("echo the site changed|mail -s NOTIFY you@isp\.net");};l=$1}'

i=10; while [ $i -gt 0 ]; do echo $i seconds remaining; i=`expr $i - 1`; sleep 1;  done
# cli countdown 10 -> 0

while :;do iwlist wlan0 scan |awk -F\" '/ESSID/{print $2}' |espeak;done
# Your Linux laptop says ESSID names while you drive around.

while true;do iwlist wlan0 scan |awk -F\" '/ESSID/{print $2}' |espeak;done 
# Your laptop says ESSID names while you drive around.

while true; do echo "Hey look shells." ; sleep 1 ; done
#

while sleep 5; do pgrep wget > /dev/null || { date ; systemctl suspend ; break ; }; done
# Suspend computer after wget exit

w=50;while v=$((${RANDOM:0:1}-3)); do read -p "Balance: $w | Play? (y/N) " a; [[ "$a" == "y" ]] && w=$((w+v));[[ $w -lt 0 ]] && break; done
#

while [ 1 ];do vardate=$(date +%d\-%m\-%Y\_%H.%M.%S);COUNTER=$((COUNTER + 1)); screencapture -t jpg -x ~/stockmon/$COUNTER.jpg; sleep 5; done;
# BASH Timelapsed Screenshot script

while true; do echo "$(date '+%D %T' | toilet -f term -F border --gay)"; sleep 1; done
#

while :; do xinput --query-state 10 |grep -q 'button\[1\]=down' && [[ $((RANDOM%20)) == 1 ]] && mplayer ScoobyHewoo.wav; sleep 0.2 ; done
# Rnd Playlist
     
while :; do [ ! -z "$a" ] && { a=""; xsel <<<"$a"; } || a=$(xsel); sleep 10; done 
# Clear your copy buffer after 10-20 seconds. #infosec
        
n=1;while :;do xinput --query-state 8|grep 'button\[1\]=d' &&{ xdotool type "$n"; n=$[n+1]; };sleep 0.3;done 
# Auto type next $n on click

# Poly synth. Press asdfghj. Uses sox.
n=CDEFGAB;l=asdfghj;while read -n1 k;do x=$(tr $l $n<<<$k);play -qn synth pl ${x}3 fade 0 .7 & done 

# Watch the Unix clock in style as it approaches 1.5 billion seconds. >10 hours to go.
while date +%s | figlet ; do sleep 1 ; clear ; done 

# "Infinite" data breach
while :;do rig |head -1 |tr '\n' ,;echo $RANDOM{,,,,} |tr -d ' ' ;done |sed -r -e 's/,(...)(..)(....).*/,\1-\2-\3/' 

while :; do diskutil apfs list | grep Decryption | awk {'print $4'}; sleep 10; done 
# Monitor FileVault progress during encryption/decryption

# hardcode dnsserver, no more rewriting of etc/resolv.conf -> runs in background rewriting /etc/resolv.conf periodically dns
while sudo sed -i -e 's/^\(nameserver\).*$/\1 $dns/' /etc/resolv.conf; do sleep 15; done &

# simple pomodoro
while true; do sleep $((40 * 60)); echo "Fuck away for some time"; sleep $((3 * 60)); done &

while true; do sleep .15; head /dev/urandom | tr -dc A-Za-z0-9; done
# Continuous random string of text

# Don't watch the clock yourself, run date after the last dd command is done.
while pidof dd ; do sleep 1; done; date 

# Watch the Unix clock in style with figlet and lolcat. Tonight it passes over 1555555555 seconds.
while date +%s | figlet | lolcat ; do sleep 1 ; clear ; done 

	
# While loop to pretty print system load (1, 5 & 15 minutes)
while [ 1 == 1 ]; do  cat /proc/loadavg | awk '{printf "1 minute load: %.2f\n", $(NF-5)}' && cat /proc/loadavg |awk '{printf "5 minute load: %.2f\n", $(NF-3)}' && cat /proc/loadavg |awk '{printf "15 minute load: %.2f\n", $(NF-2)}'; sleep 3; date; done

# While loop to pretty print system load (1, 5 & 15 minutes)
while :; do date; awk '{printf "1 minute load: %.2f\n", $1; printf "5 minute load: %.2f\n", $2; printf "15 minute load: %.2f\n", $3}' /proc/loadavg; sleep 3; done

while(1 -eq 1 ) {Get-Process -Name *APPNAME* | Select-Object -ExpandProperty ID | ForEach-Object {Get-NetTCPConnection -OwningProcess $_} -ErrorAction SilentlyContinue }
# Application network trace based on application name
# This command takes an application name as an argument and then it will listen to the tcp traffic and capture packets matching the process Id of the application. The output shows: local address / local port / Remote Address / Remote port / State / Owning Process ID This is sample output - yours may be different.
#       192.168.0.1                      50306     8.8.8.8                       443        Established Internet       21128

# While loop to pretty print system load (1, 5 & 15 minutes)
while :; do date; awk '{printf "1 minute load: %.2f\n", $1; printf "5 minute load: %.2f\n", $2; printf "15 minute load: %.2f\n", $3}' /proc/loadavg; sleep 3; done
# Explanation: while :; do ...; done is an infinite loop. You can interrupt it with Control-c. The file /proc/loadavg contains a line like this:
    0.01 0.04 0.07 1/917 25383
#  Where the first 3 columns represent the 1, 5, 15 minute loads, respectively. In the infinite loop we print the current date, then the load values nicely formatted (ignore the other values), then sleep for 3 seconds, and start again.
# Limitations: /proc/loadavg is only available in Linux.

## Related one-liners

# While loop to pretty print system load (1, 5 & 15 minutes)
while [ 1 == 1 ]; do  cat /proc/loadavg | awk '{printf "1 minute load: %.2f\n", $(NF-5)}' && cat /proc/loadavg |awk '{printf "5 minute load: %.2f\n", $(NF-3)}' && cat /proc/loadavg |awk '{printf "15 minute load: %.2f\n", $(NF-2)}'; sleep 3; date; done
# Explanation: top is great but this will make it easier to read and makes it easy to pipe to text file for historical review. kill with ctrl+c

# Passwords and username guessing or checks:
while read line; do username=$line; while read line; do smbclient -L yTARGET IP ADDRESS> -U $username%$line -g -d 0; echo $username:$line; done<<PASSWORDS>.txt; done<<USERNAMES>.txt

while [ true ]; do cat /proc/loadavg | perl -ne 'm/(^[^ ]+)/; $L = $1; $s=0.5 + $L; print "*" x $s; print " $L\n";' ; sleep 6; done
# It takes the first value of /prov/loadavg to print that many stars followed by the value. to have a rounded number of stars change it to be

while [ true ]; do cat /proc/loadavg | perl -ne 'm/(^[^ ]+)/; $L = $1; print "*" x $L; print " $L\n";' ; sleep 6; done
#  Visual system load display It takes the first value of /prov/loadavg to print that many stars followed by the value.

while :;do rig |head -1 |tr '\n' ,;echo $RANDOM{,,,,} |tr -d ' ' ;done |sed -r -e 's/,(...)(..)(....).*/,\1-\2-\3/' 
# "Infinite" data breach

# Process command output line by line in a while loop - This snippet allows to process the output of any bash command line by line.
while read -r line; do echo $line; done < <(YOUR COMMAND HERE);

while true ; do date | tr $'\n' ' ' ; du -sh One/ ; sleep 1d ; done 
# Check the size of the 'One' directory once per date, prefixing the line with a timestamp. Using tr to turn the normal newline from date into a space.

clear;look . |while read i;do echo "$i" |qrencode -o - -t UTF8 ; printf "%-20s" "$i" |toilet -w $COLUMNS ;sleep 0.1 ;printf "\e[0;0H" ; done 
# qrcode "animation"

while true; do printf "%s " $( date ); df -hP /share |grep /; sleep 10m; done 
# Poor person's filesystem time/space monitor in 10min increments. This was just a one off.

s=5;while true; do [ $(( $( date +%s ) % 600 )) -le $s ] && end=$'\n' || end=$'\r' ; printf "\e[2K%s %s$end" "$(date)" "$(df -hP / | grep /)"; sleep $s; done # Show space and update current line every 5 seconds, but every 10 minutes move down a line and save history as you go.

size=1 ; while ! stat conn.log ; do echo $size ; bro -e "redef encap_hdr_size=$size;" -r network-dump.pcap ; size=$((size+1)) ; done 
# Keep incrementing through a set of values until one is successful at creating a conn.log file. If it does not work will eventually need to Ctrl-C.

while :;do iwlist wlan0 scan |awk -F\" '/ESSID/{print $2}' |espeak;done 
# Your Linux laptop says the ESSID names while you drive around.

while :;do curl -Ls "X\.com"|md5sum;sleep 5m;done|awk '{if(NR>1&&l!=$1){system("echo the site changed|mail -s NOTIFY you@isp\.net");};l=$1}'
#

while sleep $(($RANDOM % 10)); do eject -T /dev/cdrom;sleep 0.1; done 
# If you have a cat and still have a cd/dvd/bluray rom drive, this keep a cat entertained for hours.

while true ; do tmux next-window ; sleep 10; done 
# Auto switch through the current windows in your running tmux session.

while [[ $(date +%Y) -ne 2019 ]];do figlet $(($(date -d 2019-01-01 +%s)-$(date +%s)));sleep 1;clear;done;figlet 'Happy New Year!' 
#countdown

while :; do echo wub wub wub | espeak --stdout | play - pitch -400 bend .3,-600,.3 ; done 
# CLI generated dubstep. Shout out to 

# Alert me by email when a disconnected or unreachable server comes back online
while ! ping -c1 the_host_down; do sleep 1; done && date | mail -s 'the host is back!' [email protected]

# Explanation: 
    # ping -c1 host sends just one ping and exits with success or error.
    # while will keep running until the ping succeeds
    # When the ping succeeds the while loop will end, and an email will be sent with the date as the message

# Limitations: 
    # Depending on your system the parameter for sending a single ping might be different from -c1
    # Depending on your system the mail command might be different or work differently

#==============================##==============================#
# CMD WHILE while-schleife
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░